sql 语句在 phpMyAdmin 中有效,但在 mysql_query 中无效

发布于 2024-11-28 11:37:21 字数 2133 浏览 4 评论 0原文

确实被某件事困住了。我正在尝试更新数据库,代码看起来是写的 - 如果我回显它并将其直接粘贴到 phpMyAdmin 中,它就可以完美工作 - 但代码本身不起作用......到目前为止我已经花了一天的时间尝试弄清楚为什么它不起作用,我完全没有想法......

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);
}

结果输出看起来像:

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

它似乎没有任何错误 - 但我只是无法更新它。如果它在 phpMyAdmin 中不起作用 - 我就知道 SQL 有问题 - 但它似乎是对的......我只是真的没有想法 - 任何帮助将不胜感激!


以下是带有一些格式的声明:

$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
        )
";

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
        )
";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染火枫林 2024-12-05 11:37:21
$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'] . '\")';

需要转义引号

$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

失而复得 2024-12-05 11:37:21

看起来引用错误,试试这个:

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

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'] . "')";
亽野灬性zι浪 2024-12-05 11:37:21

如果您的 SELECT 中有多个结果。
如果你这样做怎么办……

$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)';

注意 LIMIT 1

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

简单爱 2024-12-05 11:37:21

您确定第一个查询没有删除所有匹配的行吗?

我不明白第一个查询末尾的“user_data”部分。但我会在每个查询后检查受影响的行数,以查看查询是否对数据产生任何影响,如果是,它是否表现良好,或者只是存在一些逻辑错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文