Trying to get property 'num_rows' of non-object in - php

I was setting up a user profile system using a tutorial, and I came across this PHP error.
//The first line is the one that gives the error
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<hr />';
echo '<table>';
echo '<tr><td>ID:</td><td>'.$row["id"].'</td></tr>';
echo '<tr><td>Avatar:</td><td><img src="'.$row["avatar"].'" width="100px" /></td></tr>';
echo '<tr><td>Firstname:</td><td>'.$row["firstname"].'</td></tr>';
echo '<tr><td>Lastname:</td><td>'.$row["lastname"].'</td></tr>';
echo '<tr><td>Country:</td><td>'.$row["country"].'</td></tr>';
echo '</table>';
}
}
else {
echo "0 results";
}
}

REPLACE $result->num_rows with $result->num_rows(). because it is a function. I hope this will works for you.
$result->num_rows()

Try This
if ($result && ($result->num_rows() > 0))
{
while($row = $result->fetch_assoc())
{
echo '<hr />';
echo '<table>';
echo '<tr><td>ID:</td><td>'.$row["id"].'</td></tr>';
echo '<tr><td>Avatar:</td><td><img src="'.$row["avatar"].'" width="100px" /></td></tr>';
echo '<tr><td>Firstname:</td><td>'.$row["firstname"].'</td></tr>';
echo '<tr><td>Lastname:</td><td>'.$row["lastname"].'</td></tr>';
echo '<tr><td>Country:</td><td>'.$row["country"].'</td></tr>';
echo '</table>';
}
}
else {
echo "0 results";
}

This error probably happens when you have no results whatsoever, therefore you're trying to get a property of an empty object. As the comments mentioned above, first you should make sure that $results is not empty. Instead of:
if ($result->num_rows > 0) {
Try:
if ($result && ($result->num_rows > 0)) {

$lclQuery = "SELECT * FROM users";
$result = $con->query($lclQuery);
if($result->rowCount() > 0) {
while($row = $result->fetch_assoc()) {
echo '<hr />';
echo '<table>';
echo '<tr><td>ID:</td><td>'.$row["id"].'</td></tr>';
echo '<tr><td>Avatar:</td><td><img src="'.$row["avatar"].'" width="100px" /></td></tr>';
echo '<tr><td>Firstname:</td><td>'.$row["firstname"].'</td></tr>';
echo '<tr><td>Lastname:</td><td>'.$row["lastname"].'</td></tr>';
echo '<tr><td>Country:</td><td>'.$row["country"].'</td></tr>';
echo '</table>';
}
}
else {
echo "0 results";
}
Use Like Above. Above Code definitely work.

Related

How To Fix While Loop output results

I just want to get the results of while loop which is within a php in different lines
if($count == 0){
$output = 'THERE WAS NO RESULTS';
}
else{
while($row = mysqli_fetch_array($query)){
$des = $row['description'];
$file = $row['name'];
$output ='<div>'.$des. '</div>';
echo "<td>";
echo "$output";
echo "<a href='".$file."'>Download</a></td>";
}
}
}
echo "</table>";
?>
I am getting all the results in the same line itself.
Are you missing the table row tag? <tr>
if($count == 0){
$output = 'THERE WAS NO RESULTS';
}
else{
echo '<table>';
while($row = mysqli_fetch_array($query)){
$des = $row['description'];
$file = $row['name'];
echo '<tr>';
echo '<td><div>'. $des .'</div></td>';
echo '<td><a href='".$file."'>Download</a></td>';
echo '</tr>';
}
echo '</table>';
}

How to check there is data, then show nothing

I want to check if the data is empty. Is it's empty don't show enything even not the google link. If there is data then show the google link. How can I fix it?
$result = $mysqli->query("SELECT * FROM teams WHERE teamid = ".$_GET['teamid']." ORDER BY `teamname` DESC");
$teamdetails = mysqli_fetch_assoc($result);
echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
echo ''.$teamdetails['teamid'].'<br>';
echo ''.$teamdetails['website'].' <br></td>';
echo '<td><img src=../../logo/'.$teamdetails['image'].'></td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_name'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_adress'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_zip'].' '.$teamdetails['cmp1_city'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_phone'].'</td></tr>';
echo '<tr><td colspan="2">Google maps</td></tr>';
You can try this:
<?php
if (is_array($teamdetails) && count($teamdetails) > 0) {
// Do something
}
Simple do it with row counts, if greater then 0 show if not, nothing to show
$result = $mysqli->query("SELECT * FROM teams WHERE teamid = ".$_GET['teamid']." ORDER BY `teamname` DESC");
$teamdetails = mysqli_fetch_assoc($result);
if((mysqli_num_rows($result) > 0) {
echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
echo ''.$teamdetails['teamid'].'<br>';
echo ''.$teamdetails['website'].' <br></td>';
echo '<td><img src=../../logo/'.$teamdetails['image'].'></td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_name'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_adress'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_zip'].' '.$teamdetails['cmp1_city'].'</td></tr>';
echo '<tr><td colspan="2">'.$teamdetails['cmp1_phone'].'</td></tr>';
echo '<tr><td colspan="2">Google maps</td></tr>';
} else {
echo '<tr><td colspan="2">'Nothing to Show'</td></tr>';
}
right after running mysqli_fetch_assoc:
if (mysqli_affected_rows()){
...
}
you need to use "isset" function because using count() may exist rows but have no data in them.
[...]
$teamdetails = mysqli_fetch_assoc($result);
if (isset($teamdetails['teamname']) & $teamdetails['teamname']) {
echo '<table id=kalender_table><tr><td><h3>'.$teamdetails['teamname'].'</h3> <br>';
[...]
}
[...]

unable to produce result correctly in div php

I'm not very sure how can I put them into words. I'm trying to display result in each div as shown in the image but unfortunately I'm only able to make it appear only in the "request for quote" div.
May I know where have I gone wrong?
<?php
$query2 = "SELECT * FROM client c, sales_card s WHERE c.id = s.client_id and emp_id_followup = '".$_SESSION["ID"]."'";
$result2 = mysql_query($query2);
if (mysql_num_rows($result2) > 0) {
while($row2 = mysql_fetch_assoc($result2)) {
$swimlaneID = $row2['swimlane_id'];
}
}
$query = "SELECT * FROM swimlane";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
echo '<div id="right">';
echo '<div style="border-style:solid; height:1020px;">';
echo '<h2>'. $row["swimlane_name"].'</h2>';
echo '<ul id="'. $row["shortform"].'">';
if ($swimlaneID == $row["id"])
{
echo $display-> $row["shortform"]();
}
echo '</ul>';
echo '</div>';
echo '</div>';
}
}else{
echo "no row";
}
?>

PHP/SQL - Query not showing all data

Here's the code:
<?php
if(isset($_POST['results']) && $_POST['results'] != -1) {
$db = new PDO('mysql:host=localhost;dbname=;charset=utf8', '', '');
echo "<table border='1'>
<tr>
<th>Courses</th>
</tr>";
$stmt = $db->prepare("SELECT title FROM course WHERE `subject_id`=?");
$stmt->execute(array($_POST['results']));
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>", $row['title'], "</td>";
echo "</tr>";
}
} else {
echo "No results found";
} echo "</table>";
}
?>
This is just returning one result into the table, when there are more results to show.
Where am I going wrong?
remove the if, because it's change the pointer of array(fetch)
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // get the first record, remove this if
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {// get the second record and reset $row
you can change to:
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
$total_rows = $stmt->rowCount();
if($total_rows > 0){
foreach($row as $item){
echo '<td>'. $item['title'] .'</td>';
}
}else{
echo 'no results';
}

SQL Not Found Condition

I have a php script that tries to find a particular name in a database where the specified program is $q, a variable passed from an html page. I'm very new to this so I'm having trouble figuring out how to code an if not found, then display type of message. Below is what I currently have:
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
And I need it to echo a message saying if nothing was found. I tried looking at NOT IN condition in SQL and http://www.techonthenet.com/sql/exists.php along with other things on the internet but I'm not sure if this is the right thing to use. Any help would be appreciated.
use th php function
mysql_num_rows($result);
to check results found
So your code should be like:
$sql="SELECT * FROM names WHERE program='".addslashes($q)."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
while($row = mysql_fetch_array($result))
{
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
else
echo "No data found";
please note i added
addslashes($q)
in query, in order to avoid SQL injection problems.
$sql="SELECT * FROM names WHERE program='".$q."'";
$result = mysql_query($sql);
$found = false;
while($row = mysql_fetch_array($result))
{
$found = true;
echo "<div class='header'>Program Name:</div>";
echo "<div class='data'>";
echo $row['program'];
echo "</div>";
}
if ($found == false)
echo "I found nothing";

Categories