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', '')";

    $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', '')
    [R3] => 1



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
answered 7 Months ago

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());

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

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
answered 7 Months ago

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);

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
answered 7 Months ago

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;

You can use:

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

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

Tuesday, June 1, 2021
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 :