sql查询执行出现问题

发布于 2024-09-04 05:15:39 字数 432 浏览 9 评论 0原文

我在执行 sql 查询时遇到问题。我正在使用此 sql 查询:

$userid = 1;  

$sql = mysql_query("
  SELECT ID, Nm, Address, date_format(DateOfBirth, '%d%M%Y') as DateOfBirth 
  FROM PersonalDetails where UserMasterID = $userid
") or die (mysql_error());

结果显示为:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '= 附近使用的正确语法 ' 在第 1 行当

我在 PHPMyAdmin 中执行此操作时,它可以正常工作。 我正在使用 mysql(5.0.5b) 和 PHP (5.2.6)

你能帮我吗?

I have a problem in sql query execution.I am using this sql query:

$userid = 1;  

$sql = mysql_query("
  SELECT ID, Nm, Address, date_format(DateOfBirth, '%d%M%Y') as DateOfBirth 
  FROM PersonalDetails where UserMasterID = $userid
") or die (mysql_error());

The result appears as:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=
' at line 1

When I execute this in PHPMyAdmin it works properly.
I am using mysql(5.0.5b) and PHP (5.2.6)

Can you help me please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤檠 2024-09-11 05:15:39

如果 UserMasterID 不是整数,您可能需要在值两边加引号:

PersonalDetails where UserMasterID = '$userid'"

您上面引用的查询与您在 phpMyAdmin 中运行的查询相同。它包含一个 PHP 变量。
当遇到 SQL 问题时,始终输出并分析解析查询(其中不引用 PHP 变量)。

$query = "select ID... etc. etc.";
$result = mysql_query($query);

if (!$result) 
 echo "Error in query $query: ".mysql_error();

90%的问题都可以通过这种方式发现并解决。

If UserMasterID is not an integer, you may need to put quotes around the value:

PersonalDetails where UserMasterID = '$userid'"

The query you are quoting above is not identical to what you run in phpMyAdmin. It contains a PHP variable.
When in SQL trouble, always output and analyze the parsed query (with no references to PHP variables in them).

$query = "select ID... etc. etc.";
$result = mysql_query($query);

if (!$result) 
 echo "Error in query $query: ".mysql_error();

90% of problems can be spotted and solved that way.

也只是曾经 2024-09-11 05:15:39

如果它在 PHPMyAdmin 中正确运行,但在 PHP 代码中运行不正确,那么对我来说,这表明 PHPMyAdmin 正在执行其著名的任务,即转义和清理它可能执行的所有操作。

将您的代码更改为此并检查它。

$userid = 1;  

$sql = mysql_query("
  SELECT `ID`, `Nm`, `Address`, date_format(`DateOfBirth`, '%d%M%Y') as DateOfBirth 
  FROM `PersonalDetails` where `UserMasterID` = '{$userid}'
") or die (mysql_error());

它现在应该运行了。

If it runs correctly in PHPMyAdmin, but not in the PHP code, then that says to me that PHPMyAdmin is performing it's famous task of escaping and sanitizing everything it possibly can.

Change your code to this and check it.

$userid = 1;  

$sql = mysql_query("
  SELECT `ID`, `Nm`, `Address`, date_format(`DateOfBirth`, '%d%M%Y') as DateOfBirth 
  FROM `PersonalDetails` where `UserMasterID` = '{$userid}'
") or die (mysql_error());

It should run now.

甩你一脸翔 2024-09-11 05:15:39

呃——你为什么不串联起来呢?

"SELECT `ID`, `Nm`, `Address`, date_format(`DateOfBirth`, '%d%M%Y') as DateOfBirth 
FROM `PersonalDetails` where `UserMasterID` = '" . $userid . "'";

但约瑟夫是当场...

Ehhh - why don't you concatenate ?

"SELECT `ID`, `Nm`, `Address`, date_format(`DateOfBirth`, '%d%M%Y') as DateOfBirth 
FROM `PersonalDetails` where `UserMasterID` = '" . $userid . "'";

but Joseph is spot on ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文