使用php $变量的mysql搜索查询语法

时间:2014-06-03 02:20:59

标签: php mysql

我的MYSQL查询的$ message部分出现问题。

$sql = 'SELECT message_time FROM ' . MCHAT_TABLE . ' 
        WHERE user_id = ' . (int) $user->data['user_id'] . ', message = '$message'
        ORDER BY message_time DESC'; Any help with the syntax would be appreciated. If I remove , message = '$message' the query returns no errors.


    // Flood control

$message = 'I have opened Video Chat. <strong>(Automated Message)</strong> ';
$mchat_flood_current_time = time(); 
echo $mchat_flood_current_time;
echo " ";
echo $user->data['user_id'];

$sql = 'SELECT message_time FROM ' . MCHAT_TABLE . ' 
        WHERE user_id = ' . (int) $user->data['user_id'] . ', message = '$message'
        ORDER BY message_time DESC';
$result = $db->sql_query_limit($sql, 1);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);
echo " ";
echo $row['message_time'];
echo " ";
echo ($mchat_flood_current_time - $row['message_time']);
if($row['message_time'] >= 0 && ($mchat_flood_current_time - $row['message_time']) > 30)
{

1 个答案:

答案 0 :(得分:0)

将消息变量换成引号并在&#34; message =&#34;之前替换逗号;使用AND,因此您的查询如下所示:

$sql = "SELECT message_time FROM " . MCHAT_TABLE . " 
WHERE user_id = " . (int) $user->data['user_id'] . " 
AND message = '" . $message . "' 
ORDER BY message_time DESC";