Function returning incorrect response - php

I've got an issue with a function where it's returning data from the first if() statement as opposed to the secondary if() statement in a function, please see below.
function training($type,$tid)
{
if($type = "id") {
$query = mysql_query("SELECT * FROM trainroster WHERE DATE = CURDATE()");
if (mysql_num_rows($query) == 1) {
$array = mysql_fetch_array($query);
$id=$array['TID'];
}
else { $id = "1"; }
return $id;
}
else if($type = "topic"){
$query = mysql_query("SELECT * FROM trainroster WHERE TID='$tid'");
$array = mysql_fetch_array($query);
$topic = $array['TOPIC'];
return $topic;
}
}
Which is being called like this:
$training = $connection->training("topic",$row['TID']);
When the function is called it's returning the $id as opposed to the $topic even though I'm setting the $type variable to "topic".
Any help greatly appreciated, cheers!

use '==' instead of '=' to compare
'=' is an assigning operator, if you use it to compare, it will always return true. That's why you are getting id always.
if($type == "id") {
....
if($type == "topic"){
....

if($type = "id") assigns the string "id" to $type and returns "id" which is true.
As mentioned by Harish Singh you have to write if($type == "id").
A way to prevent this error is to write if("id" == $type) which results in an error if you forget one "=".

Look at your if statements:
if($type = "id")
if($type = "topic")
You're assigning, not comparing. They should be:
if($type == "id")
if($type == "topic")

Related

Laravel Query in if else converting to common query

this is my code I want to convert this query in to a common query
if ($limit == 0) {
$response = WebhookErrorNotification::where('is_error', true)->orderByDesc('id')->get();
} else {
$response = WebhookErrorNotification::where('is_error', true)->orderByDesc('id')->limit($limit)->get();
}
You can save the query in a variable above the if/else statement. So something like this would work. Now if this is not the answer you are looking for please specify what you mean by common query!
$query = WebhookErrorNotification::where('is_error', true)->orderByDesc('id');
if ($limit == 0) {
$response = $query->get();
} else {
$response = $query->limit($limit)->get();
}
you can do something like this
$response = WebhookErrorNotification::where('is_error', true)
->when($limit !== 0, function ($query) use ($limit) {
$query->limit($limit);
})->orderByDesc('id')->get();

Inspite of 'if' command,selecting values only from school id 1

By using below mentioned commands,if school id is 1 or 3,these code only picking values from if ($sch = "1")......
$xyz5 = mysql_query("select id from schools where id='UserSchool'");
$xyz6 = mysql_fetch_row($xyz5);
$sch = $xyz6[0];
if ($sch = "1") {
$xyz = mysql_query("select phone from students where student_id='$student_id'");
$xyz2 = mysql_fetch_row($xyz);
$pno = $xyz2[0];
$sql = "INSERT INTO sms_fees (STUDENT_ID,SCHOOL_ID,MESSAGE,assigned_date,phone)
values('".$student_id."', '".UserSchool()."', '".str_replace("\'","'
'",$_REQUEST['
MESSAGE '])."', '".DBDate()."', '".$pno."')
";
DBQuery($sql);
}
elseif($sch = "3") {
$xyz1 = mysql_query("select phone from students where student_id='$student_id'");
$xyz12 = mysql_fetch_row($xyz1);
$pno1 = $xyz12[0];
$sql = "INSERT INTO sms_fees (STUDENT_ID,SCHOOL_ID,MESSAGE,assigned_date,phone)
values('".$student_id."', '".UserSchool()."', '".str_replace("\'","'
'",$_REQUEST['
MESSAGE '])."', '".DBDate()."', '".$pno1."')
";
DBQuery($sql);
}
It will never matches to 1. Please see your query
$xyz5=mysql_query("select id from schools where id='UserSchool'");
You are selecting id where id='UserSchool'
So the below code never matches to "1"
if ($sch = "1") {
Correct your query first.
You must use double equal sign! Your conditions are assignments and are always evaluating to true (non empty strings), so conditions other than first are ignored.
Change it to that:
if ($sch == "1") {
Or even to that if you know your variable is always string not int:
if ($sch === "1") {
You need to use "=="
if ($sch == "1") {
if ($sch == "3") {
The reason why it is entering first if condition is because you are assigning 1 to $sch and if evaluates to true, hence the control enters the first if condition
Please try this :
if ($sch == "1")
{
// do something
}
elseif($sch == "3")
{
// do something
}
else
{
// do something
}

how to insert a string in database if value is null

In my bind parameters, I am trying to insert answers for each question. I have a simple question which is asking that if a question has no answer, how can I insert the word No Answer in its place in the db? This is because at the moment it will not insert any rows are where the answer is NULL. It only does insert if the question has at minimum one answer
Below is attempt:
$results = $_POST['value'];
foreach($results as $id => $value)
{
$answer = $value;
$quesid = $question_ids[$id];
if($answer = ''){
$answer = 'No Answer';
}
foreach($value as $answer)
{
$insertanswer->bind_param("is", $quesid, $answer);
$insertanswer->execute();
if ($insertanswer->errno) {
// Handle query error here
echo __LINE__.': '.$insertanswer->error;
break 7;
}
}
}
If it is a text input then if($answer == '' || $answer === null){, if it i from a drop down menu then retrive the value: if($answer == ''){ //'' is whatever the value is for this option
Checking if its empty is not enough, you should check for null just incase!
//Take notice of === (Identical operator)
if($answer == '' || $answer === null){
$answer = 'No Answer';
}
its looks like you are using assignment operator (=) in if condition i think it should be comparison operator (==)
if($answer = ''){
$answer = 'No Answer';
}
should be
if($answer == ''){
$answer = 'No Answer';
}
or
if(trim($answer) == '')
or
if(empty($answer)){
also if you want to make sure that $question_ids[$id] is int you can use
$quesid = (int)$question_ids[$id];

php if condition

I can't find anything wrong with this... This should work right?
function ConfirmedNumber()
{
$rs = mysql_query("CALL ConfirmedNumber('" , $_SESSION['UserID'] . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] = 1)
{
return true;
}
}
return false;
}
Assuming the Stored procedure returns a single row with the value '1' in it then I can call the function like this right?
if (ConfirmedNumber())
{
//do some stuff.
}
To expand on my comment:
if ($row['Active'] = 1) should be if ($row['Active'] == 1) to work correctly.
If you want to avoid accidentally doing this in future, you could write your if statements like this:
if (1 == $row['Active'])
This way, you can't accidentally use = as PHP will throw a Fatal Error. You can read more about Comparison Operators at PHP.net
Comment below with the full answer:
The call to the stored proc... line $rs = mysql_query("CALL ConfirmedNumber('" . $_SESSION['UserID'] . "',#Active)"); had a comma instead of the period in the initial post.
you forgot your operator in your IF statement. Change it to this:
if ($row['Active'] == 1)
or even shorter
if ($row['Active'])
it can be something like this
function ConfirmedNumber($id)
{
$rs = mysql_query("CALL ConfirmedNumber('" . $id . "',#Active)");
while ($row = mysql_fetch_assoc($rs))
{
if ($row['Active'] == 1)
{
return true;
}
}
return false;
}
ConfirmedNumber($_SESSION['UserID']);

Update statement not working

function add_new($father,$chName) // add new category
{
if($father = "1" ) {
$result = mysql_query("INSERT into stinky_menu (title,nest_under)
VALUES('".$chName."','1')");
}
else {
$result = mysql_query("UPDATE stinky_menu SET title = '$chName' nest_under = '$father'");
}
}
I am getting the value of father from parent page, but its not going to else condition if its not equal to one.
You’re using the assignment operator = rather than the comparison operator ==. So try this:
if ($father == "1") {
// …
} else {
// …
}
That's because you have
if($father = "1")
You need to use "==". "=" is the assignment operator. You are setting $father equal to "1" even when it isn't.
Try:
if ($father == 1){}
Read here about comparison operators. "=" is the assignment operator.
Look at this to see what your code does:
<?php
$father = 55;
if ($father = 1){}
else{}
echo $father;
?>
This prints "1".
Also, should not that last query be:
"UPDATE stinky_menu SET title = '$chName', nest_under = '$father'"

Categories