mattesj mattesj - 4 months ago 6x
PHP Question

SQL query doesn't get values

The SQL query returns just Array in the browser even if there is values in the database. I have tried the query in phpmyadmin and it works but not in my php document.


$query = "SELECT `id` FROM `questions` WHERE round='1' AND year='2016'";
$sql = mysqli_query($dbconnect, $query);

$row = mysqli_fetch_array($sql, MYSQLI_ASSOC);

Almost the same query works in different php documents. Any suggestions what is wrong? Should also say that the query should return integers.

$query = "SELECT `id` FROM `questions` WHERE round='1' AND year='2016'";

You're only selecting the id column. If you wish to echo more columns, then you need to add them in the query.


$query = "SELECT `id`, `col2`, `col3` FROM `questions` WHERE round='1' AND year='2016'";

then loop over results:

while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) { 
    echo $row['id']; 
    // echo "<br>";
    // echo $row['col2'] . "<br>"; 
    // echo $row['col3'];

Check for errors on the query also and assuming a successful mysqli_ connection.

Other reference: