...

View Full Version : PHP Search Mysql no results found



iadubber
06-01-2011, 05:45 PM
Hello,
This is my first venture into php. I was asked by my employer to make a search page to search a database that contains the fields id, school, name, position, extension, email. I followed a few tuts and I keep getting no results found. Any help would be greatly appreciated! I know I'm connecting to the database correctly in the code. I just didn't want to display that info. thanks!




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<?php

//get data
//$button = $_GET['submit'];

$search = $_GET['search'];


{
if (strlen($search)<=2)
echo "Search term too short.";
else

echo "You searched for <b>$search</b><hr size='1'>";

//connect to database
mysql_connect("","","");
mysql_select_db("dubuque_cms");


//explode search term
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{
//query
$x++;
if ($x==1)
$construct .= "name LIKE '%$search_each%'";
else
$construct .= "OR name LIKE '%$search_each%'";
}

//echo out contstruct

$construct = "SELECT * FROM phonelist WHERE $construct";
$run = mysql_query($construct);

$foundnum = mysql_num_rows &run;

if ($foundnum==0)
{echo "No results found. Search again.";}
else
{
echo "$foundnum result(s) found!<p>";

while ($runrows = mysql_fetch_assoc &run)
{
//get data
$school = $runrows['school'];
$name = $runrows['name'];
$position = $runrows['position'];
$extension = $runrows['extension'];
$email = $runrows['email'];

echo
"<br>$school</b><br>$name</br><br>$position</br><br>$extension</br><br>$email<p>";
}

}

}



?>
</html>




EDIT: I see I forgot two curly brackets...

iadubber
06-01-2011, 05:59 PM
after fixing the curly brackets I am getting

Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home/dubuque/public_html/testing/searchphonelist.php on line 46

Line 46 is...$foundnum = mysql_num_rows (&run);

What's wrong in that line? I deleted the () and now I still get no results found. :(

tangoforce
06-01-2011, 06:16 PM
T_PAAMAYIM_NEKUDOTAYIM is a double colon - :: (http://atlex.nl/voorbeelden/php/T_PAAMAYIM_NEKUDOTAYIM.php)

PHP gets its line numbers confused with some errors. You can't be guaranteed the problem is line 46.

Next you're not using brackets properly:


//Wrong and you should use $ not &
$foundnum = mysql_num_rows &run;

//Correct
$foundnum = mysql_num_rows($run);

//Also wrong - use parenthesis and $
while ($runrows = mysql_fetch_assoc &run)

//Correct
while ($runrows = mysql_fetch_assoc($run))


Next please use [php] tags for your php code - you've seen why above (link in my signature).

Also you might want to check out my styling link too as your indents are a bit excessive. If you use notepad++ you can replace a tab with spaces instead - i have mine set to 3 spaces which works very well and keeps everything visible with no scrolling needed.

iadubber
06-01-2011, 06:30 PM
T_PAAMAYIM_NEKUDOTAYIM is a double colon - :: (http://atlex.nl/voorbeelden/php/T_PAAMAYIM_NEKUDOTAYIM.php)

PHP gets its line numbers confused with some errors. You can't be guaranteed the problem is line 46.

Next you're not using brackets properly:


//Wrong and you should use $ not &
$foundnum = mysql_num_rows &run;

//Correct
$foundnum = mysql_num_rows($run);

//Also wrong - use parenthesis and $
while ($runrows = mysql_fetch_assoc &run)

//Correct
while ($runrows = mysql_fetch_assoc($run))


Next please use [php] tags for your php code - you've seen why above (link in my signature).

Also you might want to check out my styling link too as your indents are a bit excessive. If you use notepad++ you can replace a tab with spaces instead - i have mine set to 3 spaces which works very well and keeps everything visible with no scrolling needed.

DOH! Those & and $ got me! It works now and I thank you so much!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum