$sql = 'SELECT * FROM `phpbb_profile_fields_data`';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result)) {
if ($row['pf_kp_em_no_bonethr'] == '1') {
echo " Was 1";
} else if ($row['pf_kp_em_no_bonethr'] == '2') {
echo "Was 2";
} else {
echo "Was Neither 1 or 2";
}
}
$db->sql_freeresult($result);
I am curios, In my example I am checking the field for either a value of 1 or 2 but how do I check it for a value of NULL. Would it be any of the following three:
if ($row['pf_kp_em_no_bonethr'] == '')
if ($row['pf_kp_em_no_bonethr'] == '-1')
if ($row['pf_kp_em_no_bonethr'] == 'NULL')
Normally I would just try it out but I am not at home and wont be for the foreseeable future it has been bugging me. I am pretty sure it's not the second but I have seen -1 used for a null value in other languages. So can someone verify how I would indeed check for a NULL value please.
if ($row['pf_kp_em_no_bonethr'] === NULL)
Something like this should work.
if (is_null($row['pf_kp_em_no_bonethr'])) {
echo "Is NULL";
}
MySQL will return NULL values to PHP as actual PHP NULL. In this situation, what you need is:
// Notice lack of quotes around NULL
// And use === to distinguish type properly between integer 0 and NULL
if ($row['pf_kp_em_no_bonethr'] === NULL)
However, it would be more appropriate to check it in the query if NULL values are what you need to work with in PHP.
$sql = 'SELECT * FROM `phpbb_profile_fields_data` WHERE pf_kp_em_no_bonethr IS NULL';
Or to find all three values:
$sql = 'SELECT * FROM `phpbb_profile_fields_data`
WHERE pf_kp_em_no_bonethr IS NULL
OR pf_kp_em_no_bonethr IN (1,2)
';
I'd recommend to be very carfull with this one: I have seen
<?php
$field=$row['fieldname'];
if ($field===null) {
//Do something
}
?>
fail intermittently, especially on windows. This is why I prefer
SELECT
IFNULL(fieldname,'some_safe_value') AS fieldname
...
FROM
...
and the resulting trivial null-check.
Use is_null or === NULL.
if(is_null($row['pf_kp_em_no_bonethr'])){
}
or
if($row['pf_kp_em_no_bonethr'] === NULL){
}
Related
My if statements are not working despite the log file confirming that the values meet the conditions required.
As you will see below I have attempted to use both boolean and numerical values (as I have read that there are a few quirks with boolean statements in PHP.)
$lift = isset($p["lift"]) ? $p["lift"] : 0;
$parking = isset($p["parking"]) ? $p["parking"] : false;
// LIFT
if ( $lift === 1 && $home ) {
$query .= " AND `lift` == $lift";
}
// PARKING
if ( $parking === 1 && $home ) {
$query .= " AND `parking` != '';";
}
$log_file = "../../queries.log";
$error_message = "query: '$query' \n\n lift: ".$lift."\n home: ".$home."\n";
error_log($error_message, 3, $log_file);
I have tried both double and triple equal operators without success. I have tried both boolean and numerical values. The log statement prints the following:
'SELECT id, ref_crm, `type`, prov_name, prov_id, muni_name, muni_id, barrio, price_latest, photo,sqm,bed,bath,lift,parking,`year`,descr,
x(pt) as lat, y(pt) as lng, ref_cat FROM outlet WHERE prov_id = '06' AND `type` = 'Piso' AND price_latest >= 0 AND price_latest <= 500000 AND sqm >= 0 AND sqm <= 200'
lift: 1
home: true
As you can see, the string statements are not being attached to the query despite the two conditions both being met.
I have also tried removing the variables I've created ($lift and $home) and simply used $p["lift"] and $p["parking"] without success. The only way I am able to make this work is to specifically state $lift === 1 and $home === true (double or triple equal operators) above the conditions. This despite the log confirming that these variables already have those values set! I have also tried double and triple equal operators with $home and $p["home"]
Try echoing something out within your if statements.
Also please note:
https://www.php.net/manual/en/language.operators.comparison.php
Solution:
if (!empty($home) && $lift == 1) {
echo 'Lift works';
} else {
echo 'Lift is not 1';
}
if (!empty($home) && $parking == 1) {
echo 'Parking works';
} else {
echo 'Error: home parking is not 1';
}
I have this code snippet
$taskAssignedCompleteId = $row["TaskAssignCompletionId"];
echo "taskAssignedCompleteId::".$taskAssignedCompleteId. "\n";
if($taskAssignedCompleteId == 0 || $taskAssignedCompleteId = null)
{
echo "Currently no task assigned to you. If you are not doing a task on PMS and still receving this massage then, contact your team lead";
return;
}
$sqlInsert = "INSERT INTO emp_task_finished_request (EmpTaskAssignCompletionId, RequestDateTime) VALUES (".$taskAssignedCompleteId.", '$date')";
echo "output : ".$sqlInsert;
$queryInsert = $connPDO->exec($sqlInsert);
echo "\n output : ".$queryInsert;
Surprisingly $taskAssignedCompleteId value is not showing in query when i echo my $queryInsert varaiable while it is perfectly showing when i directly echo $taskAssignedCompleteId. Why is the problem? it is very strange for me.
here is my output
taskAssignedCompleteId::13
sqlInsert full : INSERT INTO emp_task_finished_request (EmpTaskAssignCompletionId, RequestDateTime) VALUES (, '2017-07-05 16:53:45')
output :
You need to correct your if statement. $taskAssignedCompleteId = null is not correct. Kindly replace it by following
if($taskAssignedCompleteId == 0 || $taskAssignedCompleteId == null)
Change $taskAssignedCompleteId = null to $taskAssignedCompleteId == null
By doing this, you are assigning taskAssignedCompleteId to a null value. To compare use "==" but you already know that. Just a typo I guess.
Darn, somebody beat me to the answer as I'm typing this.
you should use == for comparison operator . = is assignment operator .so it's assigning null to $taskAssignedCompleteId variable .
if($taskAssignedCompleteId == 0 || $taskAssignedCompleteId == null) { .. }
I have this PHP statement:
if (($row['rest'] != "") or ($row['rest'] != "Select An Option")) {
$rest = "<b>Rest Stops:</b> {$row['rest']},";
}
else {
$rest = "";
}
which is not evaluating properly and I can't figure out why. What I want the statement to do is if the field 'rest' is blank or "Select An Option" then the variable $rest should evaluate to "Rest Stops:" followed by the data. My data is "Select An Option" and I get "Rest Stops: Select An Option" as the output. I did some testing of this statement and I figured out PHP is assigning the variable $row['rest'] as not equal to "" instead of evaluating the 'or' statement. What would be the correct syntax?
What I want the statement to do is if the field 'rest' is blank or Select An Option than the variable $rest should evaluate to Rest Stops: followed by the data.
Your logic is incorrect. You need to check if they are equal to, so use == instead of !=.
if ($row['rest'] == "" || $row['rest'] == "Select An Option") {
^--------------------^ ^--------------------------------^
if field 'rest' blank if field is 'Select An Option'
This can be be improved by using empty() to perform the "is empty" check:
if (empty($row['test']) || $row['rest'] == "Select An Option") {
"If the field 'rest' is blank ...." - if that's true, then your logic is backwards:
if ($row['rest'] == '') || ($row['rest'] == 'Select an option') {
$rest = 'rest stops';
} else {
$rest = '';
}
Note the use of == for equality, rather than != for inequality.
A very simple query that I cannot seem to figure out...
I'm using the CodeIgniter framework.
I'm retrieving data from my database and accessing the cols within the returned row via:
$item->available
where 'available' is the column of type int.
Now, I'd like to check whether the returned integer is 1 or not.
I believed this would be a simple case of
if ($item->available == 1) {
echo "Available";
} else {
echo "Sold";
}
}
However, this is not working. Can somebody please offer me some direction?
== will only equivelent, so true == 1 '1' == 1 etc.. to match type use === this will ensure only (int)1 === (int)1
And for consistancy, use type-casting to ensure type like so...
if ((int)$item->available === 1) {
echo "Available";
} else {
echo "Sold";
}
The last hour I've been sitting with this problem. I have two if-statements (for testing purposes they are both IF-statements, and not IF- and ELSE IF-statements. The code runs the false IF-statement as if it is true.
The code:
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2)
{
echo $idkod . "==" . $alien2;
}
}
}
?>
This will give me the following text on screen:
1234567891234567891234567==1234567891234567891234567
1234567891234567891234567==1234567891234567891234568
Clearly, the second text shouldn't be there, as the statement is not true.
Don't assume anything when making conditional forks, use var_dump() on the variables to temporarily look inside them - that way you best decide how to check for the exact type and value you are expecting.
Then as said already, prefer to check using ===
If you adopt this behaviour you will save countless hours and avoid some quite subtle bugs which can appear in your code.
Having the PHP Truth Tables pinned up for a while will help.
== ignores type when testing for equality. In this case it will assume that both strings are numbers and convert them. This means this will turn into:
9223372036854775807 == 9223372036854775807 //Max int val. Will be different on different systems.
=== will make sure that both arguments are the same type and will not attempt to coerce making
'1234567891234567891234567' === '1234567891234567891234568';
Give the expected result.
PHP equality is wacky sometimes.
It is wrong to use == you need to use === the second is value comparison, the first is object comparison (depending on the context)
I have modified your code a little check it
<?php
$sth = $pdo->query("SELECT * FROM myDBTable WHERE alien1='$idkod' OR alien2='$idkod'");
$result = $sth->fetchAll();
if(!$result)
{
echo "No data";
}
else
{
foreach($result as $row)
{
$alien1 = $row['alien1'];
$alien2 = $row['alien2'];
if($idkod == $alien1 && $idkod != $alien2)
{
echo $idkod . "==" . $alien1;
}
if($idkod == $alien2 && $idkod != $alien1)
{
echo $idkod . "==" . $alien2;
}
}
}
?>