Results 1 to 11 of 11
Thread: PHP/mySQL something simple!
Hybrid View
-
05-08-2005, 10:25 PM #1Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
PHP/mySQL something simple!
I have a list of names that are comma separated. I need a script that will go and look through a mysql table and find each of these names on separate rows, and edit some information. Can anyone help me out?
-
05-08-2005, 10:59 PM #2Junior Guru Wannabe
- Join Date
- Jan 2005
- Posts
- 46
Assuming you have the data like this:
$list="name1,name2,name3,name4,name5";
I think this should work:
PHP Code:$list=explode(',',$list);
foreach($list as $item){
$query="UPDATE table SET field='newdata' WHERE name=$item";
mysql_query($query);
}
-
05-09-2005, 12:57 AM #3Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
It's not working for some reason.
-
05-09-2005, 11:41 PM #4Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
Anyone else?
-
05-10-2005, 01:56 AM #5Junior Guru Wannabe
- Join Date
- Jun 2003
- Location
- California
- Posts
- 51
Are you getting any type of error and have you put in any code that connects to your database prior to intransits code?
-
05-10-2005, 02:00 AM #6Junior Guru Wannabe
- Join Date
- Oct 2004
- Posts
- 76
The only problem I see is that there should be quotes around $item in the query. What code are you using exactly?
-
05-10-2005, 03:49 AM #7Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
I got it, thanks a lot guys!
Last edited by iamdave; 05-10-2005 at 03:59 AM.
-
05-10-2005, 04:13 AM #8Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
One thing, how can I do it so that it checks for new line instead of a comma? And also how do I count how many were actually edited?
-
05-10-2005, 05:47 AM #9WHT Addict
- Join Date
- Sep 2002
- Location
- US
- Posts
- 133
$list=explode('\n',$list);
$i = 0;
foreach($list as $item){
$query="UPDATE table SET field='newdata' WHERE name=$item";
$i++;
mysql_query($query);
}
echo ("$i times");
-
05-11-2005, 03:44 AM #10Web Hosting Master
- Join Date
- Nov 2001
- Location
- California
- Posts
- 1,991
I already tried \n, it only works on the first one.
-
05-11-2005, 04:11 AM #11Web Hosting Master
- Join Date
- Jul 2003
- Location
- Kuwait
- Posts
- 5,104
If this list is coming from a file, you can just do $list = file("somefile.txt") -- otherwise you need $list = explode("\n",$list);
"\n" not '\n'