0

I have that code:

$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

$result = $mysqli->query("SELECT id FROM User");
echo("users: " . $result->num_rows ."<br/>");

function echoUserNum()
{
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}
echoUserNum();

It prints the number of users only once (first echo call). Where is the problem?

0

1 Answer 1

3

$mysqli is out of scope in the function echoUserNum. Try global, or pass the connection

function echoUserNum()
{
    global $mysqli;
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}

or

function echoUserNum($mysqli)
{
    $result = $mysqli->query("SELECT id FROM User");
    echo("users: " . $result->num_rows ."<br/>");
}
echoUserNum($mysqli);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you :) I have no experience with PHP, I was looking at it from JavaScript perspective :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.