Reputation: 45
So I can search the whole database but i need a way to change the $db_tb_atr_name="location" to all columns. I need to search for any keyword and return the whole row. Right now all that returns is the location. I do not know how to change my code to make this happen.
<?php
if(isset($_GET['submit'])){
$db_host="localhost";
$db_username="megan";
$db_password="megan";
$db_name="megan";
$db_tb_name="user";
$db_tb_atr_name="location";
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
CONCAT(id_user,fname,lname,location,email,phone_number,username) 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();
}
?>
If i change the $db_tb_atr_name to $db_tb_name it wont work at all
Upvotes: 1
Views: 222
Reputation: 14245
Don't do it that way. Try something like this:
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE CONCAT(id_user,fname,lname,location,email,phone_number,username) LIKE '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_assoc($query_for_result))
{
echo "<li>";
echo substr(implode(",", $data_fetch), 0,160);
echo "</li><hr/>";
}
echo "</ol>";
I switched your mysql_fetch_array, to mysql_fetch_assoc. Now you can have a more organized and associate response.
Also, I imploded your $data_fetch, which means it is joining your different columns together, separated by columns.
We can always change it to how you need it, but your desired output wasnt detailed.
You could also do something like this:
$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE CONCAT(id_user,fname,lname,location,email,phone_number,username) LIKE '%".$query."%'");
echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_assoc($query_for_result))
{
echo "<li>";
foreach($data_fetch as $row => $value){
echo "Row: $row - $value<br />";
}
echo "</li><hr/>";
}
Upvotes: 1