php echo if query is empty - php

I'm querying a database but when the result is empty i want to output a table row displaying "nothing to display" but the if seems to always return true.
Here's my code...
$priorityincidentsQ = mysql_query("SELECT * FROM applications WHERE pi >= ('2') ");
while($priorityincidentsR = mysql_fetch_object($priorityincidentsQ))
{
if (empty($priorityincidentsR)) {
echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
} else {
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
}

Use mysqli_num_rows() to check is there any result:
$conn = mysqli_connect($host, $user, $password, $database);
$priorityincidentsQ = mysqli_query($conn, "SELECT * FROM applications WHERE pi >= ('2') ");
if (mysqli_num_rows($priorityincidentsQ) > 0){
while ($priorityincidentsR = mysqli_fetch_object($priorityincidentsQ)) {
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
}else{
echo "<tr><td class=\"closedcallscell centered\"><b>Nothing to display</b></td></tr>";
}
And yes, better use mysqli_* functions instead of mysql_*.

Still never figured out why this wouldn't work out for me this way, I tried the query directly in SQL workbench and everything looks how it should, I ended up solving the problem like this.
<!-- priority incidents-->
<?php
$priorityincidentsQ = mysql_query("SELECT * FROM applications WHERE pi >= ('1') ");
while($priorityincidentsR = mysql_fetch_object($priorityincidentsQ))
{
echo "<tr><td class=\"closedcallscell\"><b>$priorityincidentsR->application_friendly_name</b></td>";
echo "<td class=\"closedcallscell table_row_small\"><center>$priorityincidentsR->pi</center></td></tr>";
}
?>
<!-- if no incidents-->
<?php
$incidentNumberofRowsQ = mysql_query("SELECT COUNT(*)numberofrows FROM applications WHERE pi >= ('1') ");
while($incidentNumberofRowsR = mysql_fetch_object($incidentNumberofRowsQ))
{
if ($incidentNumberofRowsR->numberofrows == '0')
{
echo "<tr><td class=\"closedcallscell centered\"><b>Currently no priority incidents</b></td>";
}
}
?>
may seem a rather silly way of going about it but atleast it works. Thanks all for the help. :)

Related

Dynamically Updating PHP HTML Table with AJAX

I have a PHP HTML table loads in data from a SQL database. How would I be able to update that data using an onclick button function?
Basically I want the whole data to be updated so instead of the SQL query looking for the month of January, I want to display data from March.
I have setup php files to look for different datasets, im just not sure on how to use Ajax to refresh the table.
Here is a snippet of what im using to populate the HTML table:
echo "<table class='content-table is-striped'>";
echo "<tr>";
echo "<th>Person</th>";
echo "<th>Weight (Kg)</th>";
echo "<th>Date</th>";
echo "</tr>";
$month = date('m');
if ($month == "01") {
$sql = "SELECT * from WeightTracker WHERE person = 'Ayush' AND date_month = 'January' ORDER BY weight_date DESC";
$result = mysqli_query($conn, $sql);
$x = 0;
if ($result-> num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
if ($x % 2 == 0) {
$bgcolour = oddbg;
}
else {
$bgcolour = evenbg;
}
echo "<tbody><tr class='$bgcolour'><td>". $row["person"] ."</td><td>". $row["kg"] ."</td><td>". $row["weight_date"] ."</td></tr></tbody>";
$x++;
}
echo "</table>";
}
else {
echo "0 results";
}
}
Any ideas would be awesome!

How to combine to php's (sql)

I need to combine these two codes,more likely the sql. I'm sitting on it about hours and can't get it.
<?php
$mysqli2 = new mysqli("Edudb", "mct", "QwSkepticx97!");
$mysqli2->select_db("maturita");
$ask2 = $mysqli2->query("SELECT * FROM gallery");
if(mysqli_num_rows($ask2)>0)
{
while ($row2=mysqli_fetch_object($ask2))
{
echo "<a href='show-gall.php?gallery=$row2->gall_id'>$row2->name<br></a</span>";
}
}
?>
and this one
<?php
$conn = mysqli_connect("Edudb", "mct", "QwSkepticx97!", "maturita");
$sql="SELECT g.name, i.img_thumb FROM gallery g JOIN images i ON g.gall_id = i.gall_id GROUP BY g.gall_id ";
$query=mysqli_query($conn, $sql);
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_object($query))
{
echo "<div class='view'>";
echo "<a href='show-gall.php?gallery=$row->gall_id'><img src='$row->img_thumb' alt=''></a>";
echo "<br/>";
echo "<a href='show-gall.php?gallery=$row->gall_id'>$row->name</a>";
echo "<br/>";
echo "</div>";
}
}
?>
I'll be thankful for any suggestions
Try this one
include('here put the name of one file of two ');
Then put this statement in the other one.
Now the other file that has include() statement shows everything in the second one.

PHP, server isset, make form input do nothing if empty and execute code if there is any input on it (its a databse search engine)

ok as the title says, please take a look at the code im using:
$search=$_POST['search'];
$query = $pdo->prepare("select * from exportitems where partn LIKE '%$search%' OR manufacturer LIKE '%$search%'");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
if(isset($_POST['search']) || empty($_POST['search']))
{
echo "<center>WRITE SOMETHING IN THE SEARCH FIELD</center><br/>";
}
else if (!$query->rowCount() == 0) {
echo "<center>Search results</center><br/>";
echo "<table style=\"font-family:arial;color:#ffffff;\">";
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#000000;\">PART NUMBER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">MANUFACTURER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">DETAILS</td></tr>";
while ($results = $query->fetch()) {
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['partn'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['manufacturer'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['details'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo "<center>NO RESULT FOUND</center><br/>";
}
?>
the problem im having appears when doing the query, it only executes:
if(isset($_POST['search']) || empty($_POST['search']))
{
echo "<center>WRITE SOMETHING IN THE SEARCH FIELD</center><br/>";
}
and ignores the rest, so no matter if the field is empty, or anything is written on it, it wont execute the code and get stuck on there.
BUT if i use this code only:
$query->execute();
if (!$query->rowCount() == 0) {
echo "<center>Search results</center><br/>";
echo "<table style=\"font-family:verdana;color:#ffffff;\">";
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#000000;\">PART NUMBER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">MANUFACTURER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">DETAILS</td></tr>";
while ($results = $query->fetch()) {
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['partn'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['manufacturer'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['details'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo "<center>NO RESULT FOUND</center><br/>";
}
?>
the search script will work properly EXCEPT for the fact that if the form search field have no characteres and the search button is pressed, the result will output the whole database as a result, making it a security flaw.
any idea of what i'm doing wrong??
thanks in advance.
p.s.: i searched for similar solutions on the community BUT nothing appeared to be this specific case.
Your if statement is asking "is the value set or is it empty?"
In other words "is it something, or is it nothing?"
To which the answer is, of course, "yes".
empty implies !isset as part of its tests, you don't need to duplicate the work.
Just if( empty($thing)) throw error will do.
You will want to fix your query though. You're doing prepared statements wrong...
with
if(isset($_POST['search']) || empty($_POST['search']))
You are saying if exist or don't has a value write this... this will happen always.
Just check the empty state
if(empty($_POST['search']))
isset($_POST['search']) will always return true because it'll receive empty string when user doesn't enter anything into the field.
Next thing is that you are executing query and then checking if you've got anything to search for.
Try with:
$len = 0;
if (array_key_exists('search', $_POST)) {
$search = $_POST['search'];
$search = trim($search); // optional: if you want remove first and last space
$len = strlen($_POST['search']);
}
if ($len === 0) {
// do something
} else {
// do something
}
array_key_exists check if key exist in array(or variable array).
issetcheck if variable is set.
if(isset($_POST['search']) && !empty($_POST['search']))
{
//Only process the search query when its posted and yah its not empty
$search=$_POST['search'];
$query = $pdo->prepare("select * from exportitems where partn LIKE '%$search%' OR manufacturer LIKE '%$search%'");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
if (!$query->rowCount() == 0) {
//After query has been executed get the row count which you were doing right
echo "<center>Search results</center><br/>";
echo "<table style=\"font-family:arial;color:#ffffff;\">";
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#98bf21;background:#000000;\">PART NUMBER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">MANUFACTURER</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;background:#000000;\">DETAILS</td></tr>";
while ($results = $query->fetch()) {
echo "<tr><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['partn'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['manufacturer'];
echo "</td><td style=\"border-style:solid;border-width:1px;border-color:#000000;\">";
echo $results['details'];
echo "</td></tr>";
}
echo "</table>";
} else {
echo "<center>NO RESULT FOUND</center><br/>";
}
} else {
//if its not posted or either empty show the warning message
echo "<center>WRITE SOMETHING IN THE SEARCH FIELD</center><br/>";
}
Read tutorials....

Produce dynamic link from array

I have a searchpage, which works fine. Search results are displayed in a table with tr hover and a window.location to take the user to a different page.
What I am trying to achieve is a dynamic link for the window.location based on data from the array. All the data in the db belongs to 4 different categories, testkat, and I would like to direct the user to the right page depending on the value from testkat, and then using the 'testid' to fill in the data.
I have been trying numerous ways to achieve my goal, and searched both SE, Google etc, but no luck. I'm pretty new to PHP so using the right search term might have something to do with it.
From my point of view I'm thinking that I have to store the value from testkat in a variable, lets say $link. And from there make an IF statement, something like:
if ($results('testkat') == 'something') {
$link = "something.php?id='$testid'";
}
if ($results('testkat') == 'something_else') {
$link = "something_else.php?id='$testid'";
}
And from there put $link in the window.location
Here's my code:
<?php
$conn = mysql_connect("localhost", "root", "") or die("Couldn't do it: ".mysql_error());
mysql_select_db("db") or die(mysql_error());
mysql_set_charset('utf8',$conn);
$query = $_POST['query'];
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM db WHERE (`id` LIKE '%".$query."%') OR (`name` LIKE '%".$query."%') OR (`age` LIKE '%".$query."%')") or die(mysql_error());
$count = mysql_num_rows($raw_results);
if(isset($_POST['query'])) {
echo "<br>";
echo "Your search for <span class=\"bold\">" .$query. "</span> returned " . $count . " hits";
echo "<br>";
if(mysql_num_rows($raw_results) > 0){
echo "<br>";
echo "<table class=\"tbl-text\">";
echo "<tr class=\"tablelist\"><th>Heading 1</th><th>Heading 2</th><th>#</th><th>Heading 3</th><th>Heading 4</th><th>Heading 5</th>";
while($results = mysql_fetch_array($raw_results)){
echo "<tr onclick=\"window.location='#'\" style=\"cursor:pointer\" class=\"tr-hover\">";
echo "<td class=\"bordered\">" .$results['testid'] . "</td>";
echo "<td class=\"bordered\">" .$results['testkat'] . "</td>";
echo "<td class=\"bordered\">" .$results['intnr'] . "</td>";
echo "<td class=\"bordered\">" .$results['pro'] . "</td>";
}
echo "</table>";
}
else{
}
}
?>
Update:
Forgot to tell about the error. When doing it the way I think it should be done, I get an error message in the IF statement saying: Fatal error: Function name must be a string.
Referring to this one:
if ($results('testkat') == 'something') {
$link = "something.php?id='$testid'";
}
I know about MySQLi and PDO, working on it.
Eager to learn, so any hints and tricks are greatly appreciated :)
Chris
That method looks fine. You don't need the single quotations around $testid though
$link = "something_else.php?id=$testid";
As you've mentioned you should stop using mysql, get learning :)
Managed to fix it, and posting if someone else are having the same problem.
First, rewrote the whole thing to MySQLi.
Then I put an IF statement after the WHILE LOOP like this:
Connecting to db ->
if(isset($_POST['query'])) {
$query = $_POST['query'];
$query = htmlspecialchars($query);
$sql = $db->query("SELECT * FROM db WHERE (`?` LIKE '%".$query."%') OR (`?` LIKE '%".$query."%') OR (`?` LIKE '%".$query."%')");
$result = mysqli_query($db, sql);
$hits = $sql->num_rows;
echo "<br>";
echo "Your search for <span class=\"bold\">" .$query. "</span> returned " . $hits . " results";
echo "<br>";
if($sql->num_rows > 0){
echo "<br>";
echo "<table class=\"tbl-text\">";
while ($row = mysqli_fetch_array($sql)) {
if ($row['category'] == 'cat01'){
$link = 'cat01.php?id=' . $row['testid'] . '';
}
if ($row['category'] == 'cat02'){
$link = 'cat02.php?id=' . $row['testid'] . '';
}
if ($row['category'] == 'cat03'){
$link = 'cat03.php?id=' . $row['testid'] . '';
}
if ($row['category'] == 'cat04'){
$link = 'cat04.php?id=' . $row['testid'] . '';
}
echo "<tr onclick=\"window.location='$link'\" style=\"cursor:pointer\" class=\"tr-hover\">";
echo "<td class=\"bordered\">" .$row['testid'] . "</td>";
>>> more echo
}
There are probably more efficient ways to do this, but at least I got the results I was after, and the script is also more secure now using MySQLi

change from mysql to mysqli

I wrote code that allow me to get information from mysql using mysql which is old code, and i tried to change to mysqli from mysql but some how is not working right, I believe that i'm still new to learn about mysqli, can any one help me to to write into mysqli from mysql so i can learn myself from there please... here is code...
thanks...
<?php
include('db.php');
?>
<?php
if(isset($_POST['kw']) && $_POST['kw'] != '')
{
$kws = $_POST['kw'];
$kws = mysql_real_escape_string($kws);
$query = "select * from product where product_name like '%".$kws."%' limit 10" ;
$res = mysql_query($query);
$count = mysql_num_rows($res);
$i = 0;
if($count > 0)
{
echo "<ul>";
while($row = mysql_fetch_array($res))
{
echo "<a href='$row[product_name]'><li>";
echo "<div id='rest'>";
echo $row['product_name'];
echo "<br />";
echo "<td>";?><img src="<?php echo $row["screenshot"];?>" height="100" width="100" <?php echo "</td>" ;
echo "</div>";
echo "<div style='clear:both;'></div></li></a>";
$i++;
if($i == 5) break;
}
echo "</ul>";
if($count > 5)
{
echo "<div id='view_more'><a href='#'>View more results</a></div>";
}
}
else
{
echo "<div id='no_result'>No result found !</div>";
}
}
?>
The general rule of thumb is:
Do not change.
But write mysqli version from scratch.
As your approach with old mysql ext were all wrong, do not translate it to new mysqli. Just learn mysqli and then write the whole thing from scratch.
Otherwise you'd better leave your old code as is, just because such a mechanical rewriting will do not a slightest good.

Categories