0
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','my_db');
  if (mysqli_connect_errno()) {
  echo "Fail to connect :".mysqli_connect_error();
  }

mysqli_select_db($con,"my_db");
$sql="SELECT * FROM ajax WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row('Firstname') . "</td>";
  echo "<td>" . $row('Lastname') . "</td>";
  echo "<td>" . $row('AGE') . "</td>";
  echo "<td>" . $row('Hometown') . "</td>";
  echo "<td>" . $row('Job') . "</td>";
  echo "</tr>";
  echo "</table>";
}
mysqli_close($con);

I keep getting this error :Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\abc\getuser.php on line 22

I apologize in advance if there is already an answer to this simple problem, I am new at coding and I have spent quite some time reading other similar questions but still couldn't solve this problem. Thx in advance.

Vantablack
  • 45
  • 1
  • 9
  • possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Marcin Orlowski Sep 02 '14 at 09:51
  • i think your query is not runnig properly. what is yout id column data type in database? so the $result varaible will always intialized to 0 – Aksh Sep 02 '14 at 09:53

2 Answers2

2

As specified, the argument $result is boolean, not a mysqli_result. In case of error in your SQL request, the function mysqli_query returns false.

You should see why their is a problem with your request by calling mysqli_error just after your mysqli_query if $result is false.

GHugo
  • 2,584
  • 13
  • 14
  • Thank you very much, called mysqli_error and it return "Unknown column 'id' in 'where clause'", turns out there's no column 'id', my column is 'pid'. Solved. Thx. – Vantablack Sep 03 '14 at 00:59
0

you could try

$sql=mysqli_query("SELECT * FROM ajax WHERE id = '".$q."' ");

echo "<table border='1'>
.......

while($row = mysqli_fetch_array($sql)) {
mircea
  • 5
  • 5