-1

I am trying to delete a row from my database. When I execute my PHP script I am getting the error message:

Notice: Trying to get property 'num_rows' of non-object in /home/send.php on line 46.

Does someone know what is wrong with my script?

Line 46 = if ($result10->num_rows > 0) {

Here is the PHP script which I am using to delete the row:

$link10 = mysqli_connect("localhost", "root", "password", "database");

if($link10 === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10) ;

$result10 = $link10->query($sql10);

if ($result10->num_rows > 0) {
//script

}
John
  • 904
  • 8
  • 22
  • 56
  • 1
    This is nearly a duplicate of https://stackoverflow.com/questions/54975043/in-php-why-doesnt-mysqli-num-rows-return-an-integer-for-a-query-with-0-returne/ and the solution is the same -- your `query()` returned **false** because of some error in your SQL query, and you need to check for that before you try to access `num_rows`. See my answer to that question for a full explanation. – Bill Karwin Mar 05 '19 at 21:35
  • There is nothing wrong in my sql query – John Mar 09 '19 at 00:40

3 Answers3

1

You are already executing your query here:

$res10 = mysqli_query($link10, $sql10);

So you can access to the num_rows using:

if(mysqli_num_rows($res10) > 0)

And remove this line:

$result10 = $link10->query($sql10);

Full code:

$link10 = mysqli_connect("localhost", "root", "password", "database");

if($link10 === false) {
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10);

if(mysqli_num_rows($res10) > 0) {
//script
}
ALFA
  • 1,726
  • 1
  • 10
  • 19
  • Now I get the message: `Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 45` (line 45 = if(mysqli_num_rows($res10) > 0) {) – John Mar 05 '19 at 22:15
  • It looks like that your query is failing and returning false. First thing it's always better to check if query is returning true before checking the number of rows. So `if($res10 && mysqli_num_rows($res10) > 0)`. Secondly check your query if it returns something. – ALFA Mar 06 '19 at 07:27
  • The query should work. I get exactly the same error message with: `if($res10 && mysqli_num_rows($res10) > 0)` – John Mar 09 '19 at 00:40
  • Check if `if($res10)` gives true or false. – ALFA Mar 11 '19 at 07:29
1

Here is what you will do

if ($res10->num_rows > 0) {
//script

}

Try this below. I have commented the code that you do not needed

$link10 = mysqli_connect("localhost", "root", "password", "database");

if($link10 === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
$res10 = mysqli_query($link10, $sql10) ;

//$result10 = $link10->query($sql10);

if ($res10->num_rows > 0) {
//script

}

Updated section

<?php
   $dbhost = 'localhost';
   $dbuser = 'root';
   $dbpass = 'password';
   $dbname = 'database';
   $conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

   if(! $conn ) {
      die('Could not connect: ' . mysqli_error());
   }
   echo 'Connected successfully<br>';

$sql = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
   $result = mysqli_query($conn, $sql);

   if (mysqli_num_rows($result) > 0) {
     echo "deleted successfully";
   } else {
      echo " results cannot be deleted";
   }
   mysqli_close($conn);
?>
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • I still get `Notice: Trying to get property 'num_rows' of non-object in ` – John Mar 05 '19 at 22:14
  • Please see updated section of my answer for a new solution – Nancy Moore Mar 05 '19 at 22:28
  • Now I get `Connected successfully; Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in line 50` (Line 50 = if (mysqli_num_rows($result) > 0) {) – John Mar 05 '19 at 23:17
  • Then problem should be from this line of code $sql = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'"; secondly, you will need to try this with direct value. something like $sql = ' DELETE FROM statement WHERE id = "1" and number="200"'; or you can try this below Lets make it simple Try this $sid = $_SESSION['id']; $no = $_GET['number']; $sql = " DELETE FROM statement WHERE id= '$sid' AND number='$no'"; – Nancy Moore Mar 06 '19 at 00:12
  • I tried it with direct value. I still get the same error message. When I execute the script directly in my mysql database the statement is deleting the rows – John Mar 09 '19 at 00:27
  • The solution I provide is not up 20 line of code so why the error is occurring at line 50. My solution does not cause any error. its working on my system. what causes the error is the additional code you wrote which I did not have knowledge of. so don't come here and tell me that. Again you have over 50 line of code and you are just posted less than 10 lines. Are you hiding your code or what?. Hmm I can see that most id used in the queries are session based so also ensure that session is set on that page eg session_start(); Finally post all ur codes and tables if you really need help – Nancy Moore Mar 09 '19 at 07:14
1

The error message means that $result10 is not an object, therefore you can't use it as an object

Try this:

$link10 = mysqli_connect("localhost", "root", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql10 = "DELETE FROM statement WHERE id = '". $_SESSION['id'] ."' AND number = '". $_GET['number'] ."'";
mysqli_query($link10, $sql10) ;

if (mysqli_affected_rows($link10) > 0) {
  //script
}

Here is a similar example on W3C

toh19
  • 1,083
  • 10
  • 21
  • It looks like the script is not executed. I dont get a error message. The rows are still in my database. The `//script` part of my script is executed – John Mar 05 '19 at 22:12
  • @John - If the connection to your database is done with no error then the problem might me your sql query. Are you sure that there is a field `statement.id` that equals your `$_SESSION['id']` and a number equals `$_GET['number']` in your statement table? – toh19 Mar 06 '19 at 19:37
  • @John - I tested the code with a test database and it works fine – toh19 Mar 06 '19 at 19:38
  • Are you sure the script deleted the rows? In my case the script is not executed. I tried it with a direct value but the rows are still there. – John Mar 09 '19 at 00:26