Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO bindParam with $_POST not working
    primarykey
    data
    text
    <p>I've been searching and searching and trying all kinds of stuff and I just can't get this working right. Can anyone see what I'm doing wrong? I'm very new to PDO and trying to figure that out with plenty of other things.</p> <p>I can get the following to work IF I manually submit a value instead of trying to bind it, but I want to use the placeholder. I've gotten a value of 'Array' back, sometimes MySQL responds with :name causing invalid syntax... I've tried rearranging the values for bind and I just can't get it to give me back the value. I have an insert portion of this and that works fine, but I'm messing something up here and the query itself.</p> <p>I appreciate any direction you can help me with. This is driving me crazy:</p> <p>NOTE: Since this is only a test, all the db contains is name and phone columns (will expand as I get past these obsticles).</p> <pre><code> &lt;?php # VARs $host = "MYHOST"; $db = "MYDB"; $user = "MYUSER"; $pw = "MYPW"; # pdo options/attributes $opts = array( PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION ); # data source name $dsn = "mysql:host=" . $host . ";dbname=" . $db; ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h3&gt;Test&lt;/h3&gt; &lt;p&gt;Pull data using PDO&lt;/p&gt; &lt;form method="POST" action="test.php"&gt;&lt;input type="text" name="name"&gt;&lt;input type="submit" value="Search"&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt; &lt;hr /&gt; &lt;? try { $DBH = new PDO($dsn, $user, $pw, $opts); # $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $STH = $DBH-&gt;query('SELECT name, phone FROM directory WHERE name LIKE :name'); $STH-&gt;bindParam(':name', $_POST['name']); $STH-&gt;setFetchMode(PDO::FETCH_ASSOC); while($row = $STH-&gt;fetch()) { echo $row['name'] . "\n" . $row['phone'] . "&lt;br /&gt;"; } } catch(PDOException $e) { echo "I'm sorry, Dave. I'm afraid I can't do that.&lt;br /&gt;"; echo $e-&gt;getMessage(); # file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND); } ?&gt; &lt;hr /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>- - - - - - - - - - - - - RESOLVED ANSWER - - - - - - - - - - - - -</strong></p> <p><strong>- - - - - - - - - - - - - RESOLVED ANSWER - - - - - - - - - - - - -</strong></p> <p>Thanks to the responses below, here is the correction to my query and pdo layout:</p> <pre><code> &lt;? try { $DBH = new PDO($dsn, $user, $pw, $opts); # $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); ####-------Changed query to prepare $STH = $DBH-&gt;prepare('SELECT name, phone FROM directory WHERE name LIKE :name'); ####-------using bindValue instead of bindParam ####-------also using % for wildcards to help with LIKE query (would only give specific search back without) $STH-&gt;bindValue(':name', '%' . $_POST['name'] . '%'); ####-------was missing execute (had query above instead of prepare) $STH-&gt;execute(); $STH-&gt;setFetchMode(PDO::FETCH_ASSOC); while($row = $STH-&gt;fetch()) { echo $row['name'] . "\n" . $row['phone'] . "&lt;br /&gt;"; } } catch(PDOException $e) { echo "I'm sorry, Dave. I'm afraid I can't do that.&lt;br /&gt;"; echo $e-&gt;getMessage(); # file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND); } ?&gt; </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