繁体   English   中英

如何在mysql查询中编写字符串变量?

[英]How to write a string variable inside mysql query?

我想实现此查询:

if(x=1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set $update where id=100";
mysql_query($query);

但我收到错误消息,Mysql无法执行查询?

   <?php
    if($x==1){
    $update = "close = '".$date."'";
    }
    else {
    $update = "open = '".$date."'";
    }
    $query = "update table1 set $update where id=100";
    mysql_query($query);
    ?>

将您的查询放在引号中。 尝试以下操作:

if(x==1){
  $update = "close='".$date."'";
}
else {
  $update = "open = '".$date."'";
}
$query = "Update table1 set ".$update." where id=100";
mysql_query($query) or die(mysql_error());

放mysql_error()来检查你从mysql得到了什么错误

用这个

<?php


if(x==1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query);

?>

尝试使用此代码........

<?php
if($x==1){ // use $ for variables
    $update = "close = '".$date."' "; //always concatenate variables
}
else {
    $update = "open = '".$date."' ";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query, $connection); // don`t forget to add mysql connection 

?>
Replace 
$query = Update table1 set $update where id=100;

to
$query = "Update table1 set ".$update." where id=100";

暂无
暂无

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

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