Jump to content

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean


triad1234

Recommended Posts

Let me put this straight.

I have a table : name_table

name varchar(10), meaning1 varchar(10), meaning2 varchar(10) ... meaning20 varchar(10).

 

#Note : The Fields Name & Meaning2 are already Filled.

 

Test.php (URL Passed : localhost/../test.php?name=some_name)

<?php
error_reporting(E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//Get name
$name = $_GET['name'];


$name_meanings = array(sometext1, sometext2, ... sometext19, sometext20);


for($k=1; $k<=20; $k++){
//check if the table cell is empty to be filled
$element_col = "meaning" . $k;
echo "</br>" . $element_col . "</br>";
$check = $conn->query("select $element_col from name_table WHERE name=$name");
if(!$check){
echo "</br> CHECK: False</br>";
$occurance = mysqli_num_rows($check); //Warning message pointing to this line
if($occurance == 0 ){
echo "</br>Occurance is 0</br>";
for($l=0; $l<sizeof($name_meanings); $l++){
$name_element = $name_meanings[$l];
$raw_pwd=$conn->query("insert into name_table($element_col) values($name_element) where name=$name");
}
}
else{
echo "</br>Meaning exists at $element_col</br>";
}
}
else{
echo "</br>Check is True</br>";
break;
}
}
?>
Output:
 
Meaning1
Check: False
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\\test.php on line 26
Occurrence is 0
 
Meaning2  <--This iteration went wrong
Check: False
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\\test.php on line 26
Occurrence is 0
.
.
.
Meaning20
Check: False
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\\test.php on line 26
Occurrence is 0
 
Question:
1. How do I put the warning, mysqli_num_rows() right?
2. Why is the PHP unable to detect the already inserted fields (meaning2)?
Any Suggestion.....Please.   :)
Link to comment
Share on other sites

the reason you are getting errors at the mysqli_num_rows() statement, is because your program logic is not correct and your query is failing due to an error of some kind. the $check variable will be a false value when the query fails due to an error. you are then trying to use that false value in the mysqli_num_rows($check) statement. your program logic should be testing if $check is a true value, without the !.

 

the reason your query is failing, is because you don't have single-quotes around the '$name' variable inside the sql statement.

 

finally, you don't need all that program logic anyway. you should not have a database table laid out like that. the data is not normalized, requiring you to write a ton of program logic to find, insert, update, or delete any of the data. you should instead have one row for each same meaning data item, not columns in a row for each same meaning data item.

Link to comment
Share on other sites

Following mac_gyver's reply, which I completely agree with, something like this is what you should do:

 

name_table:

id int(11) primary key auto increment

name varchar(10)

meaning varchar(10)

 

So it would hold data like:

1 | test | meaning1

2 | test | meaning2

3 | test | meaning3

4 | blah | meaning1

5 | blah | meaning2

6 | name | meaning 1

 

You could prioritise meanings by adding another column or putting them in order by ID.

 

Then just loop through like:

$check = $conn->query("select * from name_table WHERE name='".$conn->escape_string($name)."'");
while($row = $check->fetch_array())
{
 // Stuff here for each meaning
}
Edited by kierany5
Link to comment
Share on other sites

the reason you are getting errors at the mysqli_num_rows() statement, is because your program logic is not correct and your query is failing due to an error of some kind. the $check variable will be a false value when the query fails due to an error. you are then trying to use that false value in the mysqli_num_rows($check) statement. your program logic should be testing if $check is a true value, without the !.

 

the reason your query is failing, is because you don't have single-quotes around the '$name' variable inside the sql statement.

 

finally, you don't need all that program logic anyway. you should not have a database table laid out like that. the data is not normalized, requiring you to write a ton of program logic to find, insert, update, or delete any of the data. you should instead have one row for each same meaning data item, not columns in a row for each same meaning data item.

I agree

if(!check) {



}

That won't work, I would do:

if($check === FALSE) {



}

or 

if($check !== TRUE) {



}
Link to comment
Share on other sites

Also, you are vulnerable to SQL injection

//Get name
$name = $_GET['name'];

$check = $conn->query("select $element_col from name_table WHERE name=$name");

Sanitize the data with functions like real_escape_string, htmlspecialchars and htmlentities because you are also vulnerable to Cross Site Scripting.

 

i.e

//Get name
$name = $_GET['name'];
$name = htmlentities($name, ENT_QUOTES);
$name = htmlspecialchars($name, ENT_QUOTES);
$name = mysqli_real_escape_string($conn, $name);

If you are still getting errors after the advice you have been given then, use var_dump() and post the output on here.

var_dump($check);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.