I am doinga MySQL query to retreive data using PHP. I need to have a logic that if the data set returned is empty it shows a warning message else it displays the results:
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
mysqli_close($conn);
You need num_rows on the object
$result = mysqli_query($conn,$sql);
if ($result->num_rows == 0) {
echo 'result empty';
} else {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
http://php.net/manual/en/mysqli-result.num-rows.php
Change mysql_escape_string to mysqli_escape_string here
$where = "";
if(isset($_POST['searchQuery']) && trim($_POST['searchQuery'])){
$searchQuery = mysqli_escape_string($conn,$_POST['searchQuery']);
$where =" WHERE column1 = '".$searchQuery."'";
}
$sql="SELECT * FROM db.tblname ".$where;
It's as simple as this:
if ($result) {
//Your code
} else {
echo "Error: ".mysqli_error($conn);
}
First check with mysqli_num_rows(). Here is how to
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
}
else {
echo "No records found";
}
mysqli_close($conn);
$searchQuery = mysql_escape_string($_POST['searchQuery']);
$sql="SELECT * FROM db.tblname WHERE column1 = '".$searchQuery."'";
$result = mysqli_query($conn,$sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
echo $row["column1"];
}
} else {
echo "No results found";
}
mysqli_close($conn);
Related
I'm trying to make an html table with the data in the database but when I write echo it just shows me a result. How can I echo all the elements?
$sql = "SELECT * FROM app_spot where company_id='$company_id'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
}
$spotcount = mysqli_num_rows($result);
} else {
echo "0 results";
}
Echo the table rows in the while loop.
if (mysqli_num_rows($result) > 0) {
echo "<table><tr><th>ID</th><th>Name</th><th>Location</th><th>Budget</th><th>Status</th></tr>";
while($row = mysqli_fetch_array($result)) {
$spot_id = $row["id"];
$spot_name = $row["spot_name"];
$store_location = $row["store_location"];
$spot_budget = $row["spot_budget"];
$spot_status = $row["spot_status"];
echo "<tr><td>$spot_id</td><td>$spot_name</td><td>$store_location</td><td>$spot_budget</td><td>$spot_status</td></tr>";
}
echo "</table>";
}
i receive "echo "Klaida!!!!"" meaning there is no results.
Could you please point out my mistake in code.
$dezesdezesId = $_GET["dezesId"];
$query = "SELECT * FROM dezes WHERE dezesId = '$dezesdezesId'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows ($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$dezesPavadinimas = $row["pavadinimas"];
$dezesLikutis = $row["likutis"];
}
echo $dezesLikutis;
}
else
{
echo "Klaida!!!!" . mysqli_error($connection);
}
echo this inside your while loop.
while ($row = mysqli_fetch_assoc($result))
{
$dezesPavadinimas = $row["pavadinimas"];
$dezesLikutis = $row["likutis"];
echo $dezesLikutis;
}
and try to use '{$dezesdezesId}' your variable inside your query string
I try to get data from category using mysql and php.
Sql Structure:
Category
-cat_id
-name
Date
-id
-url
-category
Php code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
?>
when i select the category the data is not listed.
Any idea?
Try this Code . Just removed single quotations
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
May this will work:
PHP Code
<?php
$sql = "select * from category";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo '<select id="category" name="category">';
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
echo '</select>';
}
}
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo 'url is: '.$row["url"];
}
}
?>
Try this
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
if you want result with in single quotes
$sql = "select * from date WHERE category='1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result))
{
echo "'".$row["url"]."'";
}
}
Please avoid mysql_* because the mysql_* functions have been removed in PHP7. Use MySQLi instead.
PHP + Mysql :
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo '.$row["url"].';
}
}
}
?>
PHP + Mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from category";
$result = $conn->query($sql);
echo "<select name='category'>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$category_id = $_POST['category'];
$sql = "select * from date WHERE category = '".$category_id."'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while ($row = $result->fetch_assoc())
{
echo '.$row["url"].';
}
}
}
$conn->close();
?>
Nothing changed.
I'm using this code:
<?php
$sql = "select * from category";
$result = mysql_query($sql);
echo "<select name='category'>";
if(mysql_num_rows($result) > 0)
{
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row["cat_id"].'">'.$row["name"].'</option>';
}
}
echo "</select>";
if(!empty($_POST['category'])) {
$sql = "select * from date WHERE category = '1'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{
echo $row["url"];
}
}
}
?>
If i delete if condition the data is listed but all the time.
I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!
Is better way to do this? I want more code reuse.
function display_maker_success($link, $userid){
$status="closed";
$result="completed";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If ($isempty ==0) {
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
};
echo "</table>";
};
};
function display_maker_fail ($link, $userid) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
};
echo "</table>";
};
};
function display_maker_stat ($link, $userid, $reuslt, $limit) {
$status="closed";
$result="fail";
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$result = mysql_query($sql, $link);
$isempty=mysql_num_rows($result);
If($isempty ==0){
echo "No Record";
} else {
echo "<table border=1>";
echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
};
echo "</table>";
};
};
Try the below,
Also there were few errors in your code and i have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
{
$status = "closed";
$html = '';
$sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
$query = mysql_query($sql, $link);
if (mysql_num_rows($query) != 0) {
$html .= "<table border=1>";
$html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
$html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
}
$html.= "</table>";
echo $html;
}
else {
echo "No Record";
}
}
Read about OOP
I am using this query below and it only returns the first query in the entry if i use only the if (empty($field1)) to display. If i fill in the print(""); it works but i want to use the if (empty($field1)) snippet to display. How can i do it?
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "")
{
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0)
{
print("");
}
elseif($rows > 0)
{
while($row = mysql_fetch_array($query))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
print("");
}
}
if (empty($field1)) {
echo ""; //Thats right, i don't want anything to show for this portion
} else {
echo "<div id=comments>Comments</div><br>
<div id=entries>$field1 and $field2</div>";
}
What are you trying to do? somthing like this:
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);
if ($rows > 0)
echo "here are your entries\n";
while($row = mysql_fetch_array($result))
{
echo $row['field1']." ";
echo $row['field2']."\n";
}
another way
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid' and k_id='$kid' ORDER BY id DESC";
$result=mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($result);
if ($rows > 0)
echo "here are your entries\n";
while($row = mysql_fetch_array($result))
{
if (empty($row['field1'])) {
echo " ";
} else {
echo $row['field1']." ";
echo $row['field2']."\n";
}
}
i believe mysql_fetch_array only returns one row
http://www.w3schools.com/PHP/func_mysql_fetch_array.asp
also ur sure that neither p_id and k_id are not unique?
i would also try $sql="SELECT * FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
just to see if that yields any different results, you can always parse out just the two fields from the return data
TRY THIS TO START WITH (the $results variable is just confusing things):
$sql="SELECT field1, field2 FROM table WHERE p_id='$pid'
and k_id='$kid' ORDER BY id DESC";
$query = mysql_query($sql) or die ("Error: ".mysql_error());
$rows = mysql_num_rows($query);
if($rows == 0)
{
print("");
}else{
while($row = mysql_fetch_array($query))
{
if ($row['field1'] == "")
{
print("");
}else{
$field1 = $row['field1'];
print($field1)
}
if ($row['field2'] == "")
{
print("");
}else{
$field1 = $row['field2'];
print($field2)
}
}
}