0

Could someone debug this and tell me why is is not working?

<?php
include("C:\Wamp\www\system\db\connect.php");
$term = mysqli_real_escape_string($con, $_GET['q']);
echo "results for \"".$term."\".<br>";
$sql = "SELECT * FROM `search` WHERE Keywords='%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) or die("<p color=\"#f00\">Could not fetch assoc array in database.</p>")) {
    echo $row['Title'];
}
echo json_encode($row['Title']);

mysqli_close($con);
?>

It stops working at the mysqli_fetch_assoc function.

Krii
  • 907
  • 9
  • 23

3 Answers3

0

As mentioned in comments, you should print actual mysql error message instead of customized messages, it will help you in debugging the error,

Here are some suggestions to fix your bug,

Your code should be:

<?php
include("C:\Wamp\www\system\db\connect.php"); //<-- You should give relative path instead of this one
$term = mysqli_real_escape_string($con, $_GET['q']);
echo "results for \"".$term."\".<br>";
$sql = "SELECT * FROM `search` WHERE Keywords like '%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die(mysqli_error($con)); //<-- show mysql error instead of custom one
while($row = mysqli_fetch_assoc($result) ) {
    echo $row['Title'];
}
echo json_encode($row['Title']);

mysqli_close($con);
?>
Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
  • I used the path because using a relative one returns `failed to open stream: No such file or directory` but I have been to the directory in the browser. – Krii Mar 23 '15 at 10:12
  • `", "", "") or die("

    Could not connect to database.

    "); ?>`
    – Krii Mar 23 '15 at 10:26
0

Replace these lines:

$sql = "SELECT * FROM `search` WHERE Keywords='%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) or die("<p color=\"#f00\">Could not fetch assoc array in database.</p>")) {
   echo $row['Title'];
}

with these:

$sql = "SELECT * FROM `search` WHERE Keywords LIKE '%{$term}%' LIMIT 10";
$result = mysqli_query($con, $sql) or die("<p color=\"#f00\">Could not query database.</p>");
while($row = mysqli_fetch_assoc($result) ) {
   echo $row['Title'];
}
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
0

I had the same issue on a simple table with one value, I added the die error messages and it was still showing nothing for me, try to echo one specific row of your table, for me it did show a result, so I figured my problem was in the encode as json. So i used the solution found in json encoding returning empty string As for the echo of your specific row, I tried this:

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

if($result->num_rows > 0){
     while($row= mysqli_fetch_assoc($result)){
        echo($row['name_ofRow']);
   }
}

I hope this will help some how.