views:

63

answers:

3

I have this php script:

foreach (get_all_topics () as $topic_id => $topic_info) {
   $result = mysql_query("DELETE FROM marks 
                           WHERE user_id = $user_id 
                            AND topic_id = $topic_id", $db);
    echo "DELETE FROM marks 
           WHERE user_id = $user_id 
             AND topic_id = $topic_id : " . mysql_error() . " : " . mysql_affected_rows();
}

I have set a valid $user_id and get_all_topics() gives valid topic info as printed by echo. However it always returns false and I have a data with user_id = 1 AND topic_id = 1 (and it also gets deleted if I copy and paste echoed string in interactive mysql prompt - so no spelling mistakes!).

NOTE: Other functions that Insert or Update the table works just fine. I'm learning php - mysql and this is the first time I'm deleting something from database. Can anyone please suggest what is the issue with this script? Or can there be any permissions problem with this?

Regards, Mihir Gokani

EDIT: I've changed the code above to

$all_topics = get_all_topics();

foreach ($all_topics as $topic_id => $topic_info) {
   $result = mysql_query("DELETE FROM marks 
                           WHERE user_id = $user_id 
                            AND topic_id = $topic_id", $db);
    echo "DELETE FROM marks 
           WHERE user_id = $user_id 
             AND topic_id = $topic_id : " . mysql_error() . " : " . mysql_affected_rows();
}

and it worked! Anyone having similar problem can try assigning return value to some temp variable and then use that in the foreach loop. Don't know why it works :)

Thanks, Mihir Gokani

A: 

Try putting single quotes around $user_id and $topic_id so your code will be

mysql_query( "DELETE FROM marks WHERE user_id='$user_id' AND topic_id='$topic_id'", $db );
danderson
Thanks for replying. The `user_id` and `topic_id` both are integers. Moreover, if I copy and paste the echoed output something like : `DELETE FROM marks WHERE user_id=1 AND topic_id=1` it works perfectly fine. But still, i'll try ur solution. Thanks.
Mihir Gokani
Just tried that, didn't work :(
Mihir Gokani
A: 

I like to build a query string:

$myQuery = "DELETE FROM marks WHERE user_id= " . $user_id . "AND topic_id=" . $topic_id;
mysql_query($myQuery);
cinqoTimo
Thanks for a quick reply. But I've already tried that (with a space between `"` and `AND`) But no luck :(
Mihir Gokani
+1  A: 
  1. Delete all these rows once using in (id1, id2, id3)
  2. You have not specified database in query (may be you have selected database before) - use databasename.tablename.
  3. It depend's what your mysql_error returned since false means that there is an error.
  4. May be errors somehow suspended so look in error logs.

PHP Mysql Query Manual page

Aram
Thanks for replying. Yes, I've selected the database earlier in the code and other queries like SELECT and UPDATE works just fine. In `echo`, both `mysql_error()` and `mysql_affected_rows()` doesn't return anything. Also the loop executes only once. Really frustrated! Will try to delete required rows in one shot using `in (id1, id2, ...)` but i thing there's no reason why they can't be deleted separately.
Mihir Gokani
Please, give me sql query exactly. I'll try to help that way.
Aram
For debugging purposes try to use var_dump() since it's also prints type of variable which is very helpful.
Aram
Deleting using one statement will save you resources. Separate deletes will instantiate separate processes.
Aram