triad1234 Posted February 10, 2015 Share Posted February 10, 2015 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 https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/ Share on other sites More sharing options...
kierany5 Posted February 11, 2015 Share Posted February 11, 2015 If $check is false, then there is an issue with your query or MySQLi connection... AND PLEASE, ESCAPE $name BEFORE PUTTING IT IN A QUERY. You've just shown the world that your code may be susceptible to an SQL Injection... (See http://php.net/manual/en/mysqli.real-escape-string.php for the function to use). Link to comment https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/#findComment-1505399 Share on other sites More sharing options...
mac_gyver Posted February 11, 2015 Share Posted February 11, 2015 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 https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/#findComment-1505424 Share on other sites More sharing options...
kierany5 Posted February 11, 2015 Share Posted February 11, 2015 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 } Link to comment https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/#findComment-1505437 Share on other sites More sharing options...
Tom10 Posted February 11, 2015 Share Posted February 11, 2015 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 https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/#findComment-1505459 Share on other sites More sharing options...
Tom10 Posted February 11, 2015 Share Posted February 11, 2015 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 https://forums.phpfreaks.com/topic/294513-warning-mysqli_num_rows-expects-parameter-1-to-be-mysqli_result-boolean/#findComment-1505460 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.