Naps Posted July 17, 2012 Share Posted July 17, 2012 Hello, I am still quite new to php and am currently working with mysqli to update a database. When passing data from one script to another in order to update the database I am having the following error printed occur: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in add_news.php on line 32 the code on line 32 is: $num = mysqli_num_rows($r); The database however does update fine with content I provide. I just can't figure out why I am getting this message every time even though the database updates ok. The reason I am running the code $num = mysqli_num_rows($r); is to enable to php code to verify that the database has been updated via the returned row. Could anyone explain why this is happening? Please find my code below: <? require_once ('../mysqli_connect.php'); include ('header.html'); if (isset ($_POST['title']) && (isset ($_POST['content']) && (isset ($_POST['date'])))) { $title = mysqli_real_escape_string($dbc, $_POST['title']); $content = mysqli_real_escape_string($dbc, $_POST['content']); $date = mysqli_real_escape_string($dbc, $_POST['date']); } else { echo "You have accessed this in error"; include ('footer.html'); exit; } $q = "INSERT INTO content VALUES (NULL,'$title', '$content', '$date')"; $r = mysqli_query($dbc, $q); $num = mysqli_num_rows($r); if ($num > 0) { echo '<h2>News Added<h2>'; } else { echo '<h2>Error</h2>'; echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } include ('footer.html'); ?> Edit: I should add that the database consists of the following 4 columns: 1. id (auto increment) 2. title 3. content 4. date Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/ Share on other sites More sharing options...
mikosiko Posted July 17, 2012 Share Posted July 17, 2012 from the manual: mysqli_query() Return Values Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE. and the error message that you are getting just confirm exactly that Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in add_news.php on line 32 mysqli_num_rows() Return Values Returns number of rows in the result set. most likely you want to use mysqli_affected_rows() http://www.php.net/manual/en/mysqli.affected-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/#findComment-1362281 Share on other sites More sharing options...
jazzman1 Posted July 17, 2012 Share Posted July 17, 2012 Before query statemet ( $q = "INSERT INTO content VALUES (NULL,'$title', '$content', '$date')" ), add the code below and give us a result. <?php $x = array($title, $content, $date); echo '<pre>'.var_dump($x, true). '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/#findComment-1362288 Share on other sites More sharing options...
Barand Posted July 17, 2012 Share Posted July 17, 2012 @jazzman1 If you bothered to read mikosiko's reply you would have noticed that the problem had been identified. Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/#findComment-1362308 Share on other sites More sharing options...
jazzman1 Posted July 18, 2012 Share Posted July 18, 2012 The database however does update fine with content I provide....... @Barand, you're right. Sorry, about the extra post. Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/#findComment-1362317 Share on other sites More sharing options...
Naps Posted July 18, 2012 Author Share Posted July 18, 2012 Hi all, thank you for the replies. @mikosiko many thanks for your info and suggestion! I have successfully amended the code as per your link and advice. Line 32 has now been amended to: $num = mysqli_affected_rows($dbc); Quote Link to comment https://forums.phpfreaks.com/topic/265860-mysqli-insert-problem/#findComment-1362513 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.