如何将字符串传递给mysql_query

时间:2013-12-22 23:41:31

标签: php mysql

我有一些PHP代码:

mysql_query("UPDATE people " .
"SET first_name = '$fname' , last_name = $lname , email = $email , " . 
"age = $age , cityids = $city , gender = $gender WHERE id = $Recedit" 
or die (mysql_error));

但是查询没有运行。我做错了什么?

4 个答案:

答案 0 :(得分:1)

您的MySQL查询格式不正确。在查询之后,or die应该是

mysql_query("UPDATE people SET first_name = '$fname' , last_name = $lname , email = $email , age = $age , cityids = $city , gender = $gender WHERE id = $Recedit") or die (mysql_error);

但是,为了便于阅读,我建议你做这样的事情:

$query = "UPDATE people SET"
       . " first_name = '" . $fname . "',"
       . " last_name = " . $lname . ","
       . " email = " . $email . ","
       . " age = " . $age . ","
       . " cityids = " . $city . ","
       . " gender = " . $gender
       . " WHERE id = " . $Recedit;

mysql_query($query) or die (mysql_error);

格式化查询以便它们是人类可读的,在调试逻辑和放大时始终可以帮助您完成日志运行。格式化错误。

答案 1 :(得分:0)

你必须从括号中取出“......或者死”的东西。现在你在字符串本身和“die”命令之间做了一个逻辑“或”。另外,mysql_error是一个函数,所以:

// WRONG
mysql_query("..............." or die(mysql_error))

// RIGHT
mysql_query("...............") or die(mysql_error())

也许还有别的东西,但这肯定是个错误。

答案 2 :(得分:0)

一旦你整理了其他错误/过时的内容,你的查询可能会是这样的......

UPDATE people 
   SET first_name = '$fname' 
     , last_name =  '$lname' 
     , email = '$email' 
     , age = $age 
     , cityids = $city 
     , gender = '$gender'
 WHERE id = $Recedit

答案 3 :(得分:0)

我建议你

1。编写最简单的代码。像这样:

$concat_query = "upate ...
$result = mysql_query($concat_query);
if ($result) {
   if ((mysql_num_rows($result) ) > 0) {
     ...

2. 使用Notepad ++查看不同颜色的代码片段。

3. 首先在数据库上执行dbcode,然后再编写代码。