Php PDO中mysql_result()函数的替代方法
我一直在php中使用mysql抽象类,现在我想通过使用PDO使该类更加可用和可靠。在我的类中,我使用了mysql_result()函数,该函数包含三个参数:Php PDO中mysql_result()函数的替代方法,php,mysql,pdo,Php,Mysql,Pdo,我一直在php中使用mysql抽象类,现在我想通过使用PDO使该类更加可用和可靠。在我的类中,我使用了mysql_result()函数,该函数包含三个参数: 结果:正在评估的结果资源。此结果来自调用mysql\u query() 行:正在检索的结果的行号。行号从0开始 字段:正在检索的字段的名称或偏移量 如何使用PDO实现类似的输出 下面是使用本机mysql函数编写的代码 public function SQLtoJSON($query, $indented = false) {
- 结果:正在评估的结果资源。此结果来自调用mysql\u query()
- 行:正在检索的结果的行号。行号从0开始
- 字段:正在检索的字段的名称或偏移量
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;
公共函数SQLtoJSON($query,$indented=false)
{
$query=mysql\u query($query)或die('MyJSON-SQLtoJSON-cannotmakequery');
if(!$numFields=mysql\u num\u字段($query)){
$this->errors[]='SQLtoJSON-无法获取MySQL字段数';
返回false;
}
$fields=array();
对于($i=0;$i<$numFields;$i++)
$fields[$i]=mysql\u field\u name($query,$i);
if(!$numRows=mysql\u num\u行($query)){
$this->errors[]=“SQLtoJSON-无法获取MySQL行数”;
返回false;
}
$res=array();
对于($i=0;$i<$numRows;$i++){
$res[$i]=array();
对于($j=0;$j
下面是使用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\u类转换为Json类
{
私人连接;
public$errors=array();
公共函数构造($db\u服务器,$db\u用户名,$db\u密码,$db\u名称)
{
$this->connection=newpdo(“mysql:host=$db\u server;dbname=$db\u name”、$db\u username、$db\u password);
}
公共函数MySQLtoJSON($query)
{
$query=$this->connection->query($query)或die(“无法执行查询”);
如果(!$numFields=$query->columnCount()){
$this->errors[]=“无法获取字段数”;
返回false;
}
$fields=array();
$colNames=array();
对于($i=0;$i<$numFields;$i++){
$fields[$i]=$query->getColumnMeta($i);
foreach($fields作为$field){
$colNames[]=$field['name'];
}
}
如果(!$numRows=$query->rowCount()){
$this->errors[]=“无法获取行数”;
返回false;
}
//返回$numRows;
$result=array();
对于($i=0;$i<$numFields;$i++){
$result[$i]=array();
对于($j=0;$jfetch($i);
}
}
$json=json_encode($result);
返回$json;
}
}
正如我在评论中怀疑和暗示的那样,您最初使用的mysql\u result()
是不必要的。在旧类中,您使用的是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;
}
…您无需担心行数或列数
在任何情况下,所有这些现在都是完全不必要的,可以用PDO中对的单个调用来代替,因为它所做的只是将所有行检索到一个列名称索引数组中。这与fetch操作的标准要求差不多
// 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);
请注意,由于您正在执行
$query
参数中的任意SQL语句,因此需要谨慎处理其中传递的内容,以避免SQL注入。如果您接受任何用户输入而不是静态查询,则需要考虑将其改为使用,并将其作为参数化查询执行。查看,您可能会在其中找到,其中包含您正在寻找的方法,如fetch
。请记住,使用mysql\u result
是相当罕见的,因为这种行为有点……怪异。您可以执行fetchAll()
然后按数组键访问行,但老实说,我建议您重构代码,不要依赖mysql\u result()
行为。如果您选择行,这通常意味着您的查询可以更智能地执行,只返回开始时需要的内容,如果您使用mysql\u result()循环
,这可能会更好地重构为更传统的获取循环。@Michael Berkowski,谢谢你的回答。实际上,我有一个类可以将mysql查询转换为json对象。该类是使用mysql数据库特定的函数编写的。现在,我想使用PDO转换该类,以扩展更多功能。mysql_query(),mysql_result()不久将被弃用。我一直坚持使用mysql_result()。如果你感兴趣,我可以与你分享我的代码:)@mahmudurrahmanshilly发布相关代码。mysql_result()
的专用用法有点异国情调,因此很可能你真的只需要一个简单的fetch循环加上类似于json\u encode的东西()
@Michael Berkowski,无法将代码粘贴到这里。仍然没有足够的权限。顺便说一句,我找到了一个方法。谢谢你的支持。太好了!学到了很多!谢谢你精彩的解释。
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;
}