Login | Register | FAQ
Anonymous

Convert from mysql to PDO

+ Post a reply

2 posts Page 1 of 1


Convert from mysql to PDO

by slsv » Tue Jan 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?
Code: Select all
   
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.
User avatar

slsv

  • Posts: 1
  • Joined: Tue Jan 20, 2015 7:15 pm

Re: Convert from mysql to PDO

by XainPro » Sun Feb 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
User avatar

XainPro

  • Posts: 3933
  • Joined: Fri Feb 17, 2012 8:10 pm


+ Post a reply

Page 1 of 1