1

I am trying to get an AJAX search working, i am very close to this. Here is the php that i am using.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "Products";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $searchValue = $_GET['search'];

  if(isset($searchValue) && $searchValue != ''){
    $search = addslashes($searchValue);
    $statement = $conn->prepare("SELECT ProductName FROM Product WHERE ProductName LIKE('" . $search . "%') ORDER BY ProductName");
    $statement->execute();


    $all = $statement->fetchAll(PDO::FETCH_ASSOC);
    for($i=0; $i<count($all);$i++){
      echo json_encode($all[$i]).ProductName;
    }
  }

}
catch(PDOException $e)
{
  echo "Error: " . $e->getMessage();
}

$conn = null;
?>

The responseText i get is this:

Notice: Use of undefined constant ProductName - assumed 'ProductName' in F:\xampp\htdocs\searchSuggest.php on line 23

{"ProductName":"iMac"}ProductName

Notice: Use of undefined constant ProductName - assumed 'ProductName' in F:\xampp\htdocs\searchSuggest.php on line 23

The only thing i want to display is the "iMac" part of the json object

1
  • Why was this question getting down voted (it's heading back up but was at -2)? It's an inexperienced user with a simple error but what's wrong with that? He provides a code sample, an example of the error and clearly states his problem. There is nothing wrong with that. Commented Dec 21, 2014 at 14:36

2 Answers 2

2

The fix

This line is incorrect:

echo json_encode($all[$i]).ProductName;

It looks like you are trying to get the productName as a property, but the operator for that is ->:

echo json_encode($all[$i])->ProductName;

That line is still incorrect though. The result of json_encode is not an object but a string. The right way to fix it, is to use the array result of fetchAll, and get the product name by the array key:

echo $all[$i]['ProductName'];

The error message

The . operator is for string concatination, so you are trying to concatinate to the json string, the constant ProductName, which is not defined. And that's exactly what the warning says: You are using the undefined constant ProductName, so PHP assumes you meant the constant string 'ProductName' instead.

With an -> it still won't work, though, since json_encode returns a string, not an object. You could json_decode it again, but that's a waste of processing time.

Possible solutions

You seem to be trying to treat the result as an object. Which would be done like this:

$all = $statement->fetchAll(PDO::FETCH_OBJ);
for($i=0; $i<count($all);$i++){
  echo $all[$i]->ProductName;
}

or this:

while ($row = $statement->fetch(PDO::FETCH_OBJ)) {
  echo $row->ProductName;
}
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work this way. json_encode() produces a string not an object.
@axiac Yeah, the description was a bit unclear. I hope you find it better now?
it is telling me that ProductName is an undefined index
0

You probably want:

echo($all[$i]['ProductName']);

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.