简体   繁体   English

从php运行时,查询在phpmyadmin中工作正常,返回0行

[英]Query works fine in phpmyadmin returns 0 rows when ran from php

I'm really stumped by this. 我真的为此感到难过。 Here is the php: 这是PHP:

Update: I have escaped the inputs (before I did this a different way, but didn't know about mysql_real_escape_string). 更新:我已经转义了输入(在以另一种方式执行此操作之前,但不了解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(37)“ 1。新英格兰(东北地区)”
string(122) "SELECT * FROM possible_values WHERE table_id=3 AND col_name='DIVISION' AND value = '1 .New England (Northeast region)'" 字符串(122)“在可能的值中选择* FROM table_id = 3 AND col_name ='DIVISION'AND值='1。新英格兰(东北地区)'”
bool(false) 布尔值(false)

Now when I copy that query into the phpmyadmin SQL editor it does actually return a result. 现在,当我将该查询复制到phpmyadmin SQL编辑器中时,它实际上会返回结果。 I even tried using LIKE "%...%" as suggested in here but the same thing happens (phpmyadmin returns a row, php returns 0 rows). 我什至尝试按照此处的建议使用LIKE“%...%”,但发生了同样的事情(phpmyadmin返回一行,php返回0行)。

From the manual on rawurldecode() : rawurldecode()的手册中:

Note: rawurldecode() does not decode plus symbols ('+') into spaces. 注意:rawurldecode()不会将加号('+')解码为空格。 urldecode() does. urldecode()可以。

if you output your mySQL query, I bet your strting will look something like this: 如果您输出mySQL查询,我敢打赌,您的strstr看起来会像这样:

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

try urldecode() as the manual entry suggests. 尝试urldecode(),如手动输入所示。

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

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

Double quotes are not allowed for strings in SQL, as they are used for identifiers (like MySQL uses the backtick '`'). SQL中的字符串不允许使用双引号,因为双引号用于标识符(例如MySQL使用反引号“`”)。 I think MySQL allows them for strings with some settings, but you shouldn't use them. 我认为MySQL允许它们使用某些设置来输入字符串,但是您不应该使用它们。

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. 可能是您的问题是您正在使用旧的mysql扩展名,该扩展名可能不支持您的mysql版本。

Might be a typo, but 可能是拼写错误,但

table_id=$request

should have quotes around it: 应该有引号:

table_id='$request'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查询在PHP中返回空结果,但在phpmyadmin上工作正常 - Query returns empty result in PHP,but works fine on phpmyadmin SQL 查询在 PHP 中返回 NULL 但在 PHPMyAdmin 中工作正常 - SQL Query returns NULL in PHP but works fine in PHPMyAdmin MySQL查询从PHP代码返回null,但在终端中工作正常 - MySQL query returns null from PHP code but works fine in terminal 通过phpmyadmin测试时,SQL查询工作正常,但在PHP PDO中尝试相同操作时失败 - SQL query works fine when testing through phpmyadmin but fails when attempting the same thing in PHP PDO PHP mysqli 区分大小写的查询,但在 phpmyadmin 中运行时不区分大小写 - PHP mysqli case sensitive query but not when ran in phpmyadmin MySQL查询从phpMyAdmin工作并返回行,但是$ wpdb-&gt; get_results不返回任何内容 - MySQL query works from phpMyAdmin and returns rows, but $wpdb->get_results return nothing php中的mysql语法错误,输入phpmyadmin时工作正常 - mysql syntax errors in php, Works fine when input into phpmyadmin MySQL选择可在phpMyAdmin中使用,但我的PHP不会通过相同的调用返回任何行 - MySQL select works in phpMyAdmin but my PHP returns no rows with the same call 查询在 phpMyAdmin 中有效,但在 PHP 中无效 - Query works in phpMyAdmin but not PHP 查询在phpMyAdmin中有效,但在php中无效 - Query works in phpMyAdmin but not in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM