Note that there are some explanatory texts on larger screens.

plurals
  1. POEmpty MySQL query result in PHP
    primarykey
    data
    text
    <p>Here's the problematic PHP function:</p> <pre><code>//Get data associated with $criteria from db function getUserData($criteria, $value) { //obtain user data from db based on $criteria=$value global $pdo; //echo $criteria . " " . $value; try { $sql = 'SELECT id, first, last, email, userid FROM users WHERE :criteria= :value'; //var_dump($sql); $st = $pdo-&gt;prepare($sql); $st-&gt;bindValue(':criteria', $criteria); $st-&gt;bindValue(':value', $value); $st-&gt;execute(); } catch (PDOException $ex) { $error = "Failed to obtain user data."; $errorDetails = $ex-&gt;getMessage(); include 'error.html.php'; exit(); } $row = $st-&gt;fetch(); //var_dump($row); if ($row) { $userdata = array(); $userdata['id'] = $row['id']; $userdata['first'] = $row['first']; $userdata['last'] = $row['last']; $userdata['email'] = $row['email']; $userdata['userid'] = $row['userid']; return $userdata; } return FALSE; } </code></pre> <p>I use this function to return a whole row of data associated with specific column in it.</p> <p>When used at it's current state, with a call like that <code>getUserData("email", "John_Stewart_2013")</code>, it returns false, meaning an empty result, while the same query runs fine in MySQL CLI.</p> <p>If I, on the other hand, substitute the query string $sql with :</p> <pre><code>$sql = "SELECT id, first, last, email, userid FROM users WHERE $criteria='$value'"; </code></pre> <p>And comment out the bindValue calls, Every thing runs fine in PHP, and the query returns as desired.</p> <p>But the problem is, those function arguments are user-submitted form data, meaning the solution is vulnerable to SQL Injection.</p> <p>What's wrong here in the first query form?</p>
    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.
 

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