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);
Related
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);
I am trying to fetch user
function getItemName($dbh, $userId) {
$itemId = getItemId($dbh, $userId); // the getItemId() function works
echo "item id is: " . $itemId ; // because I can see the correct result if I echo it
$sql = "SELECT name FROM items WHERE id = :item_id";
$stm = $dbh->prepare($sql);
$stm->bindParam(':item_id', $itemId, PDO::PARAM_INT);
$stm->execute();
$result = $stm->fetch();
return $result['name'];
}
And I get Trying to access array offset on value of type bool on the return $result['name']; line.
The field name exists on the items table so that's not the issue.
Also, when I try to further test it, I change the $sql statement to SELECT * FROM items and then when I do echo $stm->rowCount() it finds the correct number of rows (With the original SQL statement row count is 0)
Can't find out what's causing this
I have 3 suggestions:
Make sure to convert $itemId to integer using intval();
Just before returning the function result validate that the query returned results.
$result = $stm->fetch();
if(!$result){
return null;
}
return $result['name'];
Finally, the more obvious, make sure the itemId you are looking for exists in the DB.
I have a function
function get_table_content(){
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = $this->query($sql);
while ($row = $this->fetch($result)){
return ($row);
}
}
The problem is, when i call this function it only return the first entry in my database.
How can i make this work it suppose to return about all the data in the table.
I have also tried this and it is not working too
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
while ($row = mysqli_fetch_assoc($result)){
return ($row);
}
}
I think the best way to go about this is to return an array and do my foreach operation on it but i can not get it to work.
Can someone help me out Please.
P.S i will preffer it to return an array of all the data in the data so i can do a foreach operation on it.
Thank you
return immediately stops execution of your function during the first iteration of the loop. To get all records and return them:
$result = mysqli_query($database_connection, $sql);
return mysqli_fetch_all($result);
Side note: This is less efficient then returning a result set resource and looping through it as needed outside your function. fetch_all must convert all results to array, then presumably you're looping through this array later.
The problem is, in the while loop after fetching one row from the result set you're returning the result from the function, and hence you're getting only the first row from the result set. Either use mysqli_fetch_all() function or an array in the while loop to store the result.
Method(1):
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
return mysqli_fetch_all($result);
}
Method(2):
function get_table_content(){
global $database_connection;
$sql = "SELECT * FROM my_table WHERE id > 0";
$result = mysqli_query($database_connection, $sql);
$r = array();
while ($row = mysqli_fetch_assoc($result)){
$r[] = $row;
}
return $r;
}
You have a return inside of your while loop, which is terminating that loop and returning the first row of data.
I'm not sure if this is doable or not, and I'm not entirely sure how to search for this. I have several dynamic web pages that all link to the same MySQL database table, but pull different results. So for example, a dynamic web page with ID = 5 will run a query like:
SELECT * FROM myTable WHERE category1 = 1
The web page where ID = 7 will run:
SELECT * FROM myTable WHERE category2 = 1
And so on. The queries are all grabbing the data from the same table, but the WHERE clause is different for each query - its not looking at the same column. The page with ID 7 should ONLY be returning results where category2 = 1, and ignoring the results that would be returned for the page with id = 5. My website has about 20 different pages/queries like this which is why I'm looking to see if it can be done in a function instead.
Is there a way I can put that into a function, and if so, how would I set up the parameters correctly? Or is this an instance where I will have to just write out all the queries separately on each page?
function find_results(what to put here?) {
global $connection;
$query = "SELECT * FROM myTable WHERE (how to code this part?)";
$result = mysqli_query($connection, $query);
confirm_query ($result);
return $result;
}
You would add the necessary parameters to your functions argument list, then provide the values at runtime.
function find_results($column, $value)
{
global $connection;
$query = "SELECT * FROM myTable WHERE {$column} = $value";
$result = mysqli_query($connection, $query);
confirm_query ($result);
return $result;
}
//Usage:
$result = find_results("category2", 1)
If the value you are returning records by ever ends up being a string make sure your wrap $value in single quotes.
if its a constant relation between pageId and categoryId, you can just create an array to hold it indexed by pageId like:
$pageIdToCategoryMapping = [
1 => 'cateogory1',
2 => 'category5',
...
]
and then just use it to pass data to your function like
find_results($pageIdToCategoryMapping[$pageId])
function find_results($category) {
(...)
$query = "SELECT * FROM myTable WHERE ({$category} = 1)";
(...)
}
I have been using class and object methods for mysql operations. source code available in github
I would recommend you to pass array as an argument and can return query or result as array in format you required. And this function will work any number or condition
<?php
$arg['db']="database";
$arg['tabe']="table";
$arg['search']['id1']="id1";
$arg['search']['id2']="id2";
//
function searchAndReturnResultAsArray($arg)
{
$return = NULL;
$query="SELECT * FROM ".$arg['table'];
$flag=false;
foreach($arg['search'] as $key=>$value)
{
if($flag)
$query.=" AND ";
else
$flag=true;
$query.= $key." = '".$value."' ";
}
$row = mysqli_num_rows($query);
$field = mysqli_fetch_object($query);
if($row >= 1)
{
while($data = mysqli_fetch_array())
{
$return[] = $data;
}
}
return $return;
}
?>
Or alternatively you can just return query once it is ready.
I have the following code and it should return just one value (id) from mysql table. The following code doesnt work. How can I output it without creating arrays and all this stuff, just a simple output of one value.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = map_query($query);
echo $result;
I do something like this:
<?php
$data = mysql_fetch_object($result);
echo $data->foo();
?>
You have to do some form of object creation. There's no real way around that.
You can try:
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
//$result = map_query($query);
//echo $result;
$result = mysql_query($query); // run the query and get the result object.
if (!$result) { // check for errors.
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result); // get the single row.
echo $row['id']; // display the value.
all you have is a resource, you would still have to make it construct a result array if you want the output.
Check out ADO if you want to write less.
Not sure I exactly understood, what you want, but you could just do
$result = mysql_query('SELECT id FROM table WHERE area = "foo" LIMIT 1');
list($data) = mysql_fetch_assoc($result);
if you wish to execute only one row you can do like this.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0];
there have been many ways as answered above and this is just my simple example. it will echo the first row that have been executed, you can also use another option like limit clause to do the same result as answered by others above.