登录 | 注册 | FAQ
Anonymous

Convert from mysql to PDO

+ 发表回复

2 篇帖子 分页: 1 / 1


Convert from mysql to PDO

slsv » 周二 1月 20, 2015 7:26 pm

Hi,
I'm new here and new to PDO at all. I want to fix some old my codes to PDO. I need help with first one. Is there someone who can help a little?
代码: 全选
   
include 'misc/databe.inc.php';
$strSQL = "SELECT * FROM res";
   $objQuery = mysql_query($strSQL);
   $intNumField = mysql_num_fields($objQuery);
   $resultArray = array();
   while($obResult = mysql_fetch_array($objQuery))
   {
      $arrCol = array();
      for($i=0;$i<$intNumField;$i++)
      {
         $arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
      }
      array_push($resultArray,$arrCol);
   }
   
   mysql_close($objConnect);
   echo json_encode($resultArray);

This is what I want to rewrite.
头像

slsv

  • 帖子: 1
  • 注册: 周二 1月 20, 2015 7:15 pm

Re: Convert from mysql to PDO

XainPro » 周日 2月 08, 2015 7:51 pm

it's quite Easy 1st you need to understand how PDO create,update, delete work
there is no connection in your mentioned code it's probably in datable.inc.php file

Connect to your databse using PDO like this
$db = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');

replace host, username and password from your own.
then do it like this


include 'misc/databe.inc.php';

$data = $conn->query("SELECT * FROM res");
echo json_encode($data);


modify it according to your need or learn how PDO work from here

http://code.tutsplus.com/tutorials/php- ... -net-25338
头像

XainPro

  • 帖子: 3933
  • 注册: 周五 2月 17, 2012 8:10 pm


+ 发表回复

分页: 1 / 1