Jump to content

Unknown Printed Charector


j.silver

Recommended Posts

Hi all,

In fetching a result from a table (below code), 1 is also printed after the fetched record (fetched record below). There is no 1 in the table, nor have I accidentally included it in my script. What is it and why does it appear?

 

<?php

require 'db/connect.php';
if($result = $db->query("SELECT * FROM user")) {
if ($count = $result->num_rows) {
$rows = $result->fetch_assoc();
echo '<pre>', print_r($rows), '</pre>';
}
}
?>
Array
(
[id] => 1
[first_name] => Bill
[last_name] => John
[bio] => I'm a web developer
[created] => 2016-11-21 12:50:12
)
1
Link to comment
Share on other sites

This is what happens when people don't read the documentation of a function before using it. Read this page: http://php.net/print_r

 

print_r() prints the content of a variable and returns true. So when you echo the return value of print_r() it displays "1" which it the string representation of true.

 

You can remove the echo statement and the code will still work except it won't add "1" to the output.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...