OAFC_Rob Posted August 8, 2011 Share Posted August 8, 2011 I'm stuck on a problem that i've not encountered before. A couple of weeks ago my PC died meaning I had to install a newer version of XAMMP onto my laptop, and this is the first time this message has popped up any ideas. I get the following error message; Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\innovationation\commonResources\dbConnection\dbConnection.php on line 38 It firstly started off as saying that I was passing it a string through when it shouldn't so I broke the SQL down a bit more to the basic level, and then this one happened if($errors==0 && isset($_POST['contactSubmitted'])) {#OPEN IF INSERT & SEND EMAIL $sql = " INSERT INTO tbl_contact_log ( fName, surname, contactNum, email, comments ) VALUES ( 'test', 'testing', '01610000000', 'rr@gmail.com', 'this is a test' ) "; #die("<br/><br/>".$sql); $results = $database->sqlQuery($sql); if($database->affectedRows($results)>0) { echo " <p> YEAH BITCH! </p> "; } public function sqlQuery($sql) { #NEED TO ADD EXTRA FAIL SAFES IF TABLE DOESN'T EXIST $result = mysql_query($sql, $this->dbConnection) OR trigger_error(mysql_error($sql),E_USER_ERROR); if(mysql_num_rows($result)>0) { $this->confirmResult($result); return $result; } else { $msg = "Sorry but there seems to be an no data for this query<br/><br/>". $sql; return $msg; } } public function escapeValue($value) { $magic_quotes_active = get_magic_quotes_gpc(); $new_enough_php = function_exists("mysql_real_escape_string"); //i.e. PHP >= v4.3.0 if($new_enough_php) { //PHP v4.3.0 or higher, undo any magic quote effects so mysql_real_espace_string can do the work if($magic_quotes_active) { $value = stripslashes($value); } $value = mysql_real_escape_string($value); } else { //before PHP v4.3.0, if magic quotes aren't already on the add slashes manually if(!$magic_quotes_active) { $value = addslashes($value); } //if magic quotes are active, then the slashes already exist } return $value; } public function affectedRows() { return mysql_affected_rows($this->dbConnection); } Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/ Share on other sites More sharing options...
The Little Guy Posted August 8, 2011 Share Posted August 8, 2011 you are getting that because you have an invalid mysql query. echo out the query or the mysql error, and that should give you your answer. B.T.W. The pound comment type is deprecated. Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254500 Share on other sites More sharing options...
Maq Posted August 8, 2011 Share Posted August 8, 2011 As TLG said, that error usually indicates your query is returning false, which means your query is failing. When you setup your new PC did you import all the previous databases? Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254501 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2011 Share Posted August 8, 2011 mysql_num_rows() is only usable for queries that return a result set (SELECT and SHOW queries.) You are executing an INSERT query. Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254502 Share on other sites More sharing options...
OAFC_Rob Posted August 8, 2011 Author Share Posted August 8, 2011 I've just checked the database and it has been inserting into, despite the error message I have also just inserted the died out SQL into a program which inserted with no problems, I have also changed the sqlQuery in the db connection class to this public function sqlQuery($sql) { #NEED TO ADD EXTRA FAIL SAFES IF TABLE DOESN'T EXIST $result = mysql_query($sql, $this->dbConnection) OR trigger_error(mysql_error($sql),E_USER_ERROR); $this->confirmResult($result); return $result; #if(mysql_num_rows($result)>0) #{ # $this->confirmResult($result); # return $result; #} #else #{ # $msg = "Sorry but there seems to be an no data for this query<br/><br/>". $sql; # return $msg; #} } But still no luck as I am no getting the following; Warning: mysql_affected_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\innovationation\commonResources\dbConnection\dbConnection.php on line 107 which relates to this bit public function affectedRows($result) { return mysql_affected_rows($result); } Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254506 Share on other sites More sharing options...
PFMaBiSmAd Posted August 8, 2011 Share Posted August 8, 2011 mysql_affected_rows takes the db link as a parameter, which is what you had in the code posted in the first post in this thread. Why did you change your code to use the result resource in the mysql_affected_rows statement? Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254512 Share on other sites More sharing options...
OAFC_Rob Posted August 8, 2011 Author Share Posted August 8, 2011 I have no idea now, my brain is fried, it is 21:00 here and ive been coding since 08:30, ive changed it back and it seems to be working. How can I improve upon this? public function sqlQuery($sql) { #NEED TO ADD EXTRA FAIL SAFES IF TABLE DOESN'T EXIST $result = mysql_query($sql, $this->dbConnection) OR trigger_error(mysql_error($sql),E_USER_ERROR); $this->confirmResult($result); return $result; #if(mysql_num_rows($result)>0) #{ # $this->confirmResult($result); # return $result; #} #else #{ # $msg = "Sorry but there seems to be an no data for this query<br/><br/>". $sql; # return $msg; #} } I'm soo tired, going to stop soon and go to bed to start it all over again Quote Link to comment https://forums.phpfreaks.com/topic/244254-warning-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given-in/#findComment-1254514 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.