简体   繁体   English

我的SQL查询有什么问题?

[英]What's wrong with my SQL query?

I have a bunch of database calls in my code. 我的代码中有一堆数据库调用。

One of them is like this: 其中之一是这样的:

mysql_query("INSERT INTO coupons (id, retailerName, description, savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, dateExp) VALUES ('$newID', '$retailerName', '$description', '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', '$dateAdded', '$dateExp')");

All of the other database calls works perfectly, but this one simply doesn't write any data onto the table. 所有其他数据库调用都可以正常运行,但是该数据库调用不会将任何数据写入表中。 What's wrong with it? 它出什么问题了?

Check the return from mysql query. 检查从mysql查询返回的信息。

$result = mysql_query($query);
if ($result === false) {
    // Prints the query and the mysql error to help you find the problem.
    die($query.'<br/>'.mysql_error());
}

If there is something wrong with the query it will tell you what it was. 如果查询有问题,它将告诉您它是什么。

mysql_query() returns true on success and false on error for queries that don't return something. 对于不返回内容的查询, mysql_query()成功返回true,错误返回false。

http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-query.php

When using mysql_query() in production, you should check the return value, and exit gracefully on error, and preferably log it so that it can help you fix any issues. 在生产环境中使用mysql_query()时,应检查返回值,并在错误时正常退出,最好将其记录下来,以便它可以帮助您解决任何问题。

Also when performing a query that uses user input ensure you use mysql_real_escape_string() to avoid SQL injection, and will protect input that contains a single quote, which would break your current query. 同样,在执行使用用户输入的查询时,请确保使用mysql_real_escape_string()避免SQL注入,并且将保护包含单引号的输入,这会破坏当前查询。

http://php.net/manual/en/function.mysql-real-escape-string.php http://php.net/manual/en/function.mysql-real-escape-string.php

Check the return value from the query to find out what is happening - 检查查询的返回值以了解发生了什么-

$result = mysql_query("INSERT INTO coupons (id, retailerName, description, 
                savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, 
                dateExp) VALUES ('$newID', '$retailerName', '$description', 
                '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', 
                 '$dateAdded', '$dateExp')");

if($result)
{
    // Your query succeeded
}
else
{
    // Your query failed due to some error
    die( mysql_error() );
}

You can just write this as mysql_query("INSERT INTO coupons (id, retailerName, description, savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, dateExp) VALUES ('$newID', '$retailerName', '$description', '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', '$dateAdded', '$dateExp')") or die(mysql_error()); 您可以将其写为mysql_query("INSERT INTO coupons (id, retailerName, description, savingsDetails, pictureURL, rating, qrPicture, zipCode, dateAdded, dateExp) VALUES ('$newID', '$retailerName', '$description', '$savingsDetails', '$pictureURL', '0', '$qrPicture', '$zipCode', '$dateAdded', '$dateExp')") or die(mysql_error());

If you don't get an error it means it succeeded. 如果没有收到错误,则表示成功。 If you get an error, you'll an error straight from mySQL (so it should be pretty detailed). 如果收到错误,则直接来自mySQL的错误(因此应该非常详细)。

Another way to locate your problem: logging your sql query, to see what the query looks like at run-time. 查找问题的另一种方法:记录sql查询,以查看查询在运行时的外观。

$str = "INSERT INTO coupons ...";

echo $str;

Then try that query to your database directly, you will get the things that doesn't right. 然后将查询直接尝试到您的数据库,您将得到不正确的信息。

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

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