PHP echo 和 PDO - 如何解决成员函数查询错误?
这是我之前的问题的延续 Using PDO to替换 mysql_connect - 格式正确吗?
这是一个较大页面的一部分,它本身将被回显到另一个页面
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<?php foreach ($dbh->query($sql) as $row) ?>
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
它给出了此错误:
Fatal error: Call to a member function query() on a non-object in C:\www\vhosts\localhost\radio1.php on line 29
我想避免此错误,并且能够回声行如果我可以...不使用此代码(我的原始编码 - 它有效,但我尝试将 PDO 与 echo 一起使用,如上面的示例所示):
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
echo $row['presenter'] .' - '. $row['show'] . '<br />';
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
什么可能导致此错误,为什么?
我应该如何处理代码以确保这是循环的:
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
基本上,我正在尝试使用 PDO 来替换 mysql_connect 来回显我的测试站点的表中的行或 HTML 中的定义列表。
现在是编辑后的版本:
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
echo $row['presenter'] .' - '. $row['show'] . '<br />';
}
/*** close the database connection ***/
$dbh;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
它显示数据,但不在下表中。
This is a continuation of my previous question at Using PDO to replace mysql_connect - formatting correctly?
This is part of a larger page which itself will be echo'd into another page
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<?php foreach ($dbh->query($sql) as $row) ?>
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
It gave this error:
Fatal error: Call to a member function query() on a non-object in C:\www\vhosts\localhost\radio1.php on line 29
I want to avoid this error, and be able to echo the rows if I can... without using this code (my original coding - it worked, but I'm trying to use PDO with echo like in the example above):
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
echo $row['presenter'] .' - '. $row['show'] . '<br />';
}
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
What could be causing this error, and why?
What should I do with the code to ensure that this is looped:
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
Basically, I'm trying to use PDO to replace the mysql_connect for echoing rows in tables or definition lists in HTML for my testing site.
Here's the edited version now:
<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'MYPASSWORD';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=radiotest", $username, $password);
/*** echo a message saying we have connected ***/
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM presenters";
foreach ($dbh->query($sql) as $row)
{
echo $row['presenter'] .' - '. $row['show'] . '<br />';
}
/*** close the database connection ***/
$dbh;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
<table>
<td>
<tr><?php echo $row['presenter'] ?><?php echo $row['show'] ?> </tr>
</table>
It displays the data, but not within the table below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在设置
$dbh = null;
。所以$dbh
在第 29 行不再可用。此外,您在第一个示例中所做的事情效率非常低。您正在查询数据库两次。最好第一次保存结果集,并在必要时稍后使用。
您可能需要类似以下内容:
You're setting
$dbh = null;
. So$dbh
is not available anymore at line 29.Furthermore it's pretty inefficient what you are doing in your first example. You are querying the database twice. Better save the result set first time around, and use that later on, if necessary.
You probably need something like the following:
您将在
try
块底部杀死数据库句柄。您打开连接,运行查询,获取结果,丢弃结果,然后关闭连接。因此,当 PHP 到达第 29 行时,$dbh 现在为 NULL,并且不能再用作 PDO 对象。
You're killing your database handle at the bottom of the
try
block. You open a connection, run a query, fetch the results, throw away the results, then close the connection.So by the time PHP reaches line 29, $dbh is now NULL and can't be used as a PDO object anymore.
您可以在 try 块内定义
$dbh
。 Out 不存在于 out 之外,但您却试图使用它。摆脱 try 块。如果抛出异常,脚本将自动停止。此外,您在减速结束时将其设置为
null
。另外为什么你要尝试访问 try 块之外的 dbh ?我看不出有什么目的。
You define
$dbh
inside of a try block. Out doesn't exists outside of out, yet you are trying to use it. Get rid of the try block. If an exception will be thrown, the script will automatically stop.Also, you're setting it to
null
at the end of your deceleration.Also why are you trying to access dbh outside of the try block? I see no purpose.
PDO 并不比 mysql ext 好
你需要更智能的抽象层。
另外,echo 与 PDO 无关。你必须使用模板,并将所有代码分为获取数据部分和显示数据部分
PDO is no better than mysql ext
you need more intelligent abstraction layer.
also, echo has nothing to do with PDO. you have to use templates, and divide all your codes into getting data part and displaying data part