Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & MySQL: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given [duplicate]

Tags:

php

mysql

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

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; }  } 
like image 401
TaG Avatar asked Mar 30 '10 15:03

TaG


People also ask

What PHP stand for?

PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym." PHP executes on the server, while a comparable alternative, JavaScript, executes on the client.

Is PHP used anymore?

PHP is known to be the most frequently used programming language. According to W3Techs, 78.8% of all websites are using PHP for their server-side. Interesting fact: PHP originally stood for Personal Home Page. Now PHP is widely known and thought of as Hypertext Preprocessor.


2 Answers

$dbc is returning false. Your query has an error in it:

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 fix for this in general has been described by Raveren.

like image 158
Sean Vieira Avatar answered Sep 18 '22 02:09

Sean Vieira


The query either returned no rows or is erroneus, thus FALSE is returned. Change it 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.

like image 39
raveren Avatar answered Sep 20 '22 02:09

raveren