Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/SQL-查询未显示所有数据_Php_Mysql_Sql - Fatal编程技术网

PHP/SQL-查询未显示所有数据

PHP/SQL-查询未显示所有数据,php,mysql,sql,Php,Mysql,Sql,代码如下: <?php if(isset($_POST['results']) && $_POST['results'] != -1) { $db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', ''); echo "<table border='1'> <tr&g

代码如下:

            <?php 
            if(isset($_POST['results']) && $_POST['results'] != -1) {
            $db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', '');

            echo "<table border='1'>
            <tr>
            <th>Courses</th>
            </tr>";

            $stmt = $db->prepare("SELECT title FROM course WHERE `subject_id`=?");
            $stmt->execute(array($_POST['results']));

            if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

                     echo "<tr>";
                     echo "<td>", $row['title'], "</td>";
                     echo "</tr>";

                     }

            } else {
                    echo "No results found";
                } echo "</table>"; 
            }

            ?>

删除if,因为它需要更改数组的指针(fetch)

您可以更改为:

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_rows = $stmt->rowCount();

if($total_rows > 0){

   foreach($row as $item){
      echo '<td>'. $item['title'] .'</td>';
   }
}else{
  echo 'no results';
}
$row=$stmt->fetchAll(PDO::FETCH_ASSOC);
$total_rows=$stmt->rowCount();
如果($total_rows>0){
foreach($行作为$项){
回显“.$item['title']”;
}
}否则{
回应“没有结果”;
}

但我希望在没有results@tom,因此使用fetchAll()和count()获取数组的行号<代码>$rows=$stmt->fetchAll(PDO::FETCH_ASSOC)$计数=计数($rows);如果($count>0){//some}或者{echo'empty results';}
我更改了答案,请再次查看。这是有效的,因为表正在混乱。其他结果向右移动(不在表中),而不是在表中格式不足。@Tom,行echo“$row['title]”,”;。我不怎么使用PHP,但是你能在那里用逗号吗?
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_rows = $stmt->rowCount();

if($total_rows > 0){

   foreach($row as $item){
      echo '<td>'. $item['title'] .'</td>';
   }
}else{
  echo 'no results';
}