Asked  7 Months ago    Answers:  5   Viewed   45 times

I am confused about why I am received the following message:

mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

This question is different from previous queries because my query actually executes correctly (values were indeed entered into the database). So, I would expect MySQL to return a result set and NOT A BOOLIAN.

Below is my function:

function join_main_newsletter($firstName = null, $email)
{
    global $dbc;
    $valuesEntered = "values entered";

    $insert = "INSERT INTO newsletter (first_name, email ) VALUES ('name', 'testemail@yahoo.com')";

    $R3 = mysqli_query($dbc, $insert) or trigger_error("Query Failed! SQL: $sql - Error: " . mysqli_error(db_conx), E_USER_ERROR);
    if (mysqli_num_rows($R3) == 1) {
        return $valueentered;
    } else {
    }
}

And here is part of the results from the error:

[valuesEntered] => values entered
    [insert] => INSERT INTO newsletter (first_name, email ) 
                  VALUES ('name', 'testemail@yahoo.com')
    [R3] => 1

 Answers

13

The mysqli_num_rows() function returns the number of rows in a result set. For insert, update and delete use mysqli_affected_rows

Wednesday, March 31, 2021
 
cbcp
answered 7 Months ago
42

You've mixed procedural and object-oriented MySQLi styles. This has led to you trying to use the functions like mysqli_query($mysqli) instead of the member functions like $mysqli->query(). Your $mysqli is an object, not a resource handle.

And, you're not performing any error checking on your query. If you were, you'd see that you have mistakenly used single quotes to delimit table and field names, not backticks.

$sql = "INSERT INTO `nlcc_ver1`.`tUsers`
       (`userID`, `userName`, `userPassword`, `userHash`,
        `user_first_name`, `user_last_name`, `user_corps`,
        `is_admin`, `is_trg`, `is_sup`, `is_co`)
       VALUES (NULL, '" . $userName . "', '" . $hash . "', '" . $salt . "', '" .
               $f_name . "', '" . $l_name . "', '" . $corps . "', '" . $admin .
               "', '" . $trg . "', '" . $sup . "', '" . $co . "')";

$hostname_Database = "localhost";
$database_Database = "nlcc_ver1";
$username_Database = "root";
$password_Database = "";

$mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database); 
if (mysqli_connect_errno()) {
   printf("Connect failed: %sn", mysqli_connect_error());
   exit();
}

$result = $mysqli->query($sql);
if (!$result) {
   printf("%sn", $mysqli->error);
   exit();
}

echo "Query run. Inserted UserID " . $mysqli->insert_id . "<br />";

I strongly suggest using the manual as your reference. It's quite clear on how to use these functions when you're using either procedural or object-oriented style MySQLi.

Wednesday, March 31, 2021
 
Jesse
answered 7 Months ago
50

I think it should look like this:

$ipaddr = $_SERVER['REMOTE_ADDR'];
if ($stmt = mysqli_prepare($dbconn, 'INSERT INTO visitors(user_email, user_ip) VALUES (?, INET_ATON(?))'))
{
    mysqli_stmt_bind_param($stmt, 'ss', $email, $ipaddr);
    if (mysqli_stmt_execute($stmt) === FALSE) return FALSE;
    $rows_affected = mysqli_stmt_affected_rows($stmt);
    mysqli_stmt_close($stmt);
}

Note the change by the second ? and what parameters are sent into mysqli_stmt_bind_param.

Since the parameters you pass into mysqli_stmt_bind_param should be the actual values to be inserted, and not any functions.

Wednesday, March 31, 2021
 
xrock
answered 7 Months ago
99

The error is fairly vague, but specific. It's stating that a connection object, mysql_result, is expected but it's receiving null instead.

The reason for this is how you're attempting to use the connection inside each function:

global $con;
mysqli_query($con, $q);

The global keyword will bring in global variables to the function you're using it in, but $con isn't a global variable in this class (and, you don't need to use global inside classes). What you're looking for instead is the $this keyword.

In your class constructor MySQLDB() you define the $con variable; you should instead change this to use $this->connection as you've already defined a global variable named $connection:

$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS,DB_NAME);

Then, in each function, instead of using something like:

global $con;
mysqli_query($con,$q);

You can use:

mysqli_query($this->connection, $q);
Saturday, May 29, 2021
 
EastSw
answered 5 Months ago
79

As mentioned in comments, this is a scoping issue. Specifically, $con is not in scope within your getPosts function.

You should pass your connection object in as a dependency, eg

function getPosts(mysqli $con) {
    // etc

I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");

getPosts($con);
Tuesday, June 1, 2021
 
ioleo
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share