Notice: Trying to get property 'num_rows' of non-object (how to trouble-shoot SQL statement?)









up vote
-1
down vote

favorite












I've seen some threads on Stack Overflow with this error in the title, and all the answers basically say the same thing: there's something wrong with your SQL query. Now, I'm new to SQL and I'm really not getting any errors from my page other than that one. How can I trouble-shoot my SQL statement?



SELECT COUNT(*) FROM `rating` AS x, avg(`rating`) AS dep_avg WHERE department='IG'


PHP code:



<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Averages</title>
<link rel="stylesheet" href="css/master.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="pb">
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Change View</h5>
<p class="card-text">Currently, you are viewing overall values. Click the link below to view monthly rates (rates are measured 1 - 5 one being the worst).</p>
<a href="wavg.php" class="card-link">View Monthly Rates</a>
<a href="index.php" class="card-link">Take Survey</a>
</div>
</div>
</div>
</div>
<?php
require 'db.php';

$rot = 0;
$deps = array("cutting", "tempering", "IG", "bars", "fabrication", "paint", "shipping");
while ($rot < 6)
$cur_dep = $deps[$rot];

$sql = "SELECT COUNT(*) AS x, avg(`rating`) AS dep_avg WHERE department='$cur_dep'";
$result = $con->query($sql);
if ($result->num_rows > 0)
if ($row["dep_avg"] < 3.0)
echo '
<div class="alert alert-danger sp" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-down"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';
else
echo '
<div class="alert alert-success" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-up"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';


if (!$result)
trigger_error('Invalid query: ' . $con->error);

else
echo "<div class='alert alert-primary'><h1 class='alert-heading'>No data</h1></div>";


$rot++;

?>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Issues?</h5>
<p class="card-text">If there are any issues (parse errors, database errors, etc.) please contact Will Hoffman below.</p>
<a href="https://willwam.com/contact" target="_blank" class="card-link">Contact</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">View Git Repo</h5>
<p class="card-text">You can view the Git repo below for an always up-to-date version.</p>
<a href="https://github.com/WillWammer/tristar-survey" target="_blank" class="card-link">View Repo</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Creator</h5>
<p class="card-text">See the blog, see the Patreon page</p>
<a href="https://willwam.com/" target="_blank" class="card-link">Blog</a>
<a href="https://patreon.com/WillWam" target="_blank" class="card-link">Patreon</a>
</div>
</div>
</div>
</body>
</html>


Error:



#1064 - 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 'WHERE department='IG'' at line 1









share|improve this question



















  • 1




    Theres's no num_rows in sql query, so please can you add php code too?
    – TrueTiem
    Nov 10 at 19:58











  • Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
    – RiggsFolly
    Nov 10 at 20:04










  • @TrueTiem, sorry I forgot
    – Widdles
    Nov 10 at 20:06






  • 1




    simple test: run the query in phpMyAdmin and see if it works or creates an error
    – RiggsFolly
    Nov 10 at 20:06










  • And the complete and unsummarised error message as well please
    – RiggsFolly
    Nov 10 at 20:09















up vote
-1
down vote

favorite












I've seen some threads on Stack Overflow with this error in the title, and all the answers basically say the same thing: there's something wrong with your SQL query. Now, I'm new to SQL and I'm really not getting any errors from my page other than that one. How can I trouble-shoot my SQL statement?



SELECT COUNT(*) FROM `rating` AS x, avg(`rating`) AS dep_avg WHERE department='IG'


PHP code:



<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Averages</title>
<link rel="stylesheet" href="css/master.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="pb">
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Change View</h5>
<p class="card-text">Currently, you are viewing overall values. Click the link below to view monthly rates (rates are measured 1 - 5 one being the worst).</p>
<a href="wavg.php" class="card-link">View Monthly Rates</a>
<a href="index.php" class="card-link">Take Survey</a>
</div>
</div>
</div>
</div>
<?php
require 'db.php';

$rot = 0;
$deps = array("cutting", "tempering", "IG", "bars", "fabrication", "paint", "shipping");
while ($rot < 6)
$cur_dep = $deps[$rot];

$sql = "SELECT COUNT(*) AS x, avg(`rating`) AS dep_avg WHERE department='$cur_dep'";
$result = $con->query($sql);
if ($result->num_rows > 0)
if ($row["dep_avg"] < 3.0)
echo '
<div class="alert alert-danger sp" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-down"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';
else
echo '
<div class="alert alert-success" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-up"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';


if (!$result)
trigger_error('Invalid query: ' . $con->error);

else
echo "<div class='alert alert-primary'><h1 class='alert-heading'>No data</h1></div>";


$rot++;

?>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Issues?</h5>
<p class="card-text">If there are any issues (parse errors, database errors, etc.) please contact Will Hoffman below.</p>
<a href="https://willwam.com/contact" target="_blank" class="card-link">Contact</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">View Git Repo</h5>
<p class="card-text">You can view the Git repo below for an always up-to-date version.</p>
<a href="https://github.com/WillWammer/tristar-survey" target="_blank" class="card-link">View Repo</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Creator</h5>
<p class="card-text">See the blog, see the Patreon page</p>
<a href="https://willwam.com/" target="_blank" class="card-link">Blog</a>
<a href="https://patreon.com/WillWam" target="_blank" class="card-link">Patreon</a>
</div>
</div>
</div>
</body>
</html>


Error:



#1064 - 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 'WHERE department='IG'' at line 1









share|improve this question



















  • 1




    Theres's no num_rows in sql query, so please can you add php code too?
    – TrueTiem
    Nov 10 at 19:58











  • Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
    – RiggsFolly
    Nov 10 at 20:04










  • @TrueTiem, sorry I forgot
    – Widdles
    Nov 10 at 20:06






  • 1




    simple test: run the query in phpMyAdmin and see if it works or creates an error
    – RiggsFolly
    Nov 10 at 20:06










  • And the complete and unsummarised error message as well please
    – RiggsFolly
    Nov 10 at 20:09













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I've seen some threads on Stack Overflow with this error in the title, and all the answers basically say the same thing: there's something wrong with your SQL query. Now, I'm new to SQL and I'm really not getting any errors from my page other than that one. How can I trouble-shoot my SQL statement?



SELECT COUNT(*) FROM `rating` AS x, avg(`rating`) AS dep_avg WHERE department='IG'


PHP code:



<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Averages</title>
<link rel="stylesheet" href="css/master.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="pb">
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Change View</h5>
<p class="card-text">Currently, you are viewing overall values. Click the link below to view monthly rates (rates are measured 1 - 5 one being the worst).</p>
<a href="wavg.php" class="card-link">View Monthly Rates</a>
<a href="index.php" class="card-link">Take Survey</a>
</div>
</div>
</div>
</div>
<?php
require 'db.php';

$rot = 0;
$deps = array("cutting", "tempering", "IG", "bars", "fabrication", "paint", "shipping");
while ($rot < 6)
$cur_dep = $deps[$rot];

$sql = "SELECT COUNT(*) AS x, avg(`rating`) AS dep_avg WHERE department='$cur_dep'";
$result = $con->query($sql);
if ($result->num_rows > 0)
if ($row["dep_avg"] < 3.0)
echo '
<div class="alert alert-danger sp" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-down"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';
else
echo '
<div class="alert alert-success" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-up"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';


if (!$result)
trigger_error('Invalid query: ' . $con->error);

else
echo "<div class='alert alert-primary'><h1 class='alert-heading'>No data</h1></div>";


$rot++;

?>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Issues?</h5>
<p class="card-text">If there are any issues (parse errors, database errors, etc.) please contact Will Hoffman below.</p>
<a href="https://willwam.com/contact" target="_blank" class="card-link">Contact</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">View Git Repo</h5>
<p class="card-text">You can view the Git repo below for an always up-to-date version.</p>
<a href="https://github.com/WillWammer/tristar-survey" target="_blank" class="card-link">View Repo</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Creator</h5>
<p class="card-text">See the blog, see the Patreon page</p>
<a href="https://willwam.com/" target="_blank" class="card-link">Blog</a>
<a href="https://patreon.com/WillWam" target="_blank" class="card-link">Patreon</a>
</div>
</div>
</div>
</body>
</html>


Error:



#1064 - 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 'WHERE department='IG'' at line 1









share|improve this question















I've seen some threads on Stack Overflow with this error in the title, and all the answers basically say the same thing: there's something wrong with your SQL query. Now, I'm new to SQL and I'm really not getting any errors from my page other than that one. How can I trouble-shoot my SQL statement?



SELECT COUNT(*) FROM `rating` AS x, avg(`rating`) AS dep_avg WHERE department='IG'


PHP code:



<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Averages</title>
<link rel="stylesheet" href="css/master.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<body>
<div class="pb">
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Change View</h5>
<p class="card-text">Currently, you are viewing overall values. Click the link below to view monthly rates (rates are measured 1 - 5 one being the worst).</p>
<a href="wavg.php" class="card-link">View Monthly Rates</a>
<a href="index.php" class="card-link">Take Survey</a>
</div>
</div>
</div>
</div>
<?php
require 'db.php';

$rot = 0;
$deps = array("cutting", "tempering", "IG", "bars", "fabrication", "paint", "shipping");
while ($rot < 6)
$cur_dep = $deps[$rot];

$sql = "SELECT COUNT(*) AS x, avg(`rating`) AS dep_avg WHERE department='$cur_dep'";
$result = $con->query($sql);
if ($result->num_rows > 0)
if ($row["dep_avg"] < 3.0)
echo '
<div class="alert alert-danger sp" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-down"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';
else
echo '
<div class="alert alert-success" role="alert" class="sp">
<h1 class="alert-heading">Shipping Average <i class="fas fa-arrow-up"></i></h1>
<p class="mb-0">This department has been rated '.$row["x"].' times</p>
<p class="mb-0">The average rating is '.$row["dep_avg"].'</p>
</div>
';


if (!$result)
trigger_error('Invalid query: ' . $con->error);

else
echo "<div class='alert alert-primary'><h1 class='alert-heading'>No data</h1></div>";


$rot++;

?>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Issues?</h5>
<p class="card-text">If there are any issues (parse errors, database errors, etc.) please contact Will Hoffman below.</p>
<a href="https://willwam.com/contact" target="_blank" class="card-link">Contact</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">View Git Repo</h5>
<p class="card-text">You can view the Git repo below for an always up-to-date version.</p>
<a href="https://github.com/WillWammer/tristar-survey" target="_blank" class="card-link">View Repo</a>
</div>
</div>
</div>
<div class="pb">
<div class="card" style="width:100%;">
<div class="card-body">
<h5 class="card-title">Creator</h5>
<p class="card-text">See the blog, see the Patreon page</p>
<a href="https://willwam.com/" target="_blank" class="card-link">Blog</a>
<a href="https://patreon.com/WillWam" target="_blank" class="card-link">Patreon</a>
</div>
</div>
</div>
</body>
</html>


Error:



#1064 - 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 'WHERE department='IG'' at line 1






php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 8:03









halfer

14.2k757105




14.2k757105










asked Nov 10 at 19:53









Widdles

11




11







  • 1




    Theres's no num_rows in sql query, so please can you add php code too?
    – TrueTiem
    Nov 10 at 19:58











  • Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
    – RiggsFolly
    Nov 10 at 20:04










  • @TrueTiem, sorry I forgot
    – Widdles
    Nov 10 at 20:06






  • 1




    simple test: run the query in phpMyAdmin and see if it works or creates an error
    – RiggsFolly
    Nov 10 at 20:06










  • And the complete and unsummarised error message as well please
    – RiggsFolly
    Nov 10 at 20:09













  • 1




    Theres's no num_rows in sql query, so please can you add php code too?
    – TrueTiem
    Nov 10 at 19:58











  • Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
    – RiggsFolly
    Nov 10 at 20:04










  • @TrueTiem, sorry I forgot
    – Widdles
    Nov 10 at 20:06






  • 1




    simple test: run the query in phpMyAdmin and see if it works or creates an error
    – RiggsFolly
    Nov 10 at 20:06










  • And the complete and unsummarised error message as well please
    – RiggsFolly
    Nov 10 at 20:09








1




1




Theres's no num_rows in sql query, so please can you add php code too?
– TrueTiem
Nov 10 at 19:58





Theres's no num_rows in sql query, so please can you add php code too?
– TrueTiem
Nov 10 at 19:58













Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
– RiggsFolly
Nov 10 at 20:04




Welcome, to improve your experience on SO please read how to ask an On Topic question, and the Question Check list and the perfect question and how to create a [Minimal, Complete and Verifiable
– RiggsFolly
Nov 10 at 20:04












@TrueTiem, sorry I forgot
– Widdles
Nov 10 at 20:06




@TrueTiem, sorry I forgot
– Widdles
Nov 10 at 20:06




1




1




simple test: run the query in phpMyAdmin and see if it works or creates an error
– RiggsFolly
Nov 10 at 20:06




simple test: run the query in phpMyAdmin and see if it works or creates an error
– RiggsFolly
Nov 10 at 20:06












And the complete and unsummarised error message as well please
– RiggsFolly
Nov 10 at 20:09





And the complete and unsummarised error message as well please
– RiggsFolly
Nov 10 at 20:09


















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242832%2fnotice-trying-to-get-property-num-rows-of-non-object-how-to-trouble-shoot-sq%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242832%2fnotice-trying-to-get-property-num-rows-of-non-object-how-to-trouble-shoot-sq%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3