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
php mysql
|
show 12 more comments
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
php mysql
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
|
show 12 more comments
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
php mysql
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
php mysql
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
|
show 12 more comments
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
|
show 12 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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