从 php 运行时,查询在 phpmyadmin 中运行良好,返回 0 行

发布于 2024-08-21 00:42:52 字数 1359 浏览 8 评论 0 原文

我真的被这个难住了。这是 php:

更新: 我已经转义了输入(在我以不同的方式执行此操作之前,但不知道 mysql_real_escape_string)。另外,我用单引号替换了双引号,但仍然出现同样的问题。以下是更新后的代码:

$request = mysql_real_escape_string($_POST['id']);
$colName = mysql_real_escape_string($_POST['col_name']);

function executeAssoc1($q)
{
    $r = mysql_query($q) or die(mysql_error() . ' ' . $q);
    $rass = mysql_fetch_assoc($r);
    return $rass;
}

foreach(array_keys($_POST) as $pitem)
{

    if($pitem == (...what I want it to...))
    {
        $pitem_name = mysql_real_escape_string(rawurldecode($pitem));

        $qf = "SELECT * FROM possible_values WHERE table_id=$request AND col_name='$colName' AND value = '$pitem_name'";
        $qfr = executeAssoc1($qf);
        var_dump($pitem_name);
        echo '<br>';
        var_dump($qf);
        echo '<br>';
        var_dump($qfr);
    }

}

以下是该代码在一个循环期间输出的部分内容:

string(37) "1 .新英格兰(东北地区)"
string(122) "SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .新英格兰(东北地区)'"
布尔(假)

现在,当我将该查询复制到 phpmyadmin SQL 编辑器时,它实际上会返回结果。我什至尝试按照 此处 但发生了同样的事情(phpmyadmin 返回一行,php 返回 0 行)。

I'm really stumped by this. Here is the php:

Update: I have escaped the inputs (before I did this a different way, but didn't know about mysql_real_escape_string). Also, I replace the double quotes with single quotes, yet the same problem is happening. Below is the updated code:

$request = mysql_real_escape_string($_POST['id']);
$colName = mysql_real_escape_string($_POST['col_name']);

function executeAssoc1($q)
{
    $r = mysql_query($q) or die(mysql_error() . ' ' . $q);
    $rass = mysql_fetch_assoc($r);
    return $rass;
}

foreach(array_keys($_POST) as $pitem)
{

    if($pitem == (...what I want it to...))
    {
        $pitem_name = mysql_real_escape_string(rawurldecode($pitem));

        $qf = "SELECT * FROM possible_values WHERE table_id=$request AND col_name='$colName' AND value = '$pitem_name'";
        $qfr = executeAssoc1($qf);
        var_dump($pitem_name);
        echo '<br>';
        var_dump($qf);
        echo '<br>';
        var_dump($qfr);
    }

}

Here is part of what that code outputs during one loop:

string(37) "1 .New England (Northeast region)"
string(122) "SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .New England (Northeast region)'"
bool(false)

Now when I copy that query into the phpmyadmin SQL editor it does actually return a result. I even tried using LIKE "%...%" as suggested in here but the same thing happens (phpmyadmin returns a row, php returns 0 rows).

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

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

发布评论

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

评论(4

逆光飞翔i 2024-08-28 00:42:52

rawurldecode() 的手册中:

注意:rawurldecode() 不会将加号 ('+') 解码为空格。 urldecode() 可以。

如果您输出 mySQL 查询,我敢打赌您的字符串将如下

1+.New+England+(Northeast+region)

所示:按照手动输入的建议尝试 urldecode() 。

From the manual on rawurldecode():

Note: rawurldecode() does not decode plus symbols ('+') into spaces. urldecode() does.

if you output your mySQL query, I bet your strting will look something like this:

1+.New+England+(Northeast+region)

try urldecode() as the manual entry suggests.

℡寂寞咖啡 2024-08-28 00:42:52

你应该使用单引号而不是双引号,即:

SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .New England (Northeast region)'

you should use single quotes instead of double quotes, ie:

SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .New England (Northeast region)'
浅暮の光 2024-08-28 00:42:52

SQL 中的字符串不允许使用双引号,因为它们用于标识符(如 MySQL 使用反引号“`”)。我认为 MySQL 允许它们用于具有某些设置的字符串,但您不应该使用它们。

从技术上讲,这应该返回一个错误,所以我认为这不是你的问题。

你的问题可能是你使用的是旧的 mysql 扩展,它可能不支持你的 mysql 版本。

Double quotes are not allowed for strings in SQL, as they are used for identifiers (like MySQL uses the backtick '`'). I think MySQL allows them for strings with some settings, but you shouldn't use them.

Technically that should return an error, so I don't think that's your problem.

What could be your problem is that you're using the old mysql extension, which may not support your mysql version.

◇流星雨 2024-08-28 00:42:52

可能是拼写错误,但

table_id=$request

应该有引号:

table_id='$request'

Might be a typo, but

table_id=$request

should have quotes around it:

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