简体   繁体   English

在php中执行我的查询时出错,但在phpmyadmin中却没有

[英]error executing my query in php but not in phpmyadmin

I'm trying to execute a query in my cron job of drupal7. 我正在尝试在drupal7的cron作业中执行查询。 However something strange is happening. 但是,正在发生一些奇怪的事情。 Every time it tries to execute I get an PDOException. 每当它尝试执行时,我都会收到一个PDOException。 When I paste the query in phpmyadmin there is no problem and the query executes. 当我将查询粘贴到phpmyadmin中时,没有问题,查询将执行。 But iin my cronjob it gives the error. 但是,在我的cronjob中,它给出了错误。 The problem is not in my cronjob, I know this because it also executes other queries without any problems. 问题不在我的cronjob中,我知道这一点,因为它也可以执行其他查询而没有任何问题。

The php code of the query: 查询的php代码:

$sql_insert_product = 'INSERT INTO tblProducten(productnummer, merk, doelgroep, RefLev)'
. 'VALUES(' . $prod->productnummer . ', "tt", "' . $prod->doelgroep . '", "'
. $prod->reflev . '")';
$db_catalogus->query($sql_insert_product);

The resulted query the code produces which works in phpmyadmin: 代码生成的结果查询可在phpmyadmin中运行:

INSERT INTO tblProducten(productnummer, merk, doelgroep, RefLev) VALUES(16657, "tt", "Meisjes", "11803")

The Exception: 例外:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tt' in 'field list': INSERT INTO tblProducten(productnummer, merk, doelgroep, RefLev) VALUES(16657, "tt", "Meisjes", "11803"); PDOException:SQLSTATE [42S22]:找不到列:1054“字段列表”中的未知列“ tt”:INSERT INTO tblProducten(productnummer,merk,doelgroep,RefLev)VALUES(16657,“ tt”,“ Meisjes”,“ 11803” ); Array ( ) in cronner_cron() (line 94 of /home/... cronner_cron()中的数组()(/ home / ...的第94行

the problem is indeed in your query. 问题确实出在您的查询中。 There is a setting in mysl which makes double quote a field delimiter, instead of default apostrophe. mysl中有一个设置,使双引号成为字段定界符,而不是默认的撇号。

So, change your double quotes with single one (and modify the syntax accordingly) 因此,请用单引号将双引号更改(并相应地修改语法)

$sql = "INSERT INTO tblProducten(productnummer, merk, doelgroep, RefLev)
VALUES ({$prod->productnummer}, 'tt', '{$prod->doelgroep}','{$prod->reflev}')";
$db_catalogus->query($sql);

and, to let you know, your formatting is almost unreadable, with all that unnecessary concatenations and quotes. 并且,要让您知道,格式几乎是不可读的,带有所有不必要的串联和引号。

if you have problems with quotes, than you don't escape your values 如果您在报价方面遇到问题,那您将无法逃避自己的价值观
it will lead you to SQL injection. 它将导致您进行SQL注入。
ALWAYS escape any string you are placing in the query. 总是转义您在查询中放置的任何字符串。
and format other values accordingly. 并相应地格式化其他值。

add this code before your query 在查询之前添加此代码

$prod->productnummer = intval($prod->productnummer);
$prod->doelgroep = mysql_real_escape_string($prod->doelgroep);
$prod->reflev = mysql_real_escape_string($prod->reflev);

and there won't be any problem with any quotes ever. 而且任何报价都不会有任何问题。

Apparently, PHPMyAdmin and your cron script run in different SQL modes. 显然,PHPMyAdmin和您的cron脚本在不同的SQL模式下运行。 Here is an illustration of the problem: 这是问题的说明:

SET sql_mode = 'ANSI_QUOTES'; # treats double quotes as an identifier quote character 
SELECT "name" FROM table1;

name
=====
alpha
beta
gamma

SET sql_mode = ''; # treats double quotes as string literal quote character
SELECT "name" FROM table1;

name
=====
name
name
name

Ideally you should not use " as string delimiter ( "tt" in your example), use single quotes instead which is a standard every SQL database will understand. Or you can play around with ANSI_QUOTES setting which I would not recommend. 理想情况下,您不应该使用"作为字符串定界符(在示例中为"tt" ),而应使用单引号,这是每个SQL数据库都会理解的标准。或者您可以尝试使用我不建议使用的ANSI_QUOTES设置。

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

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