Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

What am I doing wrong with this search query

I'm using PDO and I'm still wet behind the ears so, here we go:

Here's my search query:

<table width=100% align="center">
					<?php
					include("carter_pdo.inc");
					//Prepare a select statment with 'named' parameters
					$stmt = $db->prepare("SELECT * FROM registration where stage_name LIKE :search");
							
					//setup the data to be passed as the named parameter
					$data = array(
						'search' => trim($_POST['search'])
					);
					//execute the query along with the data
					$stmt->execute($data);

					//if a result was returned (i.e there was a match) then assign that record to $row
					if ($row = $stmt->fetch(PDO::FETCH_OBJ)) 
					{
					?>
					<tr>
					<td class="body"><b>Contestants</b>
					</td>
					</tr>
					<?php
					while ($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
					{
					?>
					<tr>
					<td class="body">
					<A HREF="contestant.php?id=<?php echo $row['id']; ?>" target="_blank" class="body"><?php echo stripslashes($row['stage_name']); ?></a>
					</td>
					</tr>
					<?php
					}
					}
					?>
					</table>	

Open in new window


No errors, but no results.

In other words, there's a row in the database that corresponds to my search query, so I'm getting the table and the "Contestants" header, but no name and no contestant id.

What am I doing wrong?
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Yo, Gary!

I changed this:

while ($row=$stmt->fetch(PDO::FETCH_ASSOC))
{

to this:

foreach($row)
{

and I got a parse error on that line. What do I need to do differently?
				foreach($row as $item) {
					?>
					<tr>
					<td class="body">
					<A HREF="contestant.php?id=<?=$item['id']; ?>" target="_blank" class="body"><?=stripslashes($item['stage_name']); ?></a>
					</td>
					</tr>
					<?php
					}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First off, everybody, thanks for your feedback. Online tutorials are a huge resource, but having access to guys who can give specific answers to specific questions is invaluable!

Chris, I went with your solution, although I couldn't get the HEREDOC thing to work. But rather than try to figure that out, I went with your model and put a $stmt->execute($data);       just before the "while" on line 8 and I began weeping tears of joy as I saw my results listed on my page.

Thanks!

And, Ray... I'm reading your articles / tutorials and I'm trying to make you proud, friend.
Bruce - please you got it working...

If you come across something that 'doesn't work', a good approach to debugging is to make sure you have all errors being reported properly by PHP, use print_r() and var_dump() to see what your script is doing, and keep the php.net website open, so you can look up the definitive page for any given function. In your case, if you don't get any errors then var_dump($output) to see if the html was assigned to the variable.

Here's the link to HEREDOC - it's part of the 'strings' page

http://php.net/manual/en/language.types.string.php

You'll see a huge warning in the HEREDOC section about the closing identifier. It must be on it's own line, with no indents!

Good luck with it