需要关注mysql查询语句

时间:2011-07-15 21:49:20

标签: php mysql sql

我是新手,这句话有什么问题:

$sql=" SELECT * FROM `calendar` WHERE `DayId` ='".$day."'";
$result = mysql_query($sql, $conn);


if (!$result){
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_array($result)) {             //set $dayType
    $dayType = $row[DayType];       
}

我一直收到错误:

DB Error, could not query the database 
MySQL Error: 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

但是当我输入“echo $ result”时在以$ result = ...开头的行之后,我得到“资源ID#2”的$ result的值

4 个答案:

答案 0 :(得分:1)

您需要将“day”变量括在引号中(如果您还没有,则应该将其转义!)

$sql = "SELECT * FROM calendar WHERE DayId = '" . mysql_real_escape_string($day) . "'";

答案 1 :(得分:1)

不应该是

$sql="SELECT * FROM `calendar` WHERE `DayId` = '".$day."'"; 

答案 2 :(得分:0)

我觉得你的$ day变量没有被填充...尝试在运行之前回显SQL语句,以确保一切看起来都应该......

答案 3 :(得分:0)

如果是日期(z),请将其更改为日期('z')。