使用PDO语句选择表数据

时间:2023-01-21 15:04:04

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.

我有一个PHP脚本,通过mysql_选择数据,但最近我一直在读PDO是要走的路,而且mysql_正在变得折旧。现在我将该脚本转换为PDO。

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 :

我的问题是,我没有使用$ _POST来选择。我只想选择包含所有数据的整个表,因此我输入以下查询:

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

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.

所以就像我使用旧的折旧mysql_版本的脚本一样,我使用echo来回显一个带有数据的表。

    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.

现在使用这个,我无法获得单个输出到我的表。

使用PDO语句选择表数据

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)

我的问题是我在选择陈述中遗漏了什么?或者我没有取任何行?另外我在init.php需要的另一个名为connect.php的脚本中设置连接设置(在我的所有页面的顶部)

Edit : 1

编辑: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! 使用PDO语句选择表数据

编辑代码,使其现在可以正常工作,还添加了一张图片,向其他人展示它应该如何显示!希望有人可以将它用于某种用途!

2 个解决方案

#1


11  

You are doing too much actually:

你实际上做得太多了:

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

The problematic line is:

问题在于:

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

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.

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

For your simple query you can just do:

对于简单的查询,您可以这样做:

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

Or if you like to prepare:

或者如果你想准备:

$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.

如果要将变量插入查询中,则后者是一些样板文件,这就是您准备它的原因。


The next problem is with the foreach line:

下一个问题是foreach行:

foreach($result as $row);

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.

由于分号,你正在立即终止循环;最后。删除该分号,以便以下角度括号的代码块成为foreach循环的主体。

#2


6  

Your code is wrong:

你的代码错了:

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

After executing a prepared statement, you can just call fetchAll() on it:

执行预准备语句后,您可以在其上调用fetchAll():

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

The rest of your code will work fine once you remove the semicolon after the foreach.

在foreach之后删除分号后,其余代码将正常工作。

#1


11  

You are doing too much actually:

你实际上做得太多了:

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

The problematic line is:

问题在于:

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

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.

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

For your simple query you can just do:

对于简单的查询,您可以这样做:

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

Or if you like to prepare:

或者如果你想准备:

$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.

如果要将变量插入查询中,则后者是一些样板文件,这就是您准备它的原因。


The next problem is with the foreach line:

下一个问题是foreach行:

foreach($result as $row);

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.

由于分号,你正在立即终止循环;最后。删除该分号,以便以下角度括号的代码块成为foreach循环的主体。

#2


6  

Your code is wrong:

你的代码错了:

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

After executing a prepared statement, you can just call fetchAll() on it:

执行预准备语句后,您可以在其上调用fetchAll():

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

The rest of your code will work fine once you remove the semicolon after the foreach.

在foreach之后删除分号后,其余代码将正常工作。