2015-02-11 61 views
0

SQL语法错误,PHP MYSQL

$sql = "SELECT post_title, post_body, post_author FROM forum_post WHERE post_id='".$pid."' forum_id='".$id."' AND post_type='o'"; 
 
if($topicPost = $mysql->prepare($sql)) { 
 
    $topicPost->bind_param('ss',$pid,$id); 
 
    $topicPost->bind_result($post_title, $post_body, $post_author); 
 
    $topicPost->execute(); 
 
    $topicPost->store_result(); 
 
} else { 
 
    echo "ErrorinSQLLL, ".$mysql->error; 
 
    exit(); 
 
}

因此,有我的SQL查询语句。

我得到这个印在我的网页:

ErrorinSQLLL,你有一个错误的SQL语法;检查对应于你的MySQL服务器版本使用附近的正确语法手册第1行

如果需要,我可以发布更多的我的代码“forum_id =” 1“”。

+3

你会错过的,在这里 “POST_ID = '$ PID' AND forum_id = '$ ID'” – jarlh 2015-02-11 10:40:57

+1

'WHERE POST_ID = '$ PID' forum_id = '$ id'',然后尝试绑定'$ pid'和'$ id'以及......但是在SQL中没有占位符将它们绑定在一起......看起来好像你对绑定有根本性的误解使你的SQL不安全的变量.....你的SQL应该是WHERE post_id =? AND forum_id =?' – 2015-02-11 10:43:00

回答

3

您的查询缺失AND,此处post_id='$pid' forum_id='$id'

+1

哦,我的.....谢谢。 – 2015-02-11 10:41:27

1

缺少and在条件

... WHERE post_id = " . (int)$pid . " AND forum_id = " . (int)$id . " ... 

ID是数字,因此不包括引号。

2

你错过了一个ANDpost_id键后:

"SELECT 
    post_title, 
    post_body, 
    post_author 
FROM 
    forum_post 
WHERE 
    post_id = " . $pid . " 
AND 
    forum_id= " . $id . " 
AND 
    post_type = 'o'";