1

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.

2
  • why are you escaping $name !! Commented Feb 26, 2013 at 19:38
  • Also, mysql functions are now deprecated and it's time to move on to PDO or mysqli. Commented Feb 26, 2013 at 19:41

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

15 Comments

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.
do you have columns named age and name in cats? The quotes are to surround the value and NOT the column name.
Yes, I have a column named age in my table cats.
It gives me " resource(5) of type (mysql result) ".No idea what that means.
then try var_dump($row);. mysql_query returns a MySQL resource which you are getting.
|
0
$query = "SELECT * FROM `cats` WHERE name='" . $name . "'";

or without concatenation

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

Comments

0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.