Jump to content

search?


a2bardeals

Recommended Posts

i am having some trouble searching my database. the db is set up like an address book. name, address, city, state, phone, id(int) *auto_increment*  i created the db then created a php script to populate the db with entries. now i want to search those entries. i tried going this route but it returns a blank page.

of course i have a form page (search.html) that the action is set to srch.php

srch.php:

<font face="arial, helvetica" size="1">
<?

$username = "XXXXXX";
$password = "XXXXXXX";
$database = "aXXXXXXXX";
$server  = "localhost";


@mysql_connect($server,$username,$password);
@mysql_select_db($database);


?>
<?

$result = mysql_query("SELECT name, city, state, id FROM a2_bars WHERE name LIKE '".$_POST['term']."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {


printf('%s<br>%s<br><br>', $row[0], $row[1]);


}

mysql_free_result($result);


?>
Link to comment
Share on other sites

Hi a2bardeals,

Here are a few things I would check.

1. Make sure the method in search.html is set to POST because that is what you are using in your query.
2. If that is set to POST, then you can look at your query. Try putting an actual name in that you know exists in your database.

i.e. 

$result = mysql_query("SELECT name, city, state, id FROM a2_bars WHERE name LIKE 'Bob' "); (or whatever name might exist)

3. If that gives you a result, then the problem lies with your  WHERE name LIKE '".$_POST['term']."'

Try setting a variable to your search term and using it in your query accordingly.

i.e.

$searchcontact = $_POST ['term'];

$result = mysql_query("SELECT name, city, state, id FROM a2_bars WHERE name LIKE '%$searchcontact%' ");


Also, I've found it helpful to put some html on the result page, or print out "ran successfully" after my queries during testing. That way, if the page comes up but there are no results, then you have query problems.  But, if the page is blank it's most likely a syntax error in your php somewhere.  In that case, just check your error logs.


Hope this helps you out!

Jay


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.