我已经尝试按照PHP.net的说明进行SELECT查询,但我不确定这样做的最佳方法.
我想使用参数化SELECT查询,如果可能的话,返回ID表中name字段与参数匹配的表.这应该返回一个,ID因为它将是唯一的.
然后我想将它ID用于INSERT另一个表,所以我需要确定它是否成功.
我还读到您可以准备查询以供重用,但我不确定这有何帮助.
我有一个PHP脚本,通过mysql_选择数据,但最近我一直在读PDO是要走的路,而且mysql_正在变得折旧.现在我将该脚本转换为PDO.
我的问题是,我没有使用$ _POST来选择.我只想选择包含所有数据的整个表,所以我输入以下查询:
$query = $dbh->prepare("SELECT * FROM students");
$query->execute();
$result = $query->fetchall(); // or you can just $result = $query as hakre proposed!
Run Code Online (Sandbox Code Playgroud)
所以就像我使用旧的折旧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'] …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PDO回显表的所有行,但遇到了麻烦.
以旧的方式做我喜欢做的
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$title= $row['title'];
$body= $row['body'];
}
Run Code Online (Sandbox Code Playgroud)
但是我正在尝试使用PDO;
$result = $db->prepare("SELECT title, body FROM post");
$result->execute();
while ($row = $db->fetchAll(PDO::FETCH_ASSOC))
{
$title = $row['title'];
$body = $row['body'];
}
echo $title;
echo $body;
Run Code Online (Sandbox Code Playgroud)
哪个一直给我调用未定义的方法PDO :: fetchAll()
做手册中给出的例子
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)
工作,但我不认为我可以控制单个列,就像我用$ …
所以,现在我有一个PHP函数,它利用PDO返回特定表的第一行.这工作正常,但我希望返回所有信息,同时能够组织所有信息.
我有表zip__admins,我试图返回first_name,并last_name从表中.有了这些信息,我在登录页面上有一个按钮,要求用户选择他们的名字(每个人都有自己的按钮)来登录.截至目前,我正在返回一个结果,而不是两个结果.如何修改以下代码以返回两个结果,并将数据输入到模板参数中.
final public function fetchAdminInfo() {
global $zip, $db, $tpl;
$query = $db->prepare('SELECT first_name, last_name FROM zip__admins');
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$tpl->define('admin: first_name', $result['first_name']);
$tpl->define('admin: last_name', $result['last_name']);
}
Run Code Online (Sandbox Code Playgroud)
这是我的表:

我正在努力使用相当于以下查询的PDO,它计算了队列中有多少新项目并计算出完成它们的周数,从而给我一个工作堆时间表: -
//count new to be made
$new = "SELECT FLOOR(SUM(TotalNew) / 7) AS Weeks FROM
(
SELECT YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS , COUNT(
STATUS ) AS TotalNew
FROM new
WHERE
(STATUS = 'new'
OR STATUS = 'progress')
GROUP BY YEAR( date_ready ) , MONTHNAME( date_ready ) ,
STATUS ORDER BY YEAR( date_ready ) , MONTH( date_ready )
) Total";
$count = mysql_query($new) or die(mysql_error());
while($row = mysql_fetch_array($count)) {
$weeks = $row['Weeks'];
}
Run Code Online (Sandbox Code Playgroud)
我在哪里这是......
//count …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过PHP和PDO FetchAll()以及Prepared Statements填充来自MySql表的一列的所有结果,但我的代码只返回一行而不是所有结果.我在这里看到了这个问题(PHP PDO返回单行),这与我的问题相似但我没有用这个问题改进我的代码.
我按照PHP手册页面(http://php.net/manual/en/pdostatement.fetchall.php)中的示例尝试将我的代码构造/调整为MySeql表中一列的fetchAll()结果:
PHP手册页 - 带print的FetchAll示例:
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)
我使用HTML select标签基于上面的示例调整了我的代码:
<select name="category" class="form-control" required>
<option value="0" selected="selected" disabled="disabled" style="display: none">Select one category</option>
<?php
require_once 'pdo_connection.php';
$sth = $dbh-> prepare( 'SELECT * FROM categories' );
$sth-> execute();
$result = $sth-> fetchAll(); …Run Code Online (Sandbox Code Playgroud)