Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting PHP query to PDO query
    primarykey
    data
    text
    <p>I have some php that is receiving a variable from jquery and querying the DB. I recently learned that I need to use PDO to prevent SQL Injections and such so I have been trying to convert my query to it. I am new at php anyway so this is turning out to be more difficult than I thought (even though all the articles I read looked quite straightforward)...The DB connection is working and <code>'name'</code> is receiving the right value but it is not updating the page like it used to. I am guessing it has to do with my loop that contains the <code>json_encode</code>. Below is my old php and then my attempt at turning it into PDO format.</p> <p>Old PHP:</p> <pre><code>$dbstylename = $_POST['name']; $result = mysql_query("SELECT * FROM style where stylename like '$dbstylename'"); $array = mysql_fetch_row($result); echo json_encode($array); mysql_close($con); ?&gt; </code></pre> <p>New PDO attempt:</p> <pre><code>&lt;?php include 'db.php'; try { $dbConnection = new PDO('mysql:host=$dbhost;dbname=$dbhost;', $user, $pass); $dbConnection-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbConnection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); #Prepare the query $dbstylename = $_POST['name']; $result = $dbConnection-&gt;prepare('SELECT * FROM style where stylename like :dbstylename'); #bind $result-&gt;bindParam(':dbstylename', $dbstylename, PDO::PARAM_STR); #execute if ($result-&gt;execute(array($dbstylename))) { while ($row = $result-&gt;fetch()) { json_encode($row); } } } catch(PDOException $e) { echo 'ERROR: ' . $e-&gt;getMessage(); } ?&gt; </code></pre> <p>================UPDATE==============================</p> <p>In addition to @MadaraUchiha great answer and follow up help I had to change my jQuery from this (which worked before PDO):</p> <pre><code>$.ajax({ url: '../test.php', type: 'POST', data: {'name' : target}, dataType: 'json', success: function(data) { var styleid = data[0]; var stylename = data[1]; var stylecss = data[2]; $('#codeTest').html("&lt;b&gt;id: &lt;/b&gt;&lt;br /&gt;"+styleid+"&lt;br /&gt;&lt;b&gt; stylename: &lt;/b&gt;&lt;br /&gt;"+stylename+"&lt;br /&gt;&lt;b&gt; stylecss: &lt;/b&gt;&lt;br /&gt;"+stylecss); } }); </code></pre> <p>To this:</p> <pre><code>$.ajax({ url: '../test.php', type: 'POST', data: {'name' : target}, dataType: 'json', success: function(data) { var styleid = data.styleid; var stylename = data.stylename; var stylecss = data.stylecss; $('#codeTest').html("&lt;b&gt;id: &lt;/b&gt;&lt;br /&gt;"+styleid+"&lt;br /&gt;&lt;b&gt; stylename: &lt;/b&gt;&lt;br /&gt;"+stylename+"&lt;br /&gt;&lt;b&gt; stylecss: &lt;/b&gt;&lt;br /&gt;"+stylecss); } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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