Why variable value is not showing in insert query - php

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) { .. }

Related

PHP Simple If Statement Failing

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';
}

If or statement in php not evaluating correctly

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.

Why is my if statement not working the way I expect?

I am trying to achieve the following: I ask my SQL database a query using SELECT * FROM subjects. After doing that I ask for the array using mysqli_fetch_assoc. Until that point all is fine. The problem now is that when I try to modify in each loop the value of $genero depending if it's 1 or 0. But the value of $genero never changes, it's always 1 and I am sure that the array is fetching 0 and 1. Any idea while the values of $genero are not changing through the loop?
while ($subject = mysqli_fetch_assoc($result)) {
if ($subject["sexo"] = 1) {
$genero = "<img src='images/hombre.png' />";
} else {
$genero = "<img src='images/mujer.png' />";
}
echo $genero;
}
Your comparison operator is wrong. You're using = which is an assignment operator. In your example it will always be true. You need to use == which is a comparison operator.
if ($subject["sexo"] = 1) {
should be
if ($subject["sexo"] == 1) {
In if statement you should use double equal sign: if (a == b)

PHP/MySQL Checking for null

$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){
}

How to check whether column value is null or 0 in PHP?

$query = 'select column from table ...';
...
$row = mysql_fetch_assoc($result);
//now i have the result
//but how to check if it's null or 0?
What Pandiya was meant to say was is_null.
try
$res=mysql_query(" SELECT NULL AS anullcol, 0 AS anum, '' AS emptystring ");
var_dump(mysql_fetch_assoc($resource));
and read up on the '===' and '!==' operators.
C.
if($var == '' || $var == NULL){
//Code
}
You can use isset():
$foo = 0;
echo isset($foo) ? 'yes' : 'no';
echo "<br>";
$foo = null;
echo isset($foo) ? 'yes' : 'no';
will result in
yes
no
Maybe you are asking if the Query has returned anything. So try using:
mysql_num_rows()
from PHP:
Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
<?php
if ( mysql_num_rows ( $query ) == 0){
echo "duh, nothing!";
}
?>
maybe replace the null with an identifier in the select:
select coalesce(column, -1) as column
from table ...
that way you can test for 0 or -1 (NULL).

Categories