Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting MySQL connector to PDO
    text
    copied!<p>After taking some advice from people on here in a previous thread, I'm trying to convert my MySQL to PDO, but am running into some issues.</p> <p>Here is my original MySQL connection class:</p> <pre><code>class DbConnector { public static function getInstance() { static $instance = null; if ($instance === null) { $instance = new DbConnector(); } return $instance; } protected $theQuery; private $link; function DbConnector() { $host = 'localhost'; $db = ''; $user = ''; $pass = ''; // connect to the db $this-&gt;link = mysql_connect($host, $user, $pass); mysql_select_db($db); register_shutdown_function(array(&amp;$this, 'close')); } public function find($query) { $ret = mysql_query($query, $this-&gt;link); if (mysql_num_rows($ret) == 0) return array(); $retArray = array(); while ($row = mysql_fetch_array($ret)) $retArray[] = $row; return $retArray; } public function insert($query) { $ret = mysql_query($query, $this-&gt;link); if (mysql_affected_rows() &lt; 1) return false; return true; } public function query($query) { $this-&gt;theQuery = $query; return mysql_query($query, $this-&gt;link); } public function fetchArray($result) { return mysql_fetch_array($result); } public function close() { mysql_close($this-&gt;link); } public function exists($query) { $ret = mysql_query($query, $this-&gt;link); if (mysql_num_rows($ret) == 0) return false; } public function last_id($query) { return mysql_insert_id($query); } } </code></pre> <p>Here is the function that I'm writing:</p> <pre><code>function getRandomSubmission() { global $db; if(!empty($_GET['id'])){ $submission_id = $_GET['id']; $query = $db-&gt;find(" SELECT * FROM `submissions` WHERE id = '{$submission_id}' LIMIT 1 "); } else { $query = $db-&gt;find(" SELECT * FROM `submissions` ORDER BY RAND() LIMIT 1 "); } if($query) { return $query[0]; } else { $query = $db-&gt;find(" SELECT * FROM `submissions` ORDER BY RAND() LIMIT 1 "); } } </code></pre> <p>Here is the PDO connector:</p> <pre><code>$host = 'localhost'; $username = ''; $pass = ''; $db = ''; try { $dbh = new PDO("mysql:host=$host;dbname=$db", $username, $pass); } catch (PDOException $e) { echo $e-&gt;getMessage(); } </code></pre> <p>Here is what I've tried to convert it to, but it's just plain wrong. I think I need to be returning a PDO associative array in the 2nd if statement, but am not sure.</p> <pre><code>function getRandomSubmission() { global $dbh; if(!empty($_GET['id'])){ $submission_id = $_GET['id']; $stmt = $dbh-&gt;prepare(' SELECT * FROM `submissions` WHERE `id` = ? LIMIT 1 '); $stmt-&gt;bindParam(1, $submission_id, PDO::PARAM_INT); $stmt-&gt;execute(); } else { $stmt = $dbh-&gt;prepare(' SELECT * FROM `submissions` ORDER BY RAND() LIMIT 1 '); $stmt-&gt;execute(); } if($stmt) { return $stmt[0]; } else { $stmt = $dbh-&gt;prepare(' SELECT * FROM `submissions` ORDER BY RAND() LIMIT 1 '); $stmt-&gt;execute(); } } </code></pre> <p>The original one works as intended, however (I realize I left the connection details blank).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload