3

I am trying to show values from DB onto a web page. Here is the code below.

//connect db

$dbc = mysqli_connect('localhost', 'root', '', 'mylearndb') OR die('Cannot connect because: '.mysqli_connect_error());

//Run query...

$q = "SELECT * FROM 'pages' WHERE id = 1";
$r = mysqli_query($dbc, $q);

$page = mysqli_fetch_assoc($r);

In the index page, I am trying to print the results in a table:

<table>

    <tr>
        <td>Customer ID</td>
        <td>Full Name</td>
        <td>Email ID</td>
        <td>Phone no</td>
    </tr>

    <tr>
        <td><?php echo $page['cust_id']; ?></td>
        <td><?php echo $page['full_name']; ?></td>
        <td><?php echo $page['email_id']; ?></td>
        <td><?php echo $page['phone_no']; ?></td>
    </tr>

</table>

When I go and check the page, it throws the error...

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in..

The above code runs in a file called setup.php and I call that file into index.php.

Dharman
  • 30,962
  • 25
  • 85
  • 135
hkarthi
  • 35
  • 6

2 Answers2

1

Mostly this error indicates a problem with your query.

Remove quote(') from table name in query.

Write query as follows:

$q = "SELECT * FROM pages WHERE id = 1";
benomatis
  • 5,536
  • 7
  • 36
  • 59
Divyesh Savaliya
  • 2,692
  • 2
  • 18
  • 37
0
$q = "SELECT * FROM pages WHERE id = 1";
$r = mysqli_query($dbc, $q);
if(!$r)
   die("Mysql Query Error"); 
$page = mysqli_fetch_assoc($r);
Subash
  • 232
  • 1
  • 3
  • 12