PythonHelp Posted April 22, 2020 Share Posted April 22, 2020 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/ Share on other sites More sharing options...
requinix Posted April 22, 2020 Share Posted April 22, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577087 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577089 Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 Put this line just before you connect to the mysqli server... mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577090 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 (edited) 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 April 22, 2020 by PythonHelp Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577091 Share on other sites More sharing options...
requinix Posted April 22, 2020 Share Posted April 22, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577092 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577094 Share on other sites More sharing options...
requinix Posted April 22, 2020 Share Posted April 22, 2020 Are you getting an error from mysqli? Or is it from PHP this time? Do you see the "no data found" message? Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577095 Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 "attendance" is a table identifier and should not, therefore, be inside quotes. Quotes are for string literals. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577096 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 (edited) 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 April 22, 2020 by PythonHelp Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577099 Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 Have you ever considered looking at the manual? https://www.php.net/manual/en/class.mysqli-result.php Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577100 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 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> Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577110 Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 It's time to re-read the manual regarding mysqli_fetch_row() Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577113 Share on other sites More sharing options...
PythonHelp Posted April 22, 2020 Author Share Posted April 22, 2020 1 hour ago, Barand said: It's time to re-read the manual regarding mysqli_fetch_row() I have but not sure which section can help me. Guidance needed please. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577117 Share on other sites More sharing options...
Barand Posted April 22, 2020 Share Posted April 22, 2020 The bit that tells you what the function returns, perhaps. Quote Link to comment https://forums.phpfreaks.com/topic/310625-notice-trying-to-get-property-of-non-object-in/#findComment-1577118 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.