Jump to content

Why will it not show my search results?


colleyboy

Recommended Posts

I am trying to make a script which connects to the database and searches within the database table and brings back matches.  I have used two files.  Search.php (for the form) and srch.php (the php that does the work).  Could somebody please tell me where I am going wrong?  It just keeps returning the "Please enter search string" all the time. boo.

 

Search.php:

<html> 
<head><title>Searching the Guest Book</title> 
</head> 
<body bgcolor=#ffffff> 
<h1>Searching the Database</h1> 
<form method="post" action="srch.php"> 
<table width=90% align=center> 
<tr><td>search for:</td><td><input type=text name='search' size=60 maxlength=255></td></tr> 
<td></td><td><input type=submit></td></tr> 
</table> 
</form>  
</body> 
</html> 

 

srch.php:

 

<? 

include("dbinfo.inc.php");


if ($search) // perform search only if a string was entered. 
{ 
mysql_connect(localhost,$username,$password) or die ("Problem connecting to Database"); 

$srch="%".$search."%"; 
$query = "select * from contacts WHERE first LIKE '$srch' || last LIKE '$srch' || phone LIKE '$srch' || mobile LIKE '$srch' || fax LIKE '$srch || email LIKE '$srch'  || web LIKE '$srch'"; 

$result = mysql_db_query("contacts", $query); 

if ($result) 
{ 
echo "Here are the results:<br><br>"; 
echo "<table width=90% align=center border=1><tr> 
<td align=center bgcolor=#00FFFF>First Name</td> 
<td align=center bgcolor=#00FFFF>Last Name</td> 
<td align=center bgcolor=#00FFFF>Phone Number</td> 
<td align=center bgcolor=#00FFFF>Mobile Number</td> 
<td align=center bgcolor=#00FFFF>fax</td>
<td align=center bgcolor=#00FFFF>Email</td>
<td align=center bgcolor=#00FFFF>Web</td>
</tr>"; 

while ($r = mysql_fetch_array($result)) { // Begin while 
$first = $r["first"]; 
$last = $r["last"]; 
$phone = $r["phone"]; 
$mobile = $r["mobile"]; 
$fax = $r["fax"];
$email = $r["email"];
$web = $r["web"]; 
echo "<tr> 
<td>$first</td> 
<td>$last</td> 
<td>$phone</td> 
<td>$mobile</td>
<td>$fax</td>
<td>$email</td>
<td>$web</td></tr> 
<tr> <td colspan=4 bgcolor=\"#ffffa0\">$comment</td> 
</tr>"; 
} // end while 
echo "</table>"; 
} else { echo "problems...."; } 
} else { 
echo "Search string is empty. <br> Go back and type a string to search"; 
}  
?> 

Many thanks if anyone can help me. =D

 

Link to comment
Share on other sites

That indicates the query is failing. Change the $result line to this and see if you don't get an error . . .

$result = mysql_db_query("contacts", $query) or die('<br />This query string cratered: ' . $query . '<br />With this error: ' . mysql_error() );

 

Link to comment
Share on other sites

Did not work.  Instead it through back this error message at me:

 

This query string cratered: select * from contacts WHERE first LIKE '%ian%' || last LIKE '%ian%' || phone LIKE '%ian%' || mobile LIKE '%ian%' || fax LIKE '%ian% || email LIKE '%ian%' || web LIKE '%ian%'

With this error: Access denied for user 'wormste1_stock'@'localhost' to database 'contacts'

 

Just to make sure.

 

My database name is: wormste1_stock

table within database name: contacts

 

-----

 

I did try to change to "contacts" name to "wormste1_stock" but when i searched "ian" in the box and pressed submit it through this error at me:

 

 

This query string cratered: select * from contacts WHERE first LIKE '%ian%' || last LIKE '%ian%' || phone LIKE '%ian%' || mobile LIKE '%ian%' || fax LIKE '%ian% || email LIKE '%ian%' || web LIKE '%ian%'

With this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%ian%' || web LIKE '%ian%'' at line 1

 

----

More help needed I think as I seem well out of my depth :(

Link to comment
Share on other sites

Well "Problems..." isn't very helpful is it? Why don't you actually display the error (at least during the developement phase) so you can know what to fix? You query is failing because it is misformatted. You should echo the query and the error to the page.

 

Here is a more logic format for the page - plus it will show you the errors needed to correct.

<?php

include("dbinfo.inc.php");

$HTMLoutput = '';
$search = trim($_POST['search']);
if (empty($search))
{
    //No search value was entered
    $HTMLoutput .= "Search string is empty.<br />Go back and type a string to search."; 
}
else
{ 
    //Perform the database query
    mysql_connect(localhost,$username,$password) or die ("Problem connecting to Database"); 
    $srch="%{$search}%"; 
    $query = "SELECT first, last, phone, mobile, fax, email, web, comment
              FROM contacts
              WHERE first LIKE '$srch' OR last LIKE '$srch' OR phone LIKE '$srch'
                 OR mobile LIKE '$srch' OR fax LIKE '$srch OR email LIKE '$srch'
                 OR web LIKE '$srch'"; 
    $result = mysql_db_query("contacts", $query); 
    if (!$result)
    {
        //There was an error in running the query
        $HTMLoutput .= "problems....<br /><br />";
        //The following line is for debugging only and
        //can be commented out once in production.
        $HTMLoutput .= "<b>Query:</b><br />{$query}<br /><b>Error:</b><br />" . mysql_error();
    }
    elseif(mysql_num_rows($result)<1)
    {
        //No results from query
        $HTMLoutput .= "There were no matches.";
    }
    else
    {
        //Query ran successfully and had results
        $HTMLoutput .= "Here are the results:<br><br>"; 
        $HTMLoutput .= "<table width=\"90%\" align=\"center\" border=\"1\">\n";
        $HTMLoutput .= "<tr>\n";
        $HTMLoutput .= "<th>First Name</th>\n";
        $HTMLoutput .= "<th>Last Name</th>\n";
        $HTMLoutput .= "<th>Phone Number</th>\n";
        $HTMLoutput .= "<th>Mobile Number</th>\n";
        $HTMLoutput .= "<th>fax</th>\n";
        $HTMLoutput .= "<th>Email</th>\n";
        $HTMLoutput .= "<th>Web</th>\n";
        $HTMLoutput .= "</tr>\n"; 

        while ($r = mysql_fetch_array($result))
        {
            $HTMLoutput .= "<tr>\n";
            $HTMLoutput .= "<td>{$r['first']}</td>\n";
            $HTMLoutput .= "<td>{$r['last']}</td>\n";
            $HTMLoutput .= "<td>{$r['phone']}</td>\n";
            $HTMLoutput .= "<td>{$r['mobile']}</td>\n";
            $HTMLoutput .= "<td>{$r['fax']}</td>\n";
            $HTMLoutput .= "<td>{$r['email']}</td>\n";
            $HTMLoutput .= "<td>{$r['web']}</td>\n";
            $HTMLoutput .= "</tr>\n";
            $HTMLoutput .= "<tr><td colspan=\"7\" bgcolor=\"#ffffa0\">{$r['comment']}</td></tr>\n"; 
        }
        $HTMLoutput .= "</table>\n"; 
    } 
}  
?>
<html>
<head>
<style>
th { text-align: center; background-color: #00FFFF; }
</style>
</head>
<body>
<?php echo $HTMLoutput; ?>
</body>
</html>

Link to comment
Share on other sites

Just tryed that and I am getting exactly the same problems appearing as in my post before this.

 

It is better to have it neater and i should have problems explained and not hidden as i am in experimental phase lol.

 

I dont get whats going wrong though :(.

Link to comment
Share on other sites

I was pointing out the missing quote because it's also missing in the code you wrote. if I implied the existing code should be used instead, I didn't mean to.

No worries. I did notice that I copied the error. I did fix a lot of minor HTML formatting errors in the original code and provided a more logical flow. So, I hope my time wasn't wasted. But, no matter, the OP will do what he will.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.