I have this line of code, which checks the result of the query. I want to have a STATUS ERROR if one of the columns contains a value= '0'.
If the condition founds a zero value it will echo: Status: ERROR,
Status
PHP CODE:
<?php
$query = "SELECT * FROM `inventorybill_tbl`";
$result = mysqli_query($link, $query) or die("Failed".mysqli_error());
//Here it will check the result if there is a value contains zero
if (empty($result)) {
echo 'Status: ERROR';
} else {
echo 'Status: OK';
}
?>
Is there any way to achieve this kind of method? Thanks.
mysqli_query() Return values
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object. For
other successful queries mysqli_query() will return TRUE.
Check if empty($result->num_rows) - then there's no data.
bool zeroExist = false;
$finfo = $result->fetch_fields();
foreach ($finfo as $val)
{
while($row = $result->fetch_assoc())
{
if ($row[$val->name] === 0)
{
zeroExist = true;
break;
}
}
if(zeroExist){
break;
}
}
if (!zeroExist){
echo 'Status: OK';
}else {
echo 'Status: ERROR';
}
try this
try this.
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
//$row["your column name"]
if (empty($row["twohundred"]) || $row["twohundred"]=='0' || $row["twohundred"]==0) {
echo 'Status: ERROR';
} else {
echo 'Status: OK';
}
}
}
You should check condition in your query instead of PHP code.
Rewrite your query as below:-
SELECT * FROM table WHERE NOT columnA = 0 OR NOT columnB = 0;
OR
SELECT * FROM table WHERE columnA != 0 OR columnB != 0;
Check this Original Answer Link
Related
I am trying to return data from my database. The query will return a zero or one. If the data returns one (if psv1=1) I want to echo 'Data found'. If the data returns a zero (if psv1=0) I want to echo 'No data found'.
When I run my script I always get 'No data found', even when psv1=1. I also tried to change $res[0] == 1 to $res[0] > 0 but it didn't work.
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1" AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_row($result);
if ($res[0] == 1){
echo 'Data found';
}
else
{
echo "No data found";
}
You should use mysqli_fetch_array($result, MYSQLI_ASSOC):
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1" AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_array($result, MYSQLI_ASSOC);
if ($res['psv1'] == 1){
echo 'Data found';
}
else {
echo "No data found";
}
I wonder if you need both conditions in the WHERE. Are you meaning to have id and user_id. Suggest removing:
id="1" AND
Can only be a guess, but seems unlikely condition.
On checking no results returned, may I suggest trying:
$result = mysqli_query($db_handle, 'SELECT psv1 FROM cus WHERE id="1"
AND user_id="'. $_SESSION['user_id'] .'"');
$res = mysqli_fetch_row($result);
if(mysqli_num_rows($result) > 0){
echo 'Data found';
}
else {
echo "No data found";
}
See related SO answer on mysqli_num_rows
According to the Manual, mysqli_fetch_row() will return NULL on failure or else an array of strings corresponding to a row of data as an enumerated array. I suggest also testing that mysqli_query() does not return FALSE as per the Manual's recommendation by way of numerous examples here. So, a better test would be to code something like the following with $query having been set to the specific query:
<?php
$result = null;
$res = null;
$result = mysqli_query( $db_handle, $query ) OR die( mysqli_error( $db_handle ));
if( ( $res = mysqli_fetch_row( $result )) === NULL ) {
// Nothing to fetch
}
else
{
echo ($res[0] == 1 )? "Data found." : "No data found";
}
/MY CODE/
The if part is working properly but else is not working.
i even tried $variable instead of direct echo but still it is not working 'else'
Updated
<?php
$db = new mysqli('localhost', 'root' ,'', 'timeline');
if(!$db) {
echo 'Could not connect to the database.';
} else {
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10");
if(isset($query)) {
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
}
echo '</ul>';
} else {
echo 'create some'; // this part is not working
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
help me out.....
even read lots of like problem on stackoverflow but no real return
If you are using mysqli::query then your if(isset($query)) statement will always be evaluated as true, as $query would be either FALSE or a mysqli_result object. isset returns TRUE for both these values, so your else code will never be called.
Documentation on isset:
Returns TRUE if var exists and has value other than NULL, FALSE otherwise.
Use if($query !== false) instead.
Update
It also seems like you are checking $query to see whether or not there was a hit in the database. You need to check the number of rows in the result for that, e.g:
if ($query !== false && $query->num_rows > 0) {
// Query was ok and at least one row was returned
}
else {
// Will be reached if query was bad or there were no hits
}
Try
if($query_run = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10")){
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
}
echo '</ul>';
} else {
echo 'create some';
}
I have the following code.
$query = "SELECT HealthStatus FROM healthstatus where HealthStatus=$HealthStatus";
$result = mysql_query($query);
echo $HealthStatus;
if($result = false)
{
//do something
}
else
{
//print value already exists
}
I don't get any error or warning when the code is executed. But, even if $HealthStatus exists in database, the if part gets executed. When I give echo $HealthStatus, the value fetched is printed correctly.
I have tried using if(!$result). That doesn't work either. Can someone help me.
You have to use mysql_num_rows to know if the query returned any rows, eg:-
if($result && mysql_num_rows($result))
{
// a row exists
}
else
{
// do something
}
also if HealthStatus is a string it needs to be enclosed in quotes eg:-
$query = "SELECT HealthStatus FROM healthstatus where HealthStatus='".$HealthStatus."'";
$result = mysql_query($query);
if($result && mysql_num_rows($result))
{
// a row exists
$row=mysql_fetch_array($result);
echo "Health status was ".$row["HealthStatus"];
}
else
{
// do something
echo "There were no rows found";
}
To understand how much rows were received use mysql_num_rows function.
if(mysql_num_rows($result) > 0) {
} else {
}
Also, you have error in your if:
if($result = false)
{
//do something
}
else
{
//print value already exists
}
You assign false to $result in your if statement.
You have to use if($result == false).
To avoid such mistakes you can change order:
if(false == $result)
This will work, but this:
if(false = $result)
Will cause error.
Hope, this will help.
This is a really simple one, I just can't get my head around it sorry. I have this PHP code which picks up my form value, then compares it with the value stored in the database. That works fine.
However I am not sure how to write this logic in terms of this query:
If posted value = database value {
// do something } else { // do something else }
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
$row = mysqli_fetch_assoc($result);
echo $row['order_id'];
}
SOLVED:
Solved the question, was a silly one I know! Just needed this at the end of the code:
if($order_id === $row['order_id']) {
echo 'found';
} else {
echo 'not found';
}
Try
If ($result->num_rows === 1) { do something } else { do something else }
Since you did the business logic in your query you can just use
if( ! is_null($row)) {
// do
} else {
// nothing
}
Did I read too much into "If posted value = database value "? Are you just referring to the order_id?
if ($row['listingName'] == $_POST['frm_listingName']) {
// something
}
else {
//something else
}
Check this code:
if (empty($_POST['order_id']) === false) {
// prepare data for inserting
$order_id = htmlentities(trim($_POST['order_id']));
$order_id = preg_replace("/[^0-9]/","", $order_id);
$result = mysqli_query($con,"SELECT * FROM listings WHERE order_id = $order_id");
if(mysqli_num_rows($result)>0)
{
//Match found. do something.
}
else
{
//No match found. do something.
}
}
N.B. In place of mysqli_num_rows($result) you can also use $result->num_rows
I am dying here :-) Please help out!
The following query gets different messages for different users from the same table based on current time.
$message = mysql_query("SELECT * FROM `table`
WHERE `Scheduled` <= DATE_ADD(UTC_TIMESTAMP(), INTERVAL 1 MINUTE)
AND `Status` != 'published'");
/** Kill db connection and exit script if no results are found **/
if (mysql_num_rows($message)== 0) {
mysql_close($db) && exit();
}
else {
while ($row = mysql_fetch_array($message))
{
$msg = $row["Messages"];
$accnt = $row["Account"];
{
if ($accnt == 'Account1')
echo $msg.$accnt;
elseif ($accnt == 'Account2')
echo $msg.$accnt;
else
echo "Nothing Here!";
}
}
}
This only echos the first account, please help I have a headache. I have run this on the db directly and it works fine. I believe I am messing up in php
I think you are getting non associative array. If you wanna assoc. array, you have to change it :
while ($row = mysql_fetch_array($message))
to
while ($row = mysql_fetch_assoc($message))
//you have made a drastic mistake in your code check now it will work
if (mysql_num_rows($message)== 0) {
mysql_close($db) && exit();
}
else {
while ($row = mysql_fetch_array($message))
{
$msg = $row["Messages"];
$accnt = $row["Account"];
if ($accnt == 'Account1')
echo $msg.$accnt;
elseif ($accnt == 'Account2')
echo $msg.$accnt;
else
echo "Nothing Here!";
}
}