使用PDO的类和方法以及检索数据(Classes and methods with PDO, and retrieving the data)

编程入门 行业动态 更新时间:2024-10-10 02:15:57
使用PDO的类和方法以及检索数据(Classes and methods with PDO, and retrieving the data)

我是OOP编程的新手,我真的迷失了这个标题所说的内容。 当我尝试将查询放入一个类和另一个文件中时,我在名为Main.php的文件中遇到错误,甚至不知道如何解决它们:

注意:未定义的变量:在第10行的Select.php中

致命错误:无法访问第10行上的Select.php中的空属性

如果我把选择放在Connection.php中,它会返回行,但是有了类,我就可以得到这些。

这是我的代码:

Connection.php:

<?php $hostname = 'localhost'; $username = 'user'; $password = 'pass'; function connectDB ($hostname, $username, $password){ $dbh = new PDO("mysql:host=$hostname;dbname=database", $username, $password); return $dbh; } $dbh = connectDB ($hostname, $username, $password); echo 'Connected to database <br/>';

Select.php:

<?php require_once 'Connection.php'; class Select { public function select() { $sql= "select * from table limit 10; <br/>"; echo $sql; $select = $dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($this->$sth as $row){ echo $row['column']."<br/>"; } } }

问题是,如何从查询中打印结果(例如从具有自动加载器的main.php中),以及为什么在单个文件中出现这些错误时,它们工作得很好?

编辑:

<?php $test = new Select($dbh); echo $test->select(); ?>

除了回复中的修复之外,我还将Connection.php包含在main.php中,将Select.php中的return更改为return ,现在它完美运行。 添加这个以防万一有人像我一样迷失。

I'm new to OOP programming, and I'm really lost with this what the title says. When I try to put the query in a class and in another file, I get errors in a file called Main.php and don't even know what to do to fix them:

Notice: Undefined variable: sth in Select.php on line 10

Fatal error: Cannot access empty property in Select.php on line 10

If I put the select in Connection.php, it returns the rows just fine, but with classes, I get those.

Here's my code:

Connection.php:

<?php $hostname = 'localhost'; $username = 'user'; $password = 'pass'; function connectDB ($hostname, $username, $password){ $dbh = new PDO("mysql:host=$hostname;dbname=database", $username, $password); return $dbh; } $dbh = connectDB ($hostname, $username, $password); echo 'Connected to database <br/>';

Select.php:

<?php require_once 'Connection.php'; class Select { public function select() { $sql= "select * from table limit 10; <br/>"; echo $sql; $select = $dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($this->$sth as $row){ echo $row['column']."<br/>"; } } }

The question is, how can I print the result from the query (for example from main.php, which has an autoloader), and why do I get those errors, when on a single file, they work just fine?

Edit:

<?php $test = new Select($dbh); echo $test->select(); ?>

Besides the fixes in the replies, I included Connection.php into the main.php, changed the echo in Select.php to return and it works perfectly now. Adding this in case someone ever gets as lost as me.

最满意答案

您不想遍历查询,而是查询的结果。 所以这可能是你正在寻找的:

<?php class Select { public function select() { $sql= 'select * from table limit 10'; $select = $dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($select as $row){ echo $row['column']."<br/>"; } } }

而且您还需要注意$dbh对象实际上存在于该方法中。 将其注入到对象中或将其指定为方法参数。 所以你的全班可能看起来像这样:

<?php class Select { private $dbh; public function __construct($dbh) { $this->dbh = $dbh; } public function select() { $sql= 'select * from table limit 10'; $select = $this->dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($select as $row){ echo $row['column']."<br/>"; } } }

然后你像这样实例化对象:

$selectObj = new Select($dbh);

但是,一些常规警告:使用PDO的fetchall()方法很方便,但带来了巨大的风险:这意味着必须将完整的结果集复制到php脚本内的数组中。 对于可能导致内存使用问题(由于安全原因终止脚本)的更大结果。 在每次迭代中从结果集中获取的单个行上使用while循环通常是更好的方法。

You do not want to iterate over the query, but the result of that query. So this probably is what you are looking for:

<?php class Select { public function select() { $sql= 'select * from table limit 10'; $select = $dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($select as $row){ echo $row['column']."<br/>"; } } }

And you also need to take care that the $dbh object is actually present inside that method. Either inject it into the object or specify it as method argument. So your full class will probably look something like that:

<?php class Select { private $dbh; public function __construct($dbh) { $this->dbh = $dbh; } public function select() { $sql= 'select * from table limit 10'; $select = $this->dbh->query($sql)->fetchall(PDO::FETCH_ASSOC); foreach($select as $row){ echo $row['column']."<br/>"; } } }

And you instantiate the object like that:

$selectObj = new Select($dbh);

Some general warning, though: Using PDO's fetchall() method is convenient, but carries a huge risk: it means that the full result set has to be copied into an array inside the php script. For bigger results that may lead to issues with memory usage (scripts getting terminated for security reasons). Often it is the better approach to use a while loop over a single row fetched from the result set in each iteration.

更多推荐

本文发布于:2023-07-09 10:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   数据   PDO   Classes   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!