0

This is the code to connect to my database. I am sure the username, password and database name are correct.

$Myconn = mysqli_connect($this->host, $this->user, $this->pass, $this->DBname);

This is code for prepare statement:

$query =$Myconn->prepare("SELECT * FROM `AD` WHERE name=?");
$query->bind_param('s', $AD_Name);
$query->execute();
$query->store_result();
$query->bind_result($id, $name, $price);   

and I am sure that I sent $AD_Name correctly, as well as my query.

I used AMPPS and it was working while using my code.

My problem is that my result is always null when i print $id or $name or $price.

Ray
  • 3,864
  • 7
  • 24
  • 36
Kero Fawzy
  • 690
  • 1
  • 7
  • 19

2 Answers2

2

Ali Rasheed is right that you should use fetch() after doing a bind_result(), but there is a bigger issue here. You cannot use bind_result() with SELECT * .... It will not work properly because bind_result() will not know the order of the selected elements and thus it will not know which variable should get which value. Instead, you should revise to something like:

$query =$Myconn->prepare("SELECT id, name, price FROM `AD` WHERE name=?");
$query->bind_param('s', $AD_Name);
$query->execute();
$query->store_result();
$query->bind_result($id, $name, $price);
$query->fetch();

Substitute the column names as necessary of course.

You can see a good explanation about that here: https://stackoverflow.com/a/18753263/2694511

Community
  • 1
  • 1
Spencer D
  • 3,376
  • 2
  • 27
  • 43
1

After doing

$query->bind_result($id, $name, $price);   

use

$query->fetch();
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31