简体   繁体   English

查询适用于sql,而不适用于php

[英]query works in sql, not in php

I have this query I can run against my db and it works fine. 我有这个查询,我可以对我的数据库运行,它工作正常。 However when I try it in the PHP version I get 0 results. 但是,当我在PHP版本中尝试时,得到0个结果。 I'm missing something fundamental, I just can't tell what it is. 我缺少基本的东西,我只是无法分辨它是什么。

Query 询问

SELECT * 
FROM table_admin_20
WHERE column1 =  '0607'

PHP 的PHP

$store_info_query = "SELECT * FROM '".$table_name."' WHERE 'column1' = '".$store_number."'";

if ($query_run = mysql_query($store_info_query)) {
    if (mysql_num_rows($query_run) == NULL) {
        $response["success"] = 0;          
        echo json_encode($response);
        echo 'nope';
    } else {
        while ($row = mysql_fetch_assoc($query_run)) {
            $store_info = $row['column1'];
            echo '1111111';
            echo $store_info;           
        }       
    } 
} else {
    echo 'fail';
    }

I know I have 0 protection against SQL injection, I'm merely trying to pull data, this is in no way live yet. 我知道我对SQL注入有0保护,我只是想拉数据,这还没有实现。 Anyways, I get the 'fail' response each time. 无论如何,我每次都会收到“失败”响应。 Thanks for any help. 谢谢你的帮助。

Don't add security as an afterthought, just switch to PDO or mysqli and use prepared statements so that you don't have to worry about the values any more. 事后不要添加安全性,只需切换到PDO或mysqli并使用准备好的语句,这样您就不必再担心这些值了。 In case of table- or column names, you would need to use white-lists though. 对于表名或列名,您将需要使用白名单。

The cause of your problem is that you are quoting your table- and field names. 问题的原因是您引用了表名和字段名。 If you need to escape them, use backticks: 如果需要转义,请使用反引号:

$store_info_query = "SELECT * FROM `".$table_name."` WHERE `column1` = '".$store_number."'";

You've to replace ' with ` for the table and column names. 您必须将表名和列名的`替换为`。 ' is just for values. '仅用于价值。 Try this: 尝试这个:

$store_info_query = "SELECT * FROM `".$table_name."` WHERE `column1` = '".$store_number."'";

Please avoid using * and rethink your security-strategies. 请避免使用*并重新考虑您的安全策略。 As already mentioned, take a look at PDO: http://php.net/manual/en/book.pdo.php 如前所述,请看一下PDO: http : //php.net/manual/zh/book.pdo.php

You are putting wrong quotes around table name and column name. 您在表名和列名两边加上了错误的引号。 Try this 尝试这个

$store_info_query = "SELECT * FROM `".$table_name."` WHERE `column1` = '".$store_number."'";

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM