Mysqli::query do not work

Hello All,
Nice to meet you!

My website URL is: https://stephaneramael.world/test.php
In my php code I connect to mysql database using the mysqli class and query the Db using the method mysqli::query.

I see only the first of my echo statements confirming that the connection is successful:
Connect successful 0 3

I expect to see also the output of my other echo statements (data ‘first row&column’ taken from my database, followed by the count of rows):
testa 2

I do not use any software.
Here is a sample of my code:

/*connect to Db*/
$mysqli = new mysqli($db_server, $db_user, $db_pass, $db_name);
/* check connection */
if ($mysqli->connect_errno) {
   echo "Connect failed ".$mysqli->connect_error;
   exit();
} else {echo "Connect successful<br>";}

$sql = "SELECT * FROM test";

$exam_aArray = array();

if ($result = $mysqli->query($sql)) {
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        array_push($exam_aArray, $row);
    }
    echo ("pass : " + $exam_aArray[0]['test_a'] + "<br>");
    echo (count($exam_aArray));
}
/* close connection */
$mysqli->close();

the db contains the following:

id | test_a | test_b |test_c
1  | testa   | testb   | testc
2  | testa   | testb   | testc
3  | testa   | testb   | testc

It seems that the query method is not behaving as expected.
Please help!

Try this one?

/*connect to Db*/
$mysqli = new mysqli($db_server, $db_user, $db_pass, $db_name);
/* check connection */
if ($mysqli->connect_errno) {
   echo "Connect failed ".$mysqli->connect_error;
   exit();
} else {echo "Connect successful<br>";}

$sql = "SELECT * FROM test";

$exam_aArray = array();

if ($result = $mysqli->query($sql)) {
    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
    echo ("pass : " + $row['test_a'] + "<br>");
}
echo ($result->num_rows);
}
/* close connection */
$mysqli->close();

Hi BayoDino,

Thank you for responding quickly.
I used your solution but the php still behaves the same.
It displays:
Connect successful
0003
I used Mysql from InfinityFree, so I don’t understand why it does not works correctly.
Instead of the data from the table, the page displays ‘0’.

can you use var_dump($row)?
that may show what’s going on.

4 Likes

Thanks, I managed to see the content of the array and can refer to it!
The data is transferred as expected.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.