Jump to content

Simple search not working right.


osyrys14

Recommended Posts

Hey everyone, 

 

I found a simple search online, and it's acting strange.  If I put anything in the search field, it returns nothing, if I leave it blank, it returns every field in the database.  Code is below if anyone can check... I've been staring at it for over 2 hours trying to find out why... Thanks in advance. 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>


<form method="get" action="">
<label>Search For: </label><input type="text" name="query" />
<input type="submit" name="submit" value="Start Search" />
<input type="reset" value="Reset"
</form>
 
<?php

 
if(isset($_GET['submit'])){
 
$db_host="localhost";
$db_username="myusername";
$db_password="mypass";
$db_name="nydb";
$db_tb_name="mytable";
$db_tb_atr_name="column";
 

 
mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");
 
$query=mysql_real_escape_string($_GET['query']);
 
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE 
 
'8' like '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<li>";
    echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</li><hr/>";
}
echo "</ol>";
 
mysql_close();
}
?>

</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/276199-simple-search-not-working-right/
Share on other sites

 

... WHERE '8' like '%".$query."%'")

 

What are you putting in $query with a hope of getting records returned which match the string '8' ?

 

If you actually have a column named "8" (ridiculous, I know) then it should be in backticks, not quotes.

one more quick question.... 

 

This code is where it's putting the information out, 

 

echo "<li>";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</li><hr/>";

 

How easy is it to add more of the data (since I'm pulling * from database), to the output, and one of them is a number field that we need to add together for the sum of all rows.  I really appreciate your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.