How to check there is data, then show nothing - php

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>';
[...]
}
[...]

Related

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

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.

Missing the first row of a SELECT statment

I have a database table called 'Modules' and I am trying to select all of the rows from that table and display them in a table. Each row has a column called MOrder which ranges from 1 - how ever many modules are available.
Here is my code:
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
?>
However, for whatever reason the statement is missing out the module with MOrder 1 and always starting with 2?
Why is this?
You are calling $row = mysqli_fetch_array($result, MYSQLI_ASSOC); in the third line of your pasted code, which is pulling the first array from the results.
This is then being overwritten in your while loop:
while ($row = mysqli_fetch_array($result)) { // <-- overwritten here with item 2
//...
}
Because in the 3rd line of your code you call
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
once, and then in the loop you start calling the
$row = mysqli_fetch_array($result)
again, thus overwriting the $row variable with the 2nd row. Get rid of the 1st $row = mysqli_fetch_array($result) line.
Try this code.
$sql_query = "SELECT * FROM Modules WHERE CourseID = ". $CourseID ." ORDER BY MOrder ASC";
$result = mysqli_query($dbconfig, $sql_query);
$count = mysqli_num_rows($result);
echo '<div class="row">';
echo '<div class="col-md-12">';
echo '<table class="table">';
if($count > 0) {
$_SESSION['countMOrder'] = $count;
echo '<tr>';
echo '<th>Module Title</th> ';
echo '<th></th>';
echo '</tr>';
while ($row = mysqli_fetch_array($result)) { //for every fetch we'll get one row.
echo '<tr>';
echo '<td>'. $row['Title'] .'</td> ';
echo '<td>Take Module</td>';
echo '</tr>';
}
}
echo '</table>';
echo '</div>';
echo '</div>';
At the beginning, you fetch the first row.
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
In while loop, fetch function returns second row.

PHP incomplete result table from sql query

I am new to web development and starting learning about CRUD.
My question is that while I successfully show the table listing 3 product on 1 row, that on the second row the product no 4 are missing and skipping to the product no 5 and keep missing every last 3 row.
function getData(){
global $connect;
$query = "SELECT id, name, category, price, image, info FROM product_data";
$results = mysqli_query($connect, $query) or die(mysql_error());
echo "<table border = 0 >";
$x=1;
echo "<tr class='homepagetable'>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if($x<=3){
$x = $x + 1;
extract($row);
echo "<td class='homepagetable'>";
echo "<a href=itemdetails.php?id=$id>";
echo '<img src=img/' . $image . ' style="max-width:220px;max-height:240px;width:220px;height:240px;"></img><br/>';
echo '<div class="truncate">'. $name .'</div><br/>';
echo "</a>";
echo 'Rp.'.$price .'<br/>';
echo "</td>";
} else {
$x=1;
echo "</tr><tr>";
}
}
echo "</table>";
}
Thanks in advance!
Your problem is that you have a wrong condition here:
if($x <=3)... else{...}
Change the if/else to this:
if($x <3){$x++;}
//...do something
if($x == 3){
$x = 1;
//Close and open tr
}
And you need to close the <img> tag, and the last <tr> tag outside of the loop
Perhaps you can write it like this:
function getData()
{
global $connect;
$query = 'SELECT id, name, category, price, image, info FROM product_data';
$result = mysqli_query($connect, $query) or die(mysql_error());
echo '<table border="0">';
$x=0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if($x % 3 == 0)
{
// number of articles is a multiple of 3 - so close the row
if($x) echo '</tr>'; // but only if there was at least 1 article
echo '<tr class="homepagetable">';
}
$x ++;
echo '<td class="homepagetable"><a href="itemdetails.php?id='.$row['id'].'">
<img src="img/'.$row['image'].'" style="max-width:220px;max-height:240px;width:220px;height:240px;"><br/>
<div class="truncate">'.$row['name'].'</div><br/>
</a>Rp. '.$row['price'].'<br/>
</td>';
}
if($x) echo '</tr>'; // only close the row if there was at least 1 article
echo '</table>';
}

`while` is not showing the echo in php

I'm creating a small private forum to get some more knowledge about PHP/PDO etc. Now I have a weird bug/error/wrong piece of code that is not showing the echo. This is my code.
$sql2 = $db->prepare('SELECT topic_id, topic_subject,topic_date,topic_cat FROM topics WHERE topic_cat = :topid');
$sql2->bindParam(':topid', $_GET['id'], PDO::PARAM_INT);
$sql2->execute();
$result2 = $sql->rowCount();
if($result2 === FALSE){
echo 'The topics could not be displayed, please try again later.';
}
elseif ($result2 === 0){
echo 'There are no topics in this category yet.';
} else {
//prepare the table
echo '<table border="1">
<tr>
<th>Topic</th>
<th>Created at</th>
</tr>';
while($row = $sql2->fetch()) {
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3>' . $row['topic_subject'] . '<br /><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
}
}
It should show the echo at while($row = $sql2->fetch()), but it is not. Also I know there is not enough { and } but that's because the other part of the code is not relevant.
You appear to count the rows returned by $sql then loop through $sql2. Have you checked to see if there are any results in $sql2?

Unable to sort with date

Im trying to order posts by their date, but whenever I try to do that I get this error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\localhost\bootstrap\category.php on line 58
DATABASE STRUCTURE: http://puu.sh/1630b
<?php
//category.php
include 'connect.php';
//first select the category based on $_GET['cat_id']
$sql = "SELECT
cat_id,
cat_name,
cat_description
FROM
categories
WHERE
cat_id = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
if(!$result)
{
echo 'The category could not be displayed, please try again later.' . mysql_error();
}
else
{
if(mysql_num_rows($result) == 0)
{
echo 'This category does not exist.';
}
else
{
//display category data
while($row = mysql_fetch_assoc($result))
{
echo '<h2>Topics in ′' . $row['cat_name'] . '′ category</h2><br />';
$title = $row['cat_name'];
include 'header.php';
}
//do a query for the topics
$sql = "SELECT
topic_id,
topic_subject,
topic_date,
topic_cat
FROM
topics
ORDER BY
topic_date DESC
WHERE
topic_cat = " . mysql_real_escape_string($_GET['id']);
$result = mysql_query($sql);
// if(!$result)
// {
// echo 'The topics could not be displayed, please try again later.';
// }
// else
// {
if(mysql_num_rows($result) == 0)
{
echo 'There are no topics in this category yet.';
}
else
{
//prepare the table
echo '<table border="1" class="table table-bordered table-striped" style="float: right; width: 990px;">
<tr>
<th>Topic</th>
<th>Created at</th>
</tr>';
while($row = mysql_fetch_assoc($result))
{
echo '<tr>';
echo '<td class="leftpart">';
echo '<h3>' . $row['topic_subject'] . '<br /><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
echo '';
echo '';
}
echo '</table>';
echo '</div>';
}
// }
}
}
include('footer.php');
?>
I this case the problem will be in the commented lines 52 - 57 which are supposed to check if the mysql_query has been successful. Your query fails and returns false (boolean), which is a valid return value.
The error itself depends on your database table structure (isn't part of your link).
Your query that executes, fails and returned a boolean instead of a resource!
build in some error handling in your script.
do not use mysql_ functions, they are deprecated.
And now that you have edited your post, it is obvious that the ORDER BY comes after the WHERE.

Categories