使用PDO语句选择表数据 [英] Selecting table data with PDO statements

查看:54
本文介绍了使用PDO语句选择表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过mysql_选择数据的php脚本,但是最近我一直在阅读PDO是必经之路,而mysql_则已被贬值.现在,我将该脚本转换为PDO.

I have a php script that selects data via mysql_, however recently I have been reading that PDO is the way to go and that mysql_ is becoming depreciated. Now I am converting that script to PDO.

但是我的问题是,我没有使用$ _POST进行选择.我只想选择包含所有数据的整个表,所以我输入以下查询:

My question is though, I am not using $_POST to select. I just want to select the entire table with all of its data so I enter this query :

$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchall(); // or you can just $result = $query as hakre proposed!

因此,就像我处理旧的折旧mysql_版本的脚本一样,我使用echo来回显带有数据的表.

so then like I did with my old depreciated mysql_ version of the script I used the echo to echo a table with the data in it.

    echo 
    "<table border='2'>
    <tr>
    <th>ID</th>
    <th>A Number</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Why</th>
    <th>Comments</th>
    <th>Signintime</th>
    </tr>"
    ;

    foreach($result as $row)
    {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td><a href=Student.php?studentA_num=" . $row['anum'] . ">" .$row['anum'] . " </a></td>";
  echo "<td>" . $row['first'] . "</td>";
  echo "<td>" . $row['last'] . "</td>";
  echo "<td>" . $row['why'] . "</td>";  
  echo "<td>" . $row['comments'] . "</td>";
  echo "<td>" . $row['signintime'] . "</td>";
  echo "<td> <input type=\"button\" value=\"Start Session\"onClick=\accept.php?id=" . $row['id'] . "&start=true></td>";
}

  echo "</tr>";
  echo "</table>";

现在使用它,我无法将单个输出输出到我的表中.

now using this, I can not get a single output to my table.

我的问题是我的选择语句中是否缺少某些内容?还是我不提取任何行?另外,我在init.php所需的另一个脚本connect.php中设置了连接设置(在我所有页面的顶部)

My question is am I missing something from my select statements? Or am I not fetching any rows? Also I the connection settings set in another script called connect.php that is required by init.php (at the top of all of my pages)

1

编辑了代码,使其现在可以正常工作,还添加了图片以向其他人展示它的外观!希望有人可以将其用于某种用途!

Edited the code so it now works, also adding a picture to show others how it should look! Hopefully some one can put this to some sort of use!

推荐答案

您实际上做得太多:

$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $dbh->query($query);

有问题的行是:

$result = $dbh->query($query);

使用 http://php.net/pdo.query 检查,该参数是一个字符串,实际上是您所用的SQL字符串已经在上面使用,而不是PDO::prepare()调用的结果值.

Check with http://php.net/pdo.query, the parameter is a string, actually the SQL string you already use above, not the result value of a PDO::prepare() call.

对于您的简单查询,您可以执行以下操作:

For your simple query you can just do:

$result = $dbh->query("SELECT * FROM students");

或者,如果您想准备:

$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query;

如果要在查询中插入变量,则后面是一些样板,这就是您准备它的原因.

The later is some boilerplate if you want to insert variables into the query, that's why you prepare it.

下一个问题是foreach行:

foreach($result as $row);

由于末尾用分号;,您将立即终止循环.删除该分号,以使后面的尖括号代码块成为foreach循环的主体.

You are terminating the loop immediately because of the semicolon ; at the end. Remove that semicolon so that the following angle-bracketed code-block becomes the body of the foreach-loop.

这篇关于使用PDO语句选择表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆