0

I am trying to perform a search action by getting a value from HTML form using PDO and display the results in the same page. I am getting no errors also no results. Please advise! i am beginner.

<?php 
require_once 'db_alternate2.php';
session_start(); 

if (isset($_POST['submit'])) {
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

$payrl_search = trim($_POST['empname_search']);

$sql = "SELECT * FROM payroll_db 
WHERE 'pay_staff_name' 
LIKE '$%payrl_search%'"; 

$q = $conn->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);

} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
}

my php code inside html is

         <table>
         <tbody>
         <?php while ($r = $q->fetch()): ?>
             <tr>
             <td><?php echo htmlspecialchars($r['pay_emp_id'])?></td>
             <td><?php echo htmlspecialchars($r['pay_staff_name']); ?></td>
             <td><?php echo htmlspecialchars($r['pay_month_sal']); ?></td>
             <td><?php echo htmlspecialchars($r['pay_amount']); ?></td>
             <td><?php echo htmlspecialchars($r['pay_bankname']); ?></td>
             </tr>
         <?php endwhile; ?>
         </tbody>
         </table>

I am getting these errors:

  1. Undefined variable: q
  2. Fatal error: Call to a member function fetch() on a non-object

i think my error part is

         <?php while ($r = $q->fetch()): ?>
Franklin Vaz
  • 19
  • 1
  • 2
  • 9
  • The error simply means your query didn't work and is returning a null/false value and false is not an object so check very well and make sure your query evaluates to true – danidee May 10 '15 at 12:35
  • @danidee:_Thanks!_I have double checked my db links and names, everything looks perfect. Although i did a mistake, which i found now `LIKE '$%payrl_search%'` which i've as `LIKE '%$payrl_search%'` . but still the problem remains. Please advise if i can rewrite my query in any other way! – Franklin Vaz May 10 '15 at 13:16
  • remove the quotes for `WHERE 'pay_staff_name'` that's a column. also `'$%payrl_search%'` the `$` is in the wrong spot – Funk Forty Niner May 10 '15 at 13:24
  • @Fred-ii- I tried without quotes and corrected the `$` also.. didn't work – Franklin Vaz May 10 '15 at 13:32
  • is your form using POST and that your element holds the name attribute with no typos lettercase etc? same for your submit button – Funk Forty Niner May 10 '15 at 13:33
  • @Fred-ii- `
    ` is my form tag
    – Franklin Vaz May 10 '15 at 13:38
  • what about your input, is it named? i.e. `name="empname_search"` – Funk Forty Niner May 10 '15 at 13:39
  • @Fred-ii- yes!! i have the same name `` – Franklin Vaz May 10 '15 at 13:42
  • Try changing `$q->setFetchMode(PDO::FETCH_ASSOC);` to `$results = $q->setFetchMode(PDO::FETCH_ASSOC);` and removing `trim` from your POST array while making sure you have something in your table that matches the query. Other than that, I don't know what else could be causing this. Do a `var_dump();` to see what's going through and add error reporting to see if it catches anything else. – Funk Forty Niner May 10 '15 at 13:48
  • I tested your code and was successful with the fixes that were outlined above. Make sure you've chosen the right database and table and that your columns also all exist, while checking for the column types. Plus, what's in here `require_once 'db_alternate2.php';`? Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner May 10 '15 at 13:55
  • I'm guessing your connection variables may have something to do with this. Make sure you've chosen the right db etc. - am moving on, good luck. – Funk Forty Niner May 10 '15 at 14:04
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – andrewsi May 11 '15 at 00:35

0 Answers0