Connection is successful. No typo's were made (look below). If read it from top to bottom, some of the tests that I show you will seem stupid. Until you look at the end (still keep reading from top to bottom) and find out that it's one of the indicators.

SQL command SELECT * FROM forums, results in the only two rows in forums being shows. 2 rows, 8 columns, exactly as it should be.

Database has been connected. PHP says:

$result = mysqli_query($dbconn, "SELECT * FROM forums");
var_dump($result);

Brings:

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(8) ["lengths"]=> NULL ["num_rows"]=> int(2) ["type"]=> int(0) }

Especially this part: ["num_rows"]=> int(2).

Next line:

$result = mysqli_fetch_row($result);
var_dump($result);

Results in only first row being shown:

array(8) { [0]=> string(1) "1" [1]=> string(11) "Primo Forum" [2]=> string(23) "Primo Forum Descriptini" [3]=> string(3) "F00" [4]=> string(1) "0" [5]=> string(11) "11Exampleur" [6]=> string(4) "1234" [7]=> string(4) "9991" }

2nd row missing, even though they're next to each other, in same table, matches query, and is being found by the SELECT query, but... doesn't show.

Now for the weirdest stuff. Saying:

$result = mysqli_query($dbconn, "SELECT * FROM forums");
$result = mysqli_fetch_row($result);

foreach($result as $data) {
    echo $data["name"];
}

Gives:

Warning: Illegal string offset 'name' in C:\###\index.php on line 21
1
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
P
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
P
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
F
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
0
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
1
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
1
Warning: Illegal string offset 'name' in C:\###\index.php on line 21
9

What you see after every warning. Is the very first character of the first row of every column.

Dumping the table creates this:

INSERT INTO `forums` (`id`, `name`, `description`, `color`, `visibilityRequirement`, `lastTopicAuthor`, `lastTopicId`, `topicCount`) VALUES
(1, 'Primo Forum', 'Primo Forum Descriptini', 'F00', 0, '11Exampleur', 1234, 9991),
(2, 'Dual Forumo', 'Dual Forumo Descriptini', '0F0', 0, '22Exampleur', 2345, 9992);

Ask what you need for this solution.

Recommended Answers

All 6 Replies

Firstly, this line only showing one row:
$result = mysqli_fetch_row($result);
var_dump($result);

You've only asked it to fetch one row so the dump only has one row. mysqli_fetch_row does just what it says i.e. it returns ONE row.
For the second problem, the var_dump of your row above shows what's wrong there. 'name' isn't a key in the array, everything is index based.
echo $row[1]; // will outut the name column

Firstly, this line only showing one row: You've only asked it to fetch one row so the dump only has one row. mysqli_fetch_row does just what it says i.e. it returns ONE row.

mysqli_fetch_assoc doesn't solve it either. mysqli_fetch_rows doesn't exist. How can I fetch all the results? I remember using this some time ago, which doesn't work now, that means that I'm pretty close to the ultimate solution. mysqli_fetch_array results in only first row.

To add:

For the second problem, the var_dump of your row above shows what's wrong there. 'name' isn't a key in the array, everything is index based.

How come first character of every row is getting outputted? I don't have any other printing running.

I worked it out:

if ($var1 = mysqli_query($databaseconn, "YOUR_SQL_QUERY")) {
    while ($row = mysqli_fetch_assoc($var1)) {
        // $row is associative array.
    }
}

Little note: consider that the query() method will return an iterable object from the Result class:

So, if you want to get the next row you have to move the internal pointer, through a loop or through data_seek(), which should be used if, for example, you want to loop the result set again, see:

In order to get the full result set, you could use mysqli_fetch_all(), but this is available only if you're using the mysqlnd driver (MySQL Native Driver), which should be the default setting for PHP 5.4 and above:

With PHP 5.3 and lower, the default is the libmysqlclient.

commented: Good post. mysqlnd driver has been my bugbear on a number of hosts who should have known better! +15

Impressive post and thanks for answering it in a detail manner.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.