我一直在 php 中使用 mysql 抽象类,现在我想通过使用 PDO 使该类更加可用和可靠。在我的课上,我使用了带有三个参数的 mysql_result() 函数:
- result:正在评估的结果资源。此结果来自调用 mysql_query()。
- 行:正在检索的结果中的行号。行号从 0 开始。
- field:正在检索的字段的名称或偏移量。
如何使用 PDO 实现类似的输出?
这里是用原生 mysql 函数编写的代码。
public function SQLtoJSON($query, $indented = false)
{
$query = mysql_query($query) or die ('MyJSON - SQLtoJSON - Cannot make query');
if (!$numFields = mysql_num_fields($query)) {
$this->errors[] = 'SQLtoJSON - Cannot get number of MySQL fields';
return false;
}
$fields = array();
for ($i = 0; $i < $numFields; $i++)
$fields[$i] = mysql_field_name($query, $i);
if (!$numRows = mysql_num_rows($query)) {
$this->errors[] = 'SQLtoJSON - Cannot get number of MySQL rows';
return false;
}
$res = array();
for ($i = 0; $i < $numRows; $i++) {
$res[$i] = array();
for ($j = 0; $j < count($fields); $j++)
$res[$i][$fields[$j]] = mysql_result($query, $i, $j);
}
$json = json_encode($res);
if ($indented == false)
return $json;
这里是使用 PDO 的代码的更新版本:
class MySql_To_Json
{
private $connection;
public $errors = array();
public function __construct($db_server, $db_username, $db_password, $db_name)
{
$this->connection = new PDO("mysql:host=$db_server;dbname=$db_name", $db_username, $db_password);
}
public function MySQLtoJSON($query)
{
$query = $this->connection->query($query) or die("Unable to execute the query");
if (!$numFields = $query->columnCount()) {
$this->errors[] = "Unable to get the number of fields";
return false;
}
$fields = array();
$colNames = array();
for ($i = 0; $i < $numFields; $i++) {
$fields[$i] = $query->getColumnMeta($i);
foreach ($fields as $field) {
$colNames[] = $field['name'];
}
}
if (!$numRows = $query->rowCount()) {
$this->errors[] = "Unable to get the number of rows";
return false;
}
// return $numRows;
$result = array();
for ($i = 0; $i < $numFields; $i++) {
$result[$i] = array();
for ($j = 0; $j < count($field); $j++) {
return $result[$i][$field[$j]] = $query->fetch($i);
}
}
$json = json_encode($result);
return $json;
}
}
最佳答案
正如我在评论中怀疑和暗示的那样,您最初使用 mysql_result()
是不必要的。您在旧类(class)中拥有的是一组 C 风格的增量 for
循环,这在 PHP 中很少适用。相反,使用 foreach
迭代数组以及处理数据库 API 时更常见的做法是:
// This is the normal and easy way to get an associative array of results:
while ($row = mysql_fetch_assoc()) {
$results[] = $row;
}
...您无需担心行数或列数。
无论如何,所有这些现在都是完全没有必要的,可以通过一次调用 fetchAll()
来代替在 PDO 中,因为它所做的只是将所有行检索到列名索引数组中。这与提取操作的标准差不多。
// ALL of this...
// $res = array();
// for ($i = 0; $i < $numRows; $i++) {
// $res[$i] = array();
// for ($j = 0; $j < count($fields); $j++)
// $res[$i][$fields[$j]] = mysql_result($query, $i, $j);
// }
// ...may be replaced with simply:
$res = $query->fetchAll(PDO::FETCH_ASSOC);
这也意味着您真的不需要前面两个用于检索行数和列数的 block 。所有这些都隐含在对 fetchAll()
的调用中。您现在真正需要的唯一部分是查询和获取:
public function MySQLtoJSON($input_query)
{
// Do your query...
$query = $this->connection->query($input_query) or die("Unable to execute the query");
// Fetch the rows as associative arrays
$result = $query->fetchAll(PDO::FETCH_ASSOC);
// And encode it as JSON
$json = json_encode($result);
return $json;
}
请注意,由于您是在 $query
参数中执行任意 SQL 语句,因此您需要对传入的内容保持谨慎,以避免 SQL 注入(inject)。如果您接受任何用户输入而不是静态查询,则需要考虑调整它以使用 PDO::prepare()
并将它们作为参数化查询执行。
关于php - PDO 中用于 mysql_result() 函数的替代方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17839652/