Jump to content

Notice: Trying to get property of non-object in


PythonHelp

Recommended Posts

I am trying to run a count query and faced with the error: Notice: Trying to get property of non-object in

Not sure how to resolve this error in these lines:

 

if($result->num_rows > 0){
while($row = $result->fetch_rows()){

Code:

<?php

$sql = "SELECT 
    Form_Group
    COUNT(Presence LIKE '/') AS Present,
    COUNT(Presence LIKE 'N') AS Absent,
FROM 'attendance'";
$result = $conn->query($sql);

?>

<?php
//Fetch Data form database
if($result->num_rows > 0){
while($row = $result->fetch_rows()){
?>


<tr>
<td><?php echo $row['Form_Group']; ?></td>
<td><?php echo $row['Present']; ?></td>
<td><?php echo $row['Absent']; ?></td>
</tr>

<?php
}
}
else
{
?>
<tr>
<th colspan="6">There's No data found!!!</th>
</tr>
<?php
}
?>

 

Link to comment
Share on other sites

6 minutes ago, requinix said:

Your query failed to run. Get an error message from mysqli to see what the server thinks is wrong.

Or spend a couple minutes looking closely at the query. That might do it too.

Error message is: Notice: Trying to get property of non-object in \\.........\reports_attendance.php on line ...

I looked very closely at the query for a very long time and just cannot find the problem,. It worked well before using the case with just the one count, when the second count went in,  it showed this error.

I am new to PHP so any guidance or tutorial to help will be greatly appreciated.

Link to comment
Share on other sites

17 minutes ago, Barand said:

Put this line just before you connect to the mysqli server...


mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

 

Thank you Barand. The mysql server is showing the following error:

Error 1:

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(Presence LIKE '/') AS Present, COUNT(Presence LIKE 'N') AS Absent, F' at line 3'

Error 2:

Line 47:

$result = $conn->query($sql);

 

i tried to follow the MariaDB syntax but to no success:

 

$sql = "SELECT 
    Form_Group
    COUNT(DISTINCT Presence LIKE '/') AS Present,
    COUNT(DISTINCT Presence LIKE 'N') AS Absent,
FROM 'attendance'";
$result = $conn->query($sql);

 

Edited by PythonHelp
Link to comment
Share on other sites

17 minutes ago, PythonHelp said:

i tried to follow the MariaDB syntax but to no success:

You need to learn how to read error messages from the software you want to use.

Syntax error messages from MySQL (and forks) will show you where in your query the problem was detected. That almost always means what you need to do is look at that spot, or perhaps slightly before, to see what's wrong.

Link to comment
Share on other sites

14 minutes ago, requinix said:

You need to learn how to read error messages from the software you want to use.

Syntax error messages from MySQL (and forks) will show you where in your query the problem was detected. That almost always means what you need to do is look at that spot, or perhaps slightly before, to see what's wrong.

Oh ok, so I fixed some foolish errors (missing commas etc) *embarassed*

There now seems to  be this final error but I just cant seem to spot what it is - near attendance

$sql = "SELECT 
    Form_Group,
    COUNT(Presence LIKE '/') AS Present,
    COUNT(Presence LIKE 'N') AS Absent FROM 'attendance'";
$result = $conn->query($sql);

?>

 

 

Link to comment
Share on other sites

2 hours ago, requinix said:

Are you getting an error from mysqli? Or is it from PHP this time? Do you see the "no data found" message?

I believe it is a mysql error , after removing the  ' ' around the table name, I am getting: 

Fatal error: Call to undefined method mysqli_result::fetch_rows() in \\143FF240DC.STORAGE-1B.HOSTING.STACKCP.NET\SITES\1b\1\143ff240dc\public_html\Progress_Tracker\Pages\reports_attendance.php on line 65

 

2 hours ago, Barand said:

"attendance" is a table identifier and should not, therefore, be inside quotes. Quotes are for string literals.

After removing the single quotes, I am getting:

 

Fatal error: Call to undefined method mysqli_result::fetch_rows() in \\143FF240DC.STORAGE-1B.HOSTING.STACKCP.NET\SITES\1b\1\143ff240dc\public_html\Progress_Tracker\Pages\reports_attendance.php on line 65

 

Edited by PythonHelp
Link to comment
Share on other sites

4 hours ago, Barand said:

Have you ever considered looking at the manual?

https://www.php.net/manual/en/class.mysqli-result.php

Thank you so much, with your support, I am learning as well as becoming a step closer to the end result.

So, after looking at the PHP manual, I have altered the script , but now it is producing the error ""Notice: Undefined index for the following lines:

 

<td><?php echo $row['Form_Group']; ?></td>
<td><?php echo $row['Present']; ?></td>
<td><?php echo $row['Absent']; ?></td>

 

Here is the entire code

 

<?php

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL);


?>

<?php

mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);

?>



<?php

//Connection for database

$conn = mysqli_connect("xxx", "xxxxxxxxxx", "xxxxxxxxxx", "xxxxxxx");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  exit();
}


$sql = "SELECT 
    Form_Group,
    COUNT(Presence LIKE '/') AS Present,
    COUNT(Presence LIKE 'N') AS Absent FROM attendance";
?>


<!doctype html>
<html>
<body>
<h1 align="center">Summary</h1>
<table border="1" align="center" style="line-height:25px;">
<tr>
<th>Form</th>
<th>Present</th>
<th>Absent</th>
</tr>
<?php
//Fetch Data form database
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_row($result)) {

?>

<tr>
<td><?php echo $row['Form_Group']; ?></td>
<td><?php echo $row['Present']; ?></td>
<td><?php echo $row['Absent']; ?></td>
</tr>

<?php

  }
  mysqli_free_result($result);
}

mysqli_close($conn);


?>

</table>
</body>
</html>

 

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.