Using a Variable in WHERE clause does not work - php

I'm running a simple query that is working, that is, without a PHP variable in the WHERE clause.
However, when I insert the variable, it does nothing.
<?php
$query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= $student_value->id and `exam_group_class_batch_exam_subject_id`=1");
$getrem = $query->row();
echo $getrem->rem1;?>
But when I insert just a value, everything works
<?php
$query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= 11 and `exam_group_class_batch_exam_subject_id`=1");
$getrem = $query->row();
var_dump($getrem);?>
I var_dump this variable student_value['id'] and it printed out the correct value which is 11.
I've been at this for hours. Please help

Try to assign the value of your object to another variable.
Example
$id_value = student_value['id'];
Then use the variable in your SQL code
$query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= $id_value and `exam_group_class_batch_exam_subject_id`=1");

It's probably a syntax problem
$query = $this->db->query("SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`= ".$student_value->id." and `exam_group_class_batch_exam_subject_id`=1");
Otherwise, check whether your variable is an object or an array

I think the query syntax is not read the Student ID, so the result will show as is WHERE CLAUSE.
// Try this
$studentId = $student_value->id;
$sql = "SELECT `rem1` FROM `exam_group_exam_results` WHERE `exam_group_class_batch_exam_student_id`=`$studentId` and `exam_group_class_batch_exam_subject_id`=1"
$query = $this->db->query($sql);

Related

Check if a value exists in a result_array()

I have the following query ....
$query = "SELECT * FROM crescent.main_stock where transaction_type like '%Receive%' LIMIT 0,1;";
$query = $this->db->query($query);
$result = $query->result_array();
if (in_array('Receive', $result)) {
echo 'this array contains Receive';
}
I am trying to check if a value Receive exists inside the result_array(), What is the best way to implement this? Which function should I use to check ?
If you want to know if your query returned a value in the form of an array, you can put this at the end of your function:
$result = $query->result_array();
print_r($result);

How to take value from array into integer (PHP)

Input :
$sql1 = "SELECT COUNT(*) FROM matchTrip where userTripId = :tripId";
$stmt1 = $this->db->prepare($sql1);
$stmt1->bindParam(':tripId', $trip, PDO::PARAM_INT);
$temp = $stmt1->fetchObject();
echo(json_encode($temp));
Output:
How to take value from array :
of which json_encode looks like this: {"COUNT(*)":"7"}
Any help will be appreciated.
Why don't you just give the column an alias in the SQL itself?
$sql1 = "SELECT COUNT(*) as myCount FROM matchTrip where userTripId = :tripId";
Makes the rest easier to work with.
Do you mean json_decode? You can just put it between quotes and it should work; $array["COUNT(*)"].
But you can also add "AS myCount" to your SQL.
if to get rid of all the useless stuff from your code
$sql = "SELECT COUNT(*) FROM matchTrip where userTripId = ?";
$stmt = $this->db->prepare($sql);
$stmt->execute(array($table_of_user[$i]));
$count = $stmt->fetchColumn();
echo $count;
So why not just fecth as array?
$temp = $stmt1->fetch(PDO::FETCH_ASSOC);
echo $temp['COUNT(*)'];
Just use like this:
$json = json_encode($temp);
echo $json->{'COUNT(*)'}; // 7

php variable in a select query

I am pretty sure this is a syntax problem, but there may be other issues as well.
Ultimately, I'm trying to create a variable from an array that will be used in an insert statement, but I can't get past a select statement with a variable.
For a while I had $country_id = $coun; as $country_id = mysqli_real_escape_string($coun); but the var_dump suggested that the mysql_real_escape_string was preventing $country_id from taking on the value of $coun.
When I check for errors on $q, the query it spits out works just fine on phpmyadmin, yet the array outputs a NULL value.
I'm stumped.
Here is the code:
//get short_name variable
$country_id = $coun;
//var_dump($coun, $country_id);
$q = "SELECT short_name FROM country WHERE country_id = $country_id LIMIT 1";
$r = mysqli_query($dbc, $q);
$num = mysqli_num_rows($r);
if ($num > 0) {//match was made
//Get short_name
$row = mysqli_fetch_assoc($r, MYSQLI_ASSOC);
//var_dump($row);
}else {
echo '<p>no match</p>';
}
I just got some help and it turns out that the problem was that "assoc" should have been "array." it looks like this now. $row = mysqli_fetch_array($r);

Issue with mysql query when changing value to a variable

$results = mysql_query("select * from doctorlist where assignednumber = '1231231234' ");
I need to change the number 1231231234 to a variable. If I change it to the code below it does not work. I have displayed the variable on the page so I know it is set.
$results = mysql_query("select * from doctorlist where assignednumber = '$phoneNumber' ");
Could someone please help. I know it is a small issue, but have been unable to fix it.
Perhaps split it like this
$sql_query = "select * from doctorlist where assignednumber='$phoneNumber'";
$results = mysql_query($sql_query);
or
$sql_query = "select * from doctorlist where assignednumber='".$phoneNumber."' ";
$results = mysql_query($sql_query);
First check your variable type with var_dump($phoneNumber) than do the following:
$results = mysql_query("select * from doctorlist where assignednumber = '".$phoneNumber."' ");
to improve readability and last if you expect an Integer cast your variable like:
(int)$phoneNumber
or if string do
mysql_real_escape_string($phoneNumber)
Try using the variable inside the query like this:
'{$phoneNumber}'

Multiple Conditions in Where Clause, breaking with second condition

This is a very simple issue. I must just be doing something stupid:
This query echos out a row id number:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
When I add in an additional condition (even though the condition is DEFINITELY met in the SQL database the the echo does produce anything (i.e. the variable is empty). The failing code is:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer = NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
You can't use
reviewer = NULL
You need to use
reviewer IS NULL
NULL is an undefined value, so it isn't equal to anything; so you need to use IS to look for it.
you should use
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer IS NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
I hope it works.
As far as I know null values are verified with IS
... where xxx IS null
... where xxx IS NOT null

Categories