Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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_assoc 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.

share|improve this question
why are you escaping $name !! – Broncha Feb 26 at 19:38
Also, mysql functions are now deprecated and it's time to move on to PDO or mysqli. – webnoob Feb 26 at 19:41

3 Answers

up vote 2 down vote accepted

You're escaping the $ in the variable by doing \$. Try:

$query = "SELECT * FROM `cats` WHERE name='$name'";

EDIT

From the discussion below.

The problem with the undefined index is the fact that you are using $row['age'] when really, the column name in the database is Age. Therefore you must use $row['Age'] when referring to the item. The same goes for name.

share|improve this answer
Thanks.I tried the query suggested by you. It now gives me "Notice: Undefined index: name and Notice: Undefined index: age " error msg. But if I add qoutes around name ($query = "SELECT * FROM cats WHERE 'name'='$name'";) the error goes away but still the query does not fetch any results. – user2112593 Feb 26 at 20:17
do you have columns named age and name in cats? The quotes are to surround the value and NOT the column name. – UnholyRanger Feb 26 at 20:18
Yes, I have a column named age in my table cats. – user2112593 Feb 26 at 20:22
do var_dump($results); – UnholyRanger Feb 26 at 20:23
It gives me " resource(5) of type (mysql result) ".No idea what that means. – user2112593 Feb 26 at 20:28
show 12 more comments
$query = "SELECT * FROM `cats` WHERE name='" . $name . "'";

or without concatenation

$query = "SELECT * FROM `cats` WHERE name='$name'";
share|improve this answer

This should work:

$query = "SELECT * FROM cats WHERE name = '$name'";

Also, when you call "exit;" in the following block it will cancel the execution of the rest of your script:

    if (!empty($results)){
     echo "query successful" ;
     exit;
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.