douchu2823 2014-02-22 08:03
浏览 53
已采纳

mysql fetch数组继续返回false

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; 
    }

The fetch query keeps returning false although I have 3 items in my table. Some of the code obviously is testing to see if I got result back and those all work but the fetch array isn't executing the if statement because it thinks that it is false. Please help a few hours of frustration belong to this problem

  • 写回答

2条回答 默认 最新

  • douye9822 2014-02-22 08:07
    关注

    You have the wrong type of quotes in your query, they should be backticks:

    $query .= "WHERE `id` ={$subject_id} "; 
                     ^  ^
    

    Quotes make id a string, not a column name. Since the string id is never equal to $subject_id, the query doesn't return anything.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答