Page 1 of 1

search form problem

Posted: Fri Oct 14, 2005 9:46 pm
by kristie380
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am trying to get my PHP script to display multiple search results for a keyword that a user inputs into my search form.  So far I can only get it to display 1 record (whatever the last record entered was).  Can someone tell me what I need to add to my PHP script to get it to display more than one search record? 

Here is my PHP code:

Code: Select all

<?php
$dbHost = 'mysql';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'alumni';

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!$db_selected) { die('Error : ' . mysql_error()); }

$sql = "SELECT * FROM signup";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{
$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$email = $newArray['email'];
$url = $newArray['url'];
$update = $newArray['update'];

 }
?>

<hr width="60%" size="1" color="#006633" align="left">
<font color="#006633" size="4"><b><?php echo "$firstname $maidenname $lastname";?></b></font><p>
<font color="#006633" size="3"><?php echo "$update/n";?></font>

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Fri Oct 14, 2005 9:55 pm
by feyd
your output is outside the result set loop iteration.

Posted: Sat Oct 15, 2005 10:07 pm
by kristie380
I'm sorry but I'm kind of new to this. Can you explain a little more?

Posted: Sat Oct 15, 2005 10:11 pm
by mickd
he means that because the display of the output

Code: Select all

<hr width="60%" size="1" color="#006633" align="left"> 
<font color="#006633" size="4"><b><?php echo "$firstname $maidenname $lastname";?></b></font><p> 
<font color="#006633" size="3"><?php echo "$update/n";?></font>
is not inside the loop, when the loop finishes, it will only use the last loop values for that.

to solve put the above code inside the loop so that it does it once for every record.

Same problem with me

Posted: Sun Oct 16, 2005 5:04 am
by hiren
I am facing same problem and I tried to search for it in so many forums.

But his is the best forum I found for php, I loved it so much.

Please solve my problem so that I can solve ur problem.

Posted: Sun Oct 16, 2005 5:16 am
by mickd
if you are facing the same problem make sure your not doing what he did wrong by looking at my above post. if thats not it, posting your code might help.

Posted: Mon Oct 17, 2005 11:01 am
by kristie380
Ok so when I place my code inside the loop and perform a search, it pulls up all of the records in my database rather than pulling up only the records that meet my search criteria. Any suggestions? Here is my new code:

Code: Select all

<?php
$dbHost = 'mysql';
$dbUser = 'username';
$dbPass = 'password';
$dbname = 'alumni';

$db = mysql_connect($dbHost,$dbUser,$dbPass);
$db_selected = mysql_select_db($dbname,$db);
if (!$db_selected) { die('Error : ' . mysql_error()); }

$sql = "SELECT * FROM signup";
$result = mysql_query($sql,$db);

while ($newArray = mysql_fetch_array($result))
{

$firstname = $newArray['firstname'];
$maidenname = $newArray['maidenname'];
$lastname = $newArray['lastname'];
$email = $newArray['email'];
$url = $newArray['url'];
$update = $newArray['update'];


echo "<hr width=\"60%\" color=\"#006633\" size=\"1\" align=\"left\"><font color=\"#006633\" size=\"4\"><b>$firstname $maidenname $lastname </b></font><p><font color=\"#006633\" size=\"3\">$update</font><p>";

 }
?>

Posted: Mon Oct 17, 2005 5:01 pm
by mickd
you need the query to have the criteria in it to limit the return for example

Code: Select all

"SELECT * FROM signup WHERE username LIKE '%search%'"
the % is a wildcard thatll match anything for example

thelkjsearch
searchrgrger
efkjsearchrgkl

should all match above

Thanks

Posted: Mon Oct 17, 2005 8:52 pm
by kristie380
Thanks! That worked!!