function get_subject_by_id($subject_id){
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE 'id' ={$subject_id} ";
$query .= "LIMIT 1 ";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// if no rows are returned fetch_array will return false
if(!is_null($result_set)){
echo "got here";
echo $result_set;
}
if($subject=mysql_fetch_array($result_set)){ // not working properly
echo "testing fetch array";
return $subject;
}else {
return NULL;
}
虽然我的表中有3个项目,但fetch查询仍会返回false。一些代码显然正在测试是否我得到了结果并且这些都工作但是fetch数组没有执行if语句,因为它认为它是假的。请帮助几个小时的挫折属于这个问题
答案 0 :(得分:3)
您的查询中的引号类型错误,它们应该是反引号:
$query .= "WHERE `id` ={$subject_id} ";
^ ^
引号使id
成为字符串,而不是列名。由于字符串id
永远不等于$subject_id
,因此查询不会返回任何内容。
答案 1 :(得分:1)
我认为这是因为id列用单引号括起来而不是后面的刻度
$query .= "WHERE 'id' ={$subject_id} ";
应该是
$query .= "WHERE id ={$subject_id} ";
或者
$query .= "WHERE `id` ={$subject_id} ";