473,396 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PDO and MYSQLI

i tried moving the code below to PDO
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //Make a database connection
  3. session_start();
  4. mysql_connect("localhost", "root", "");
  5. mysql_select_db("test");
  6.  
  7. //Login section start
  8. if(!isset($_SESSION["logged"])) {
  9.   if(isset($_POST["username"]) && ($_POST['password'])) {
  10.     $query = mysql_query("SELECT id FROM login WHERE username = '" . $_POST["username"] . "' AND password = '" . $_POST["password"] . "'");
  11.     if(mysql_num_rows($query) > 0) {
  12.       $row = mysql_fetch_array($query);
  13.       $_SESSION["logged"] = $row["id"];
  14.       header("Location: " . $_SERVER["PHP_SELF"]);
  15.     }
  16.   } else {
  17.     echo("<form method=\"POST\">
  18.     <input type=\"text\" name=\"username\" value=\"Type username here\">
  19.     <input type=\"text\" name=\"password\" value=\"Type username here\">
  20.     <input type=\"submit\" name=\"submit\">  
  21.     </form>");
  22.   }
  23. } else {
  24. //end of login section
  25.  
  26. //Section for adding
  27.   if(isset($_GET["add"])) {
  28.     $query = mysql_query("SELECT id FROM login WHERE id = '" . $_GET["add"] . "'");
  29.     if(mysql_num_rows($query) > 0) {
  30.       $_query = mysql_query("SELECT * FROM data WHERE leo = '" . $_SESSION["logged"] . "' AND jon = '" . $_GET["add"] . "'");
  31.       if(mysql_num_rows($_query) == 0) {
  32.         mysql_query("INSERT INTO data SET leo = '" . $_SESSION["logged"] . "', jon = '" . $_GET["add"] . "'");
  33.       }
  34.     }
  35.   }
  36. //END
  37.  
  38. //Section for exceting
  39.   if(isset($_GET["accept"])) {
  40.     $query = mysql_query("SELECT * FROM data WHERE leo = '" . $_GET["accept"] . "' AND jon = '" . $_SESSION["logged"] . "'");
  41.     if(mysql_num_rows($query) > 0) {
  42.  
  43.       $_query = mysql_query("SELECT * FROM login WHERE id = '" . $_GET["accept"] . "'");
  44.       $_row = mysql_fetch_array($_query);
  45.  
  46.       $rec = unserialize($_row["rec "]);
  47.       $rec [] = $_SESSION["rec "];      
  48.  
  49.       mysql_query("UPDATE login SET rec  = '" . serialize($rec ) . "' WHERE id = '" . $_GET["accept"] . "'");
  50.  
  51.       $_query = mysql_query("SELECT * FROM login WHERE id = '" . $_SESSION["logged"] . "'");
  52.       $_row = mysql_fetch_array($_query);
  53.  
  54.       $rec  = unserialize($_row["rec "]);
  55.       $rec [] = $_GET["accept"];      
  56.  
  57.       mysql_query("UPDATE login SET rec  = '" . serialize($rec ) . "' WHERE id = '" . $_SESSION["logged"] . "'");
  58.     }
  59.     mysql_query("DELETE FROM data WHERE leo = '" . $_GET["accept"] . "' AND jon = '" . $_SESSION["logged"] . "'");
  60.   }
  61. //END
  62.  
  63. //Section for showing
  64.   $query = mysql_query("SELECT * FROM data WHERE leo = '" . $_SESSION["logged"] . "'");
  65.   if(mysql_num_rows($query) > 0) {
  66.     while($row = mysql_fetch_array($query)) {
  67.       $_query = mysql_query("SELECT * FROM login WHERE id = '" . $row["leo"] . "'");
  68.       while($_row = mysql_fetch_array($_query)) {
  69.          }
  70.     }
  71.   }
  72. //END
  73.  
  74. //Section for showing 2
  75.  
  76.   $query = mysql_query("SELECT * FROM login WHERE id != '" . $_SESSION["logged"] . "'");
  77.   while($row = mysql_fetch_array($query)) {
  78.     $ok = false;
  79.     $rec = unserialize($row["rec"]);
  80.     if(isset($rec[0])) {
  81.       foreach($rec as $rec) {
  82.         if($rec == $_SESSION["logged"]) $ok = true;
  83.       }
  84.     }
  85.     echo "not bad";
  86.     $_query = mysql_query("SELECT * FROM data WHERE leo = '" . $_SESSION["logged"] . "' AND jon = '" . $row["id"] . "'");
  87.     if(mysql_num_rows($_query) > 0) {
  88.        echo "good.";
  89.     } elseif($ok == false) {
  90.        echo " good again";
  91.     } else {
  92.       echo " thats me.";
  93.     }
  94.     echo "<br />";
  95.   }
  96. //END
  97.  
  98. //display
  99.  
  100.   $query = mysql_query("SELECT rec FROM login WHERE id = '" . $_SESSION["logged"] . "'");
  101.   while($row = mysql_fetch_array($query)) {
  102.     $rec = unserialize($row["rec"]);
  103.  
  104.     if(isset($rec[0])) {
  105.       foreach($rec as $rec) {
  106.         $_query = mysql_query("SELECT username FROM login WHERE id = '" . $rec . "'");
  107.         $_row = mysql_fetch_array($_query);
  108.         echo  "thanks";
  109.       }
  110.     }
  111.   }
  112. //END
  113. }
  114. ?>
  115.  
  116.  

below is my code migration to PDO but is not working

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4.  
  5.  
  6. <?php
  7. //Make a database connection
  8. session_start();
  9. error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
  10. require "config.php"; // Database connection details.
  11. dataConnect();
  12.  
  13.  
  14.  
  15.  
  16. //Login section start
  17. if(!isset($_SESSION["logged"])) {
  18.   if(isset($_POST["username"]) && ($_POST['password'])) {
  19.     $count=$dbo->prepare("select id FROM login where username=:username AND password=:password");
  20. $count->bindParam(":username",$username,PDO::PARAM_STRING,30);
  21. $count->bindParam(":password",$password,PDO::PARAM_STRING,35);
  22. $count->execute();
  23. $total = $count->rowCount();
  24.  
  25.  
  26. if($total>0){
  27. echo " login Success <br>";
  28. $row = $count->fetch(PDO::FETCH_OBJ);
  29. $_SESSION["logged"] = row->id;
  30. //$row = $count->fetch();
  31. //print_r($row);
  32. header("Location: " . $_SERVER["PHP_SELF"]);
  33. }else{
  34. echo("<form method=\"POST\">
  35.     <input type=\"text\" name=\"username\" value=\"Type username here\">
  36.     <input type=\"text\" name=\"password\" value=\"Type username here\">
  37.     <input type=\"submit\" name=\"submit\">  
  38.     </form>");
  39.  
  40. }
  41.  
  42. } else {
  43. //end of login section
  44.  
  45.  
  46. //Section for adding friend
  47.   if(isset($_GET["add"])) {
  48. $count=$dbo->prepare("select * from login where id=:add");
  49. $count->bindParam(":add",$id,PDO::PARAM_INT,1);
  50. $count->execute();
  51. $total = $count->rowCount();
  52. $row = $count->fetch(PDO::FETCH_OBJ);
  53.  
  54.  
  55. if($total>0){
  56. $count2=$dbo->prepare("select * from data where leo=:add AND jon=:logged");
  57. $count2->bindParam(":add",$leo,PDO::PARAM_STRING,1);
  58. $count2->bindParam(":logged",$jon,PDO::PARAM_STRING,2);
  59. $count2->execute();
  60. $total2 = $count2->rowCount();
  61. $row = $count2->fetch(PDO::FETCH_OBJ);
  62. if($total2==0){
  63. $sql=$dbo->prepare("INSERT INTO data SET leo =:logged,jon=:add");
  64.  
  65. $sql->bindParam(':logged',$leo,PDO::PARAM_STR, 15);
  66. $sql->bindParam(':add',$jon,PDO::PARAM_STR, 15);
  67.  
  68. if($sql->execute()){
  69. $id=$dbo->lastInsertId();
  70. echo " Thanks .. Your  id = $id ";
  71. }
  72. else{
  73. echo " Not able to add data please ";
  74. }
  75.   }
  76.     } 
  77.   }
  78. //END
  79.  
  80.  
  81. //Section for exceting
  82.   if(isset($_GET["accept"])) {
  83.  
  84. $count=$dbo->prepare("select * from data where leo=:accept AND jon =:logged");
  85. $count->bindParam(":accept",$leo,PDO::PARAM_STRING,34);
  86. $count->bindParam(":logged",$jon,PDO::PARAM_STRING,34);
  87. $count->execute();
  88. $total = $count->rowCount();
  89. $row = $count->fetch(PDO::FETCH_OBJ);
  90.  
  91. if($total>0){
  92. $count2=$dbo->prepare("select * from login WHERE id=:accept");
  93. $count2->bindParam(":accept",$id,PDO::PARAM_INT,1);
  94. $count2->execute();
  95. $total2 = $count->rowCount();
  96. $row = $count2->fetch(PDO::FETCH_OBJ);
  97.  
  98. $rec = unserialize($row["rec"]);
  99.  $rec[] = $_SESSION["rec"];    
  100.  
  101. $sql=$dbo->prepare("update login SET rec=:serialize($rec) where id=accept");
  102. $sql->bindParam(":id",$id,PDO::PARAM_INT,1);
  103. $sql->bindParam(':rec',$rec,PDO::PARAM_STR, 15);
  104. if($sql->execute()){
  105. echo "Successfully updated";
  106. }
  107.  
  108.  
  109.  
  110. $count2=$dbo->prepare("select * from login WHERE id=:logged");
  111. $count2->bindParam(":logged",$id,PDO::PARAM_INT,1);
  112. $count2->execute();
  113. $total2 = $count->rowCount();
  114. $row = $count2->fetch(PDO::FETCH_OBJ);
  115.  
  116.  
  117. $rec = unserialize($row["rec"]);
  118.   $rec[] = $_GET["accept"];     
  119.  
  120.  
  121.  $sql=$dbo->prepare("update login SET rec=:serialize($rec) where id=logged");
  122. $sql->bindParam(":id",$id,PDO::PARAM_INT,1);
  123. $sql->bindParam(':rec',$rec,PDO::PARAM_STR, 15);
  124. if($sql->execute()){
  125. echo "Successfully updated";
  126. }
  127.  
  128.     }
  129. $sql=$dbo->prepare("delete  from data where leo=:accept AND jon =:logged");
  130. $sql->bindParam(":id",$id,PDO::PARAM_INT,1);
  131. $sql->bindParam(':rec',$rec,PDO::PARAM_STR, 15);
  132. if($sql->execute()){
  133. echo "Successfully deleted";
  134. }
  135.  
  136.   }
  137. //END
  138.  
  139.  
  140.  
  141.  
  142.  
  143. //Section for showing 
  144. $count=$dbo->prepare("select * from data where  leo =:logged");
  145. $count->bindParam(":logged",$leo,PDO::PARAM_STRING,34);
  146. $count->execute();
  147. $total = $count->rowCount();
  148. $row = $count->fetch(PDO::FETCH_OBJ);
  149.  
  150. if($total>0){
  151. while($row = $count->fetch(PDO::FETCH_OBJ)) { 
  152.  
  153. $count2=$dbo->prepare("SELECT * FROM login where  id =: $row['leo']");
  154. $count2->bindParam(":id",$row['leo'],PDO::PARAM_STRING,34);
  155. $count2->execute();
  156. $total 2= $count2->rowCount();
  157. $row2 = $count2->fetch(PDO::FETCH_OBJ);
  158.  
  159. while($row2 = $count2->fetch(PDO::FETCH_OBJ)) { 
  160.  
  161. echo " $row2->username";
  162. }
  163.     }
  164.   }
  165. //END
  166.  
  167.  
  168. //Section for showing 2
  169.  
  170. $count=$dbo->prepare("SELECT * FROM login WHERE id ! =:logged");
  171. $count->bindParam(":logged",$id,PDO::PARAM_STRING,34);
  172. $count->execute();
  173. $total = $count->rowCount();
  174. $row = $count->fetch(PDO::FETCH_OBJ);
  175. while($row = $count->fetch(PDO::FETCH_OBJ)) { 
  176.  $ok = false;
  177.     $rec = unserialize($row["rec"]);
  178. if(isset($rec[0])) {
  179.       foreach($rec as $rec) {
  180.         if($rec == $_SESSION["logged"]) $ok = true;
  181.       }
  182.     }
  183.     echo "not bad";
  184.  
  185.  
  186. $count2=$dbo->prepare("SELECT * from data where leo=:logged AND jon =:$row->id");
  187. $count2->bindParam(":id",$row->id,PDO::PARAM_STRING,34);
  188. $count2->execute();
  189. $total2= $count2->rowCount();
  190. $row2 = $count2->fetch(PDO::FETCH_OBJ);
  191. if($total2>0){
  192.  echo " good";
  193.     } elseif($ok == false) {
  194.        echo " good again";
  195.     } else {
  196.       echo " thats me";
  197.     }
  198.     echo "<br />";
  199.   }
  200. //END
  201.  
  202.  
  203.  
  204.  
  205. $count=$dbo->prepare("SELECT * FROM login WHERE id =:logged");
  206. $count->bindParam(":logged",$id,PDO::PARAM_STRING,34);
  207. $count->execute();
  208. $total = $count->rowCount();
  209. $row = $count->fetch(PDO::FETCH_OBJ);
  210. while($row = $count->fetch(PDO::FETCH_OBJ)) { 
  211.  
  212.     $rec = unserialize($row["rec"]);
  213. if(isset($rec[0])) {
  214.       foreach($rec as $rec) {
  215. $count2=$dbo->prepare("SELECT username FROM login WHERE id =:$rec");
  216. $count2->bindParam(":id",$rec,PDO::PARAM_STRING,34);
  217. $count2->execute();
  218. $total2= $count2->rowCount();
  219. $row2 = $count2->fetch(PDO::FETCH_OBJ);
  220.  echo "Thanks";
  221.       }
  222.     }
  223.   }
  224. //END
  225. }
  226.  
  227.  
  228.  
  229. ?>
  230.  
  231.  
  232.  
  233.  
Apr 29 '13 #1
5 2029
Dormilich
8,658 Expert Mod 8TB
there’s possibly something wrong in config.php.

and I’m not sure, where your length values are correct.

tune up PDO’s error handling:
Expand|Select|Wrap|Line Numbers
  1. $pdo = new PDO(...);
  2. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Apr 29 '13 #2
config file is okay.please look at the code.it seems that i combined mysql_function deprecated to PDO
Thanks
Apr 29 '13 #3
Dormilich
8,658 Expert Mod 8TB
config file is okay
how can you know that the config file is ok and the other PDO code not?
Apr 29 '13 #4
i used config file to run other applications and is working.
here am just trying to move the code from mysql_function deprecated to PDO. Am new to PDO,please check the two code and know what is wrong, perhap i need your correction

below is the config.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function dataConnect() {
  3. global $dbo;
  4.  
  5. $info['dbhost_name'] = "localhost";
  6. $info['database'] = "test";
  7. $info['username'] = "root";
  8. $info['password'] = "";
  9.  
  10. $dbConnString = "mysql:host=" . $info['dbhost_name'] . "; dbname=" . $info['database'];
  11.  
  12. $dbo = new PDO($dbConnString, $info['username'], $info['password']);
  13. $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
  14. //$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.  
  16. $error = $dbo->errorInfo();
  17. if($error[0] != "") {
  18. }
  19. }
  20. ?>
  21.  
  22.  
Thanks
Apr 29 '13 #5
Dormilich
8,658 Expert Mod 8TB
doesn’t look wrong either, but has some issues?

- why do you write a function when you use global anyway?
- a PDO connection error always throws an exception
- why do you set the error mode to silent? if something goes wrong, you won’t be notified.
Apr 29 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: yzzzzz | last post by:
Hi, I am compiling PHP 5.0.2 myself with MySQL support. I did a ./configure --with-mysqli=/usr/local/mysql/bin/mysql_config (see end of post for complete configure) Note: I also have...
0
by: Roy Shaw | last post by:
When configuring PHP5 (5.0.3) to use the mysqli libraries I get a "No such file or directory" from the configure script. My goal is to get PHP5 running with mysql 4.1.09 with both the mysql and...
2
by: ojorus | last post by:
Hi! Some questions regarding the mysqli-extension (php5) 1) Prepared statements: If I understand things right, prepared statements will give better performance if you make several similar...
12
by: davids58 | last post by:
trying to figure out how to use a mysql database with PHP. I ran the following code: <?php // defines database connection data define('DB_HOST', 'localhost'); define('DB_USER', 'ajaxuser');...
2
by: Curtis | last post by:
Hello everyone: Recently, I decided to upgrade to PHP 5.2.0. I have C:\php5 in the Windows XP PATH, so upgrading is quite painless; just unzip new release, and restart Apache! Usually it goes...
13
by: Schmidty | last post by:
If you do a page reload with $_SERVER will your program lose a mysqli connection upon the reload of the page? Would this code work? I need to know how to carry over a connection between methods as...
2
by: Curtis | last post by:
Hello everyone: I have come to love the ease of updating PHP, since getting used to using it these past few years. Recently, however, when I upgraded from PHP 5.1 to PHP 5.2.0 and again when...
21
by: Daz | last post by:
Hi everyone. I am trying to create an extension of the mysqli class within PHP, and I am finding it quite difficult. I am fairly new to PHP classes, and decided to give them a go. Here's what I...
2
by: Michael | last post by:
Hi, I try to use mysqli object instead of standard mysql functions. Is it ok to create mysqli object within my class or schould I pass mysqli object to my object. The problem is, with code...
1
by: Tuyet | last post by:
I am attempting to connect to the database (which was successfully). However, I can not extend the connection to execute other functions. In my example below, the output is not giving me what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.