PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Fri Oct 14, 2005 9:46 pm
feyd | Please use 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Oct 14, 2005 9:55 pm
your output is outside the result set loop iteration.
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Sat Oct 15, 2005 10:07 pm
I'm sorry but I'm kind of new to this. Can you explain a little more?
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Sat Oct 15, 2005 10:11 pm
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.
hiren
Forum Newbie
Posts: 3 Joined: Sun Oct 16, 2005 4:48 am
Post
by hiren » Sun Oct 16, 2005 5:04 am
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.
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Sun Oct 16, 2005 5:16 am
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.
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Mon Oct 17, 2005 11:01 am
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>";
}
?>
mickd
Forum Contributor
Posts: 397 Joined: Tue Jun 21, 2005 9:05 am
Location: Australia
Post
by mickd » Mon Oct 17, 2005 5:01 pm
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
thelkj
search
search rgrger
efkj
search rgkl
should all match above
kristie380
Forum Commoner
Posts: 36 Joined: Sun Oct 09, 2005 10:51 pm
Post
by kristie380 » Mon Oct 17, 2005 8:52 pm
Thanks! That worked!!