sql语句在phpMyAdmin中工作,但在mysql_query中不起作用

Really stuck on something. I'm trying to update a database and the code looks write - and if I echo it out and paste it directly into phpMyAdmin it works perfectly - but the code itself doesn't work... I have spend a day so far trying to figure out why it's not working and I'm completely out of ideas...

function restoreSession() 
{

mysql_connect("theHost", "root", "rootPWD") or die(mysql_error());
mysql_select_db("myDatabase") or die(mysql_error());    

$restore_cmd = 'UPDATE wp_dor_cart66_sessions SET user_data = (SELECT user_data FROM wp_dor_cart66_stored_sessions WHERE ip_address = "' . $_SERVER['REMOTE_ADDR'] . '")';

$clean_up = "DELETE FROM `wp_dor_cart66_sessions` WHERE `ip_address` = \"" . $_SERVER['REMOTE_ADDR'] . "\" AND id NOT IN (SELECT id FROM ( SELECT id FROM `wp_dor_cart66_sessions` ORDER BY id DESC LIMIT 1 ) user_data )";

mysql_query($clean_up) or die('Query failed: ' . mysql_error()); 
$result = mysql_query($restore_cmd) or die('Query failed: ' . mysql_error()); 
echo "<br/>";
echo $restore_cmd;
echo "<br/>";
var_dump($result);
echo "<br/>";
print_r($result);
}

The resulting output looks like:

UPDATE wp_dor_cart66_sessions SET user_data = 
(SELECT user_data FROM   wp_dor_cart66_stored_sessions 
WHERE ip_address = "196.54.110.24");

bool(true)

1

It doesn't appear to have any errors - but I just can't get it to update. If it didn't work in phpMyAdmin - I'd know there was something wrong with the SQL - but it seems right... I'm just really out of ideas - any help would be greatly appreciated!


Here are the statements again with some formatting:

$restore_cmd = '
    UPDATE
        wp_dor_cart66_sessions
    SET
        user_data = (
            SELECT
                user_data
            FROM
                wp_dor_cart66_stored_sessions
            WHERE
                ip_address = "' . $_SERVER['REMOTE_ADDR'] . '"
        )
';

$clean_up = "
    DELETE FROM
        `wp_dor_cart66_sessions`
    WHERE
        `ip_address` = \"" . $_SERVER['REMOTE_ADDR'] . "\"
        AND id NOT IN (
            SELECT
                id
            FROM
                (
                    SELECT
                        id
                    FROM
                        `wp_dor_cart66_sessions`
                    ORDER BY
                        id DESC
                    LIMIT
                        1
                ) user_data
        )
";
$restore_cmd = 'UPDATE wp_dor_cart66_sessions SET user_data = (SELECT user_data FROM wp_dor_cart66_stored_sessions WHERE ip_address = \"' . $_SERVER['REMOTE_ADDR'] . '\")';

need to escape the quotation marks

Looks like quoting error, Try this:

"UPDATE wp_dor_cart66_sessions SET user_data = (SELECT user_data FROM wp_dor_cart66_stored_sessions WHERE ip_address = '" . $_SERVER['REMOTE_ADDR'] . "')";

If could be that you have multiple results in your SELECT. What if you do ...

$restore_cmd = 'UPDATE wp_dor_cart66_sessions SET user_data = (SELECT user_data FROM wp_dor_cart66_stored_sessions WHERE ip_address = "' . $_SERVER['REMOTE_ADDR'] . '" LIMIT 1)';

... note the LIMIT 1

Are you sure that the first query is not deleting all the matching rows?

I don't understand the "user_data" part at the end of the first query. But I would check the number of affected rows after each query to see if query is doing any affect on data and if it is, is it doing well or there's just some logical mistake.