dryb38654 2017-04-23 04:57
浏览 13
已采纳

如何使用PHP从mysql中正确检索数据?

I am getting this error Fatal error: Call to a member function RecordCount() on boolean on this line if($results->RecordCount()>0) when i am trying to retrieve data from MYSQL and display into a table. I try a lot of solution but it still getting the error. Below is my php code

    if (!$conn) 
    {
       die("Failed to connect to database : " . $dsn);
    }
    else 
    {
        //database connection ok
        ADOdb_Active_Record::SetDatabaseAdapter($conn);
        if($category=='4')
        {
            $query= $conn->prepare("SELECT * FROM profiles");
            $results=$conn->Execute($query); 
        } 
        else if ($category=='3')
        {
            $query= $conn->prepare("SELECT * FROM profiles WHERE biography LIKE ?");
            $results=$conn->Execute($query); 
        }
        else if ($category=='2')
        {
            $query= $conn->prepare("SELECT * FROM profiles WHERE ranks = ?");
            $results=$conn->Execute($query); 
        }
        else if ($category=='1')
        {
            $query= $conn->prepare("SELECT * FROM profiles WHERE nationality LIKE ?");
            $results=$conn->Execute($query); 
        }
        else
        {

        }

        if($results->RecordCount()>0)
        {
            while(!$results->EOF)
            {
                //if($results->RecordCount()>0)
                //{
                echo "<table border='1' cellpadding='5'>"
                . "<tr><th>Name</th><th>Nationality</th><th>Current Score</th><th>Current Standings</th><th>Event Participate</th><th>Team</th><th>Bike</th><th>Picture</th>"; 
                echo "<tr><td>".$results->fields[1]."</td><td>".$results->fields[2]."</td><td>".$results->fields[3]."</td> <td>".$results->fields[4]."</td> 
                <td>".$results->fields[5]."</td> <td>".$results->fields[6]."</td> <td>".$results->fields[7]."</td> <td>><img src='loadimage.php?id=".$results->fields[0]."' height='100' /></td></tr>";
                echo "</table><br/>";
                $results->MoveNext();
                 }
        }
        else
        {
           echo "There is no match record. <br/><br/>";
        }
    }
        $results->close();
    }
  • 写回答

1条回答 默认 最新

  • duanlu9816 2017-04-23 05:13
    关注

    Your error is caused by $results being false. That means that the query failed and it's probably because you don't seem to be providing the parameters to replace the "?" In your queries.

    You can check your db errors using

    $conn->ErrorMsg()
    

    When $results===false

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题