PHP MySQL mysqli num rows expects parameter 1 to be mysqli result boolean given

0 votes

I'm trying to Integrate HTML Purifier http://htmlpurifier.org/ to filter my user submitted data and I was wondering how can I fix this problem as I get the following error:-

on line 22: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

line 22 is.

if (mysqli_num_rows($dbc) == 0) {


Here is the php code.

if (isset($_POST['submitted'])) { // Handle the form. 

  require_once '../../htmlpurifier/library/HTMLPurifier.auto.php'; 

$config = HTMLPurifier_Config::createDefault(); 
$config->set('Core.Encoding', 'UTF-8'); // replace with your encoding 
$config->set('HTML.Doctype', 'XHTML 1.0 Strict'); // replace with your doctype 
$purifier = new HTMLPurifier($config); 

$mysqli = mysqli_connect("localhost", "root", "", "sitename"); 
$dbc = mysqli_query($mysqli,"SELECT users.*, profile.* 
                                                    FROM users 
                                                    INNER JOIN contact_info ON contact_info.user_id = users.user_id  WHERE users.user_id=3");

$about_me = mysqli_real_escape_string($mysqli, $purifier->purify($_POST['about_me'])); $interests = mysqli_real_escape_string($mysqli, $purifier->purify($_POST['interests'])); 

if (mysqli_num_rows($dbc) == 0) { 
            $mysqli = mysqli_connect("localhost", "root", "", "sitename"); 
            $dbc = mysqli_query($mysqli,"INSERT INTO profile (user_id, about_me, interests) 
                                                                  VALUES ('$user_id', '$about_me', '$interests')"); 
} 


if ($dbc == TRUE) { 
            $dbc = mysqli_query($mysqli,"UPDATE profile 
                                                                  SET about_me = '$about_me', interests = '$interests' 
                                                                  WHERE user_id = '$user_id'"); 

echo '<p class="changes-saved">Your changes have been saved!</p>'; 

} 

if (!$dbc) {

                  // There was an error...do something about it here... 
                  print mysqli_error($mysqli); 
                return; 
} 

}
Feb 16, 2022 in Others by Rahul
• 9,670 points
4,225 views

1 answer to this question.

0 votes

Your query seems to have an ERROR in it due to the $dbc returning false:-

SELECT users.*, profile.* --You do not join with profile anywhere. 
                                                FROM users 
                                                INNER JOIN contact_info 
                                                ON contact_info.user_id = users.user_id 
                                                WHERE users.user_id=3");


The query either returns no rows or is erroneous, thus FALSE is returned. Alter that to:-
 

if (!$dbc || mysqli_num_rows($dbc) == 0)

Mysqli_num_rows:

Return Values

Returns TRUE on success or FALSE on failure. For SELECT, SHOW, DESCRIBE or EXPLAIN mysqli_query() will return a result object.

answered Feb 16, 2022 by Aditya
• 7,680 points

Related Questions In Others

0 votes
0 answers
0 votes
1 answer
0 votes
2 answers
0 votes
0 answers

Converting feet & inches (e.g., "5 ft 1 in") to decimal feet using VBA or Excel

I have a database with a few ...READ MORE

Nov 2, 2022 in Others by Kithuzzz
• 38,010 points
428 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
9,645 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,240 points
946 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

How to fix "Headers already sent" error in PHP

The functions that send and modify the ...READ MORE

answered Feb 17, 2022 in Others by Aditya
• 7,680 points
6,627 views
0 votes
1 answer

How to redirect to another page using PHP

You could use a function which is ...READ MORE

answered Feb 18, 2022 in Others by Aditya
• 7,680 points
4,687 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP