[英]Fetch values from one column of a table with PDO
I'm trying to fetch values from one table column of a MySQL db with PHP (PDO) , but I have two errors in the same line, this is the line (line 7): 我正在尝试使用PHP (PDO)从MySQL数据库的一个表列中获取值,但是我在同一行中有两个错误,这是该行(第7行):
$query = $dbh->prepare("SELECT * FROM table_name");
And these are error messages: 这些是错误消息:
Notice: Undefined variable: dbh in ... on line 7 注意:未定义的变量:dbh in ...在第7行
Fatal error: Call to a member function prepare() on a non-object in ... on line 7 致命错误:在第7行的...上的非对象上调用成员函数prepare()
My complete code: 我完整的代码:
1) file config.php 1)文件config.php
<?php
$host = "localhost";
$db_user = "user";
$db_psw = "pass";
$db_name = "database";
?>
2) file connection.php 2)文件connection.php
<?php
include("config.php");
//collegamento
$col = "mysql:host=$host;dbname=$db_name";
try {
//tentativo di connessione
$db = new PDO($col , "$db_user", "$db_psw");
}
//gestione errori
catch(PDOException $e) {
echo 'Attenzione errore: '.$e->getMessage();
}
?>
3) file display-values.php (error in line 7) 3)文件display-values.php (第7行错误)
<?php
// richiamo lo script responsabile della connessione a MySQL
require 'connection.php';
?>
<?php
$query = $dbh->prepare("SELECT * FROM table_name");
$query->execute();
$result = $query->fetchall();
?>
<?php
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>";
?>
Note: In this code I've tried to fetch all values (just for a test), but I need to fetch data from one column "name_column" only, so how can I fix the two errors and edit the code to extract values from a specific column? 注意:在这段代码中,我尝试获取所有值(仅用于测试),但是我只需要从“ name_column”列中获取数据,因此,如何解决这两个错误并编辑代码以从中提取值特定的列?
Thanks in advance! 提前致谢!
Your PDO object is db
not dbh
您的PDO对象是
db
而不是dbh
The error states that: 该错误指出:
Notice: Undefined variable: dbh in ... on line 7 注意:未定义的变量: dbh in ...在第7行
PHP couldn't find an initialized variable called dbh, so dbh is undefined variable, therefore you have your notice here. PHP找不到名为dbh的初始化变量,因此dbh是未定义的变量,因此请注意此处。
Fatal error: Call to a member function prepare() on a non-object in ... on line 7 致命错误:在第7行的...上的非对象上调用成员函数prepare()
As your code calls a method on a non existent variable, PHP is yelling this second error. 当您的代码在不存在的变量上调用方法时,PHP大喊第二个错误。
To sum up, you just need to change 总结一下,您只需要更改
$query = $dbh->prepare("SELECT * FROM table_name");
for 对于
$query = $db->prepare("SELECT * FROM table_name");
Editing query for fetching values from 2 columns: 编辑查询以从2列中获取值:
If you want to fetch values for 2 column only, just change your SQL query: 如果只想获取2列的值,则只需更改SQL查询即可:
$query = $db->prepare("SELECT column_name_1, column_name_2 FROM table_name");
With this query you're selecting values from column_name_1 and column_name_2 通过此查询,您可以从column_name_1和column_name_2中选择值
I recommend you to take some basic SQL tutorial 我建议您参加一些基本的SQL教程
Use $db
instead of $dbh
. 使用
$db
代替$dbh
。 Because in your connection.php
you are saving the database connection link in $db
and in your display-values.php
you are using $dbh
for executing the query. 因为在
connection.php
您将数据库连接链接保存在$db
而在display-values.php
,则使用$dbh
执行查询。 So try using $db
instead of $dbh
. 因此,尝试使用
$db
而不是$dbh
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.