如何使用PHP中的对象从MYSQL数据库连接/检索数据?

时间:2021-08-17 07:15:49

Generally I connect and retrieve data using the standard way (error checking removed for simplicity):

通常我使用标准方式连接和检索数据(为简单起见,删除了错误检查):

$db = mysql_select_db("dbname", mysql_connect("host","username","passord"));
$items = mysql_query("SELECT * FROM $db");

while($item = mysql_fetch_array($items)) {
    my_function($item[rowname]);
}

Where my_function does some useful things witht that particular row.

my_function在那个特定行中做了一些有用的事情。

What is the equivalent code using objects?

使用对象的等效代码是什么?

2 个解决方案

#1


3  

Since version 5.1, PHP is shipped with the PDO driver, which gives a class for prepared statements.

从5.1版开始,PHP随PDO驱动程序一起提供,它为预准备语句提供了一个类。

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); //connect to the database
//each :keyword represents a parameter or value to be bound later
$query= $dbh->prepare('SELECT * FROM users WHERE id = :id AND password = :pass');

# Variables are set here.
$query->bindParam(':id', $id); // this is a pass by reference 
$query->bindValue(':pass', $pass); // this is a pass by value

$query->execute(); // query is run

// to get all the data at once
$res = $query->fetchall();
print_r($res);

see PDO driver at php.net

在php.net上看到PDO驱动程序

Note that this way (with prepared statements) will automatically escape all that needs to be and is one of the safest ways to execute mysql queries, as long as you use binbParam or bindValue.

请注意,这种方式(使用预准备语句)将自动转义所有需要的内容,并且是执行mysql查询的最安全的方法之一,只要您使用binbParam或bindValue即可。

There is also the mysqli extension to do a similar task, but I personally find PDO to be cleaner.

还有mysqli扩展来执行类似的任务,但我个人觉得PDO更干净。

What going this whole way around and using all these steps gives you is possibly a better solution than anything else when it comes to PHP.

在整个过程中使用所有这些步骤可能是一个比PHP更好的解决方案。

You can then use $query->fetchobject to retrieve your data as an object.

然后,您可以使用$ query-> fetchobject将数据作为对象检索。

#2


2  

You can use the mysql_fetch_object()

你可以使用mysql_fetch_object()

http://is2.php.net/manual/en/function.mysql-fetch-object.php

#1


3  

Since version 5.1, PHP is shipped with the PDO driver, which gives a class for prepared statements.

从5.1版开始,PHP随PDO驱动程序一起提供,它为预准备语句提供了一个类。

$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password); //connect to the database
//each :keyword represents a parameter or value to be bound later
$query= $dbh->prepare('SELECT * FROM users WHERE id = :id AND password = :pass');

# Variables are set here.
$query->bindParam(':id', $id); // this is a pass by reference 
$query->bindValue(':pass', $pass); // this is a pass by value

$query->execute(); // query is run

// to get all the data at once
$res = $query->fetchall();
print_r($res);

see PDO driver at php.net

在php.net上看到PDO驱动程序

Note that this way (with prepared statements) will automatically escape all that needs to be and is one of the safest ways to execute mysql queries, as long as you use binbParam or bindValue.

请注意,这种方式(使用预准备语句)将自动转义所有需要的内容,并且是执行mysql查询的最安全的方法之一,只要您使用binbParam或bindValue即可。

There is also the mysqli extension to do a similar task, but I personally find PDO to be cleaner.

还有mysqli扩展来执行类似的任务,但我个人觉得PDO更干净。

What going this whole way around and using all these steps gives you is possibly a better solution than anything else when it comes to PHP.

在整个过程中使用所有这些步骤可能是一个比PHP更好的解决方案。

You can then use $query->fetchobject to retrieve your data as an object.

然后,您可以使用$ query-> fetchobject将数据作为对象检索。

#2


2  

You can use the mysql_fetch_object()

你可以使用mysql_fetch_object()

http://is2.php.net/manual/en/function.mysql-fetch-object.php