|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
mysql query problem
Let me explain you the whole set-up.
There are 2 tables which i need to work on for this query--- "proj_cont" and "contact". proj_cont is a table with only 2 fields pid,cid. which is used to associate projects with contacts. Now i want to delete all those associations for which the notes field in contact table is blank(null). so in effect it should delete all rows in proj_cont table whose cid = contactid(notes=blank). When i run this query it gives me the rows i need. SELECT contact.contactid FROM contact WHERE contact.notes = " " I am trying to run this query and it is giving me error 1064. i am new to mysql so can anybody help me with this query or give me another query which will work. $query= "SELECT contact.contactid FROM contact WHERE contact.notes = " " "; $getvalue = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($getvalue); for($i=0; $i < $num_rows; $i++) { $row = mysql_fetch_array($getvalue); DELETE * FROM proj_cont WHERE cid = "$row[i]"; } |
|
#2
|
||||
|
||||
|
Note that a blank field is different to a NULL field. If you want to search for NULL fields, then your query needs to look like:
"WHERE foo IS NULL" |
|
#3
|
|||
|
|||
|
I tried putting null, but Its still not working. Infact the echo statement gives me the number of rows correctly.
so i guess the problem is in for loop. can anybody help me with it. $query= "SELECT contact.contactid FROM contact WHERE contact.notes = ' ' "; $getvalue = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($getvalue); echo "$num_rows<br>" for($i=0; $i < $num_rows; $i++) { $row = mysql_fetch_array($getvalue); DELETE FROM proj_cont WHERE cid = "$row[i]"; } |
|
#4
|
|||
|
|||
|
is your delete statement in a variable? and then executed with mysql_query ?
|
|
#5
|
|||
|
|||
|
Quote:
as i have understood about $row = mysql_fetch_array($getvalue); $row[0] ---> the value of the first field/column retrieved $row[1] ---> the value of the second field/column retrieved .... $row[i] ----> the value of the n field/column retrieved since your query retrieved only 1 field, when it increments 'i' (from 0 to 1) $row[1] does not exist. you can try to use a while loop to do the fetching while ($row = mysql_fetch_array($getvalue)) { $sqlDelete = "DELETE FROM proj_cont WHERE cid = '$row[0]'"; $mysql_query($sqlDelete) or die (mysql_error()); } i may be wrong, i may be right. make things work in the test first before doing it in the real thing. good luck |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > mysql query problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|