PHP/Mysql - 动态选择数据?
我对 OOP 有点陌生,但我已经使用它大约一个月了。通常,我创建一个名为 Mysql 的类,它有一个直接连接到数据库的 __construct 函数。之后,我有很多不同的函数可以将数据获取或插入到不同的表中。
今天在回家的巴士上,我开始思考,并想出了一个绝妙的主意,可以让房间不再那么混乱。我的想法是使用一个函数来选择数据(以及一个用于插入),并且根据传入的查询的外观,它将从不同的表中选择不同的数据。很好,对吧?
但我有点被困在这里。我完全不确定如何实现这一目标。我有一个关于它如何工作的小线索,但我不知道如何绑定结果,或将它们提取到数组中。查询将在另一个方法中创建,然后传递到 Mysql 类中的 select/insert 函数。
我画了一个“草图”来说明我认为它是如何运作的。如下:
当然,下面的函数将被放置在 Mysql 类中,并且已经连接到数据库。
// This is an example query that could be passed in.
$query = "SELECT * FROM table WHERE id=150";
function select_data($query) {
if ( $smtp = $this->conn->prepare($query) ) {
$smtp->execute();
$smtp->bind_results(What happens here?);
if ( $smtp->fetch() ) {
foreach ( fetched_row? as $key => $value ) {
$return[] = $key => $value;
}
return $return;
}
else return NULL;
}
else return $this->conn->error;
}
非常感谢任何能够向我展示如何实现这一目标的人。
I'm a bit new to OOP, but i've been playing with it for about a month now. Usually, i create a class called Mysql which has a __construct function that connects to a database directly. And after that i have lots of different functions that gets or inserts data into different tables.
On the bus home today, i began thinking and i came up with a brilliant idea that would make it less cluttered. My idea is to use one single function that selects data (and one for inserting), and depending on how the query that's passed in looks, it will select different data from different tables. Nice, right?
But i'm kind of stuck here. I'm not sure at all how to achieve this. I've got a small clue how it could work, but i don't know how i would bind the results, or fetch them into an array. The query will be created in another method, and then be passed into the select/insert function within the Mysql class.
I drew a "sketch" on how i think it may work. Here it is:
Of course, the function below will be placed in the Mysql class, and will already have connection to a database.
// This is an example query that could be passed in.
$query = "SELECT * FROM table WHERE id=150";
function select_data($query) {
if ( $smtp = $this->conn->prepare($query) ) {
$smtp->execute();
$smtp->bind_results(What happens here?);
if ( $smtp->fetch() ) {
foreach ( fetched_row? as $key => $value ) {
$return[] = $key => $value;
}
return $return;
}
else return NULL;
}
else return $this->conn->error;
}
Thanks a lot to anyone who can show me how this can be achieved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 PHP 中使用更多选项,并且它们都有自己的特点。
我可以推荐一些 ORM
就像Doctrine一样,因为它易于使用、稳定、社区以及最重要的有效性。
您可以像以下一样简单地使用它:
或:
You have more options to use in PHP and they has their own specifics.
I can recommend some ORM
like Doctrine because of ease of use, stability, community and most importantly efectivity.
You can use it as easy as:
or:
我认为当您需要相关数据时您会遇到问题。换句话说,当您的一个对象具有另一个对象的属性时,也应该收集并动态填充数据。我曾经走得很远,但是当像 INNER、LEFT 和 RIGHT 连接这样的东西遇到时,你会三思而后行;)
关于 bind_results:http://php.net/manual/en/mysqli-stmt.bind-result。 php
(也许有点偏离主题;SMTP?那是一个邮件协议,你确定你不是指 MySQLi 的 STMT 吗?)
I think you are running into problems when you need related data. In other words, when an object of yours has a property which is another object that data should also be gathered and dynamically filled. I once came pretty far but when stuff like INNER, LEFT and RIGHT joins come accross you'll think twice about going further ;)
About bind_results:http://php.net/manual/en/mysqli-stmt.bind-result.php
(Maybe slightly off topic; SMTP? That's a mailprotocol, are you sure you don't mean MySQLi's STMT?)
作为参考,PDO 已经做了很多你似乎想做的事情。它甚至有一个返回行数组的 fetchAll 方法,就像您的函数一样。您不需要绑定任何内容即可使用它,除非查询字符串中有参数(当然还有要绑定到这些参数的值)。
查看PDO 文档,看看它是否不符合您的需求。特别是
PDOStatement->fetchAll()
。For reference, PDO already does a lot of what you seem to want to do. It even has a
fetchAll
method that returns an array of rows, much like your function does. You don't need to bind anything in order to use it, unless you have parameters in your query string (and of course, values to bind to those parameters).Check out the PDO documentation, and see if that doesn't fit your needs. Particularly
PDOStatement->fetchAll()
.