dtcd27183 2011-08-09 06:31
浏览 72

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
        )
";
  • 写回答

4条回答 默认 最新

  • dougan7657 2011-08-09 06:34
    关注
    $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

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog