I'm new to PHP and MySQL. I am trying to make a simple search form using which I would want to show the results from the database based on the input text entered in the form. My code is like this:
Form.php
<!DOCTYPE html>
<html lang="en">
<html>
<head>
<title>test</title>
</head>
<body>
<form action="search.php" method="GET" id="form">
Name: <input type="text" name="name" >
Age:<input type="text" name="age">
Search<input type="submit" name="submit" id="Search" Value="Search">
</form>
</body>
</html>
Connect.php
<?php
$connect = mysql_connect('localhost','$user','$password');
if(!$connect){
die('Could not connect'.mysql_error() );
}
$db_selected = mysql_select_db('test');
if(!$db_selected){
die('wrong'.mysql_error() );
}
?>
Search.php
<?php
include("includes/connect.php");
$name=$_GET['name'];
echo $name;
$query = "SELECT * FROM `cats` WHERE name='\$name'";
$results= mysql_query($query);
if (!empty($results)){
echo "query successful" ;
exit;
}
$row=mysql_fetch_assoc($results);
echo "Age:".$row['age'];
echo "Name:".$row['name'];
?>
The echo $names
ouputs the result correctly and so does echo "query successful"
.
However the
echo "Age:".$row['age'];
echo "Name:".$row['name'];
only echo's the string part and the query does not seem to fetch any results.
I tried changing the mysql_fetch_asso
c to mysql_fetch_array
, but it does not do anything either. Could anyone tell me what am i doing wrong here. My DB table has two columns and two rows.
$name
!! – Broncha Feb 26 at 19:38