MySQL查询由于保留关键字而失败? [英] MySQL query failing due to reserved keyword?

查看:78
本文介绍了MySQL查询由于保留关键字而失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您来自Google,那么此问题是由于int是PHP中的保留关键字引起的.请参见已接受答案的结尾.

If you're coming here from Google, this issue is a result of the word int being a reserved keyword in PHP. See the end of the accepted answer.

我仍在学习PHP/MySQL,对我一生来说,我不知道自己的代码出了什么问题.

I'm still learning PHP/MySQL and for the life of me I can't figure out what's wrong with my code.

我正在尝试从html页面中获取一些数据,并将其添加到数据库中的表中.我通过GET请求传递数据,然后通过PHP的$_GET检索数据.

I'm trying to take some data from an html page and add it to a table in my database. I'm passing the data with a GET request, then retrieving it with PHP's $_GET.

我已经对此进行了测试,并且变量已正确传递到PHP脚本,但它们未出现在数据库中.脚本在此行结束: mysql_query($query) or die('data entry failed');

I've tested this and the variables are passed correctly to the PHP script but they don't appear in the database. The script dies on this line: mysql_query($query) or die('data entry failed');

$database='a9293297_blog';
$con = mysql_connect('mysql2.000webhost.com','my_username','my_password');
mysql_select_db($database,$con) or die('failed to connect to database');

$username = $_GET['username'];
$password = $_GET['password'];
$charName = $_GET['charName'];
$sex = $_GET['sex'];
$class = $_GET['class'];
$race = $_GET['race'];
$str = $_GET['str'];
$sta = $_GET['sta'];
$dex = $_GET['dex'];
$int = $_GET['int'];
$cha = $_GET['cha'];

$query = "INSERT INTO Players (username, password, charName, sex, class, race, str, sta, dex, int, cha)
VALUES ('" . $username . "', '" . $password . "', '" . $charName . "', '" . $sex . "', '" . $class . "', '" . $race . "', '" . $str . "', '" . $sta ."', '" . $dex . "', '" . $int . "', '". $cha . "')";

mysql_query($query) or die('data entry failed'); // Fails here
mysql_close($con);

推荐答案

要更好地了解SQL查询的问题,请使用 mysql_error() :

To know better what's wrong with your SQL query, use mysql_error():

mysql_query($query) or die(mysql_error());

使用 mysql_real_escape_string( ) .示例:

Escape your string variables with mysql_real_escape_string(). Example:

$query = "INSERT INTO MYTABLE(MYFIELD) VALUES ('".mysql_real_escape_string($myVar)."');

编辑

int似乎是保留的MySQL关键字.用反引号将其转义:

int seems to be a reserved MySQL keyword. Escape it with backquotes:

INSERT INTO Players (username, password, ..., str, sta, dex, `int`, cha) ...

这篇关于MySQL查询由于保留关键字而失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆