I have two put two condition in the result of a sql query result for the below function:
Table projects has two columns a_name and b_name and I want to exclude result in which b_name has value = xxxxx and value in a_name and b_name are same.
Below is currently used:
public function search_project_name($term)
{
$params = array( ':term' => $term);
$sql = "SELECT * FROM projects WHERE project_name like '%$term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[]=$row;
endwhile;
return $result;
}
You could add these condition with a not operator:
SELECT *
FROM projects
WHERE a_name like '%$term%' AND
NOT (a_name = 'xxxx' and a_name = b_name)
Problem part is $term in query so replace $term with :term try updated code:
public function search_project_name($term){
$params = array(
':term'=>$term);
$sql = "SELECT * FROM projects WHERE project_name like '%:term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[] = $row;
endwhile;
return $result;
}
One more missing parameter is $limit you used in query but not passed to function?
With $limit as 2nd parameter default set to 10 rows:
public function search_project_name($term, $limit = 10){
$params = array(
':term'=>$term);
$sql = "SELECT * FROM projects WHERE project_name like '%:term%'ORDER BY projects.create_date DESC $limit";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
$result[] = $row;
endwhile;
return $result;
}
Related
$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result = $this->doSelect($sql);
$result[$key]['idduc'] = $result;
}
return $result;
}
You are looping on a variable called $result, from the first query.
In the second query you destroy $result by reusing it in the second query processing. Also you then destroy that $result by assigning things to it as an array you want to return.
So use a different variable for the inner look like this
$sql = "select * from tbl_user";
$result = $this->doSelect($sql);
foreach ($result as $key => $row) {
$sql = "select * from tbl_comment order by id desc";
$result1 = $this->doSelect($sql);
$return[$key]['idduc'] = $result1;
}
return $return;
}
I would like to secure my requests in my code.
Today my curent functions are like this.
public function UpdatePMS($table,$data,$where) {
$ret = array();
$set_data = "";
foreach($data as $key => $value){
$set_data .= $key."= '".$value."', ";
}
if (isset($where)) {
$where = "WHERE ".$where;
}
$sql = "UPDATE ".$table." SET ".$set_data."".$where;
$sql = str_replace(", WHERE", " WHERE", $sql);
$stm = $this->db->prepare($sql);
$ret = $stm->execute();
return $ret;
}
With this way, i can select any tables, any datas, and any conditions.
For example:
WHERE id = 1 and status < 10
Or only
WHERE id = 10
Or sometimes
WHERE id = 1 and status >= 5
The content of where could change.
A kind of universal request.
Same for Delete, Update, Select, insert.
I tried to do like this, but it doesn't work.
$db = new PDO('mysql:host=localhost;dbname=asterisk','root','');
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
$ret = $stm->fetchall(PDO::FETCH_ASSOC);
Any ideas?
Frankly, you cannot use prepared statements this way. There are rules to follow. So it just makes no sense to write something like this
$table = "my_table";
$where = "WHERE id = 1";
$sql = 'SELECT * FROM :table :where';
$stm = $db->prepare($sql);
$stm->execute(array(":table" => $table, ":where" => $where));
instead you should write this code
$sql = 'SELECT * FROM my_table WHERE id = ?';
$stm = $db->prepare($sql);
$stm->execute(array($id));
Besides, you cannot parameterize table and field names, so it's better to write them as is.
so i need to make one function per different requests, right?
Honestly - yes. It will spare you from A LOT of headaches.
public function UpdatePMS($data, $id)
{
$data[] = $id;
$sql = "UPDATE table SET f1 = ?, f2 = ? WHERE id = ?";
$stm = $this->db->prepare($sql);
$ret = $stm->execute($data);
return $ret;
}
which is going to be used like
$obj->UpdatePMS([$f1, $f2], $id);
I'm trying to fetch a list of values from MySQL table using query1 and then use them in query2 to fetch values. Query1 gives 4 values however Query2 gives output matching the last value of Query1.
Following is my controller code
public function example_ctl(){
$data['result'] = $this->some_model->example();
}
Following is my model code
public function example() {
$query = "select m.queue_id from agent_queue_mapping_table as m, user_table as u where u.id=m.user_id and m.company_id = ".$this->session->userdata('user_comp_id')." and u.id = ".$this->session->userdata('user_id');
$res = $this->db->query($query);
foreach ($res->result_array() as $value) {
$queue_ids = implode(',', $value);
}
$query_ticket = "select * from tickets_table where company_id = ".$this->session->userdata('user_comp_id')." and ticket_status = 'New' and queue_id IN (".$queue_ids.") ORDER BY id DESC LIMIT 3";
$res_ticket = $this->db->query($query_ticket);
return $res_ticket->result_array();
}
I'm not able to understand where I'm going wrong. Please help.
try changing your code into this
<?php
$queue_id = array();
foreach ($res->result_array() as $value) {
$queue_id[] = $value;
}
$queue_id = implode(",",$value);
?>
$queue_id is overridden inside the loop each time loop executes that is why you get last value
This is how I fixed this issue.
$query = "select m.queue_id from agent_queue_mapping_table as m, user_table as u where u.id=m.user_id and m.company_id = ".$this->session->userdata('user_comp_id')." and u.id = ".$this->session->userdata('user_id');
$res = $this->db->query($query);
foreach ($res->result_array() as $value) {
$queue_ids[] = $value['queue_id'];
}
$queue_id = implode(',', $queue_ids);
$query_ticket = "select * from tickets_table where company_id = ".$this->session->userdata('user_comp_id')." and ticket_status = 'New' and queue_id IN (".$queue_id.") ORDER BY id DESC LIMIT 3";
$res_ticket = $this->db->query($query_ticket);
return $res_ticket->result_array();
I have a table and i want to echo the 2 last rows of my tabel, I used the below code but just the last one showed, what is the problem.
$result1 =(mysql_fetch_array(mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")));
Print $result1['time'];
mysql_fetch_array = 1 fetch.
do it again for fetching 2nd result.
Also, use mysqli eh.
You're doing mysql_fetch_array only one time, so it gets the first element. If you want to get all the elements, then do it again, or use a loop.
Something like:
$query = "SELECT * FROM $table ORDER BY id DESC LIMIT 2";
while($row = mysql_fetch_array(mysql_query($query) )
{
echo $row['time'].'<br>';
}
For 2 Or more rows you need to loop it
$sql = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2")
while($row=mysql_fetch_array($sql))
{
echo $row['time']."<br>";
}
$query = mysqli_query("SELECT * FROM $table ORDER BY id DESC LIMIT 2");
while ($result = mysqli_fetch_array($query)) {
echo $result['time'];
}
Gives out every return of your database (2 in this case). You should use mysqli_-functions.
Maybe you should try like this, since mysql_fetch_array returns only one row
while ($row = mysql_fetch_array($yourQuery)) {
echo $row["yourAlias"];
}
Further details here : http://fr2.php.net/manual/en/function.mysql-fetch-array.php
My solution:
$limit = 2;
$sql = "SELECT * FROM $table ORDER BY id DESC LIMIT $limit";
$result = mysql_query($sql);
$array = array(); $i = 0;
while($row = mysqli_fetch_array($result))
{
$array[$i] = $row;
$i++;
}
var_dump($array[0]);
var_dump($array[1]);
I am trying to run 2 database querys the second using an array from the first, any one have any ideas why this stops the page from loading?
$query = $this->db->query("SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1");
foreach ($query->result() as $row)
{
$query2 = $this->db->query("SELECT listing_title FROM listings WHERE listing_type = '".$row->category_id."' ORDER BY RAND() LIMIT 2");
foreach ($query2->result() as $row)
{
echo $row->listing_title;
}
}
You are overwriting the outer $row in your inner loop
$query = $this->db->query("SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1");
foreach ($query->result() as $row)
{
$query2 = $this->db->query("SELECT listing_title FROM listings WHERE listing_type = '".$row->category_id."' ORDER BY RAND() LIMIT 2");
foreach ($query2->result() as $row2) // <--- $row2 not $row
{
echo $row2->listing_title;
}
}
Try this:
(Take advantage of prepare statements, the prepare() function, etc)
$query = "SELECT category_id FROM listings WHERE listing_id = '1' LIMIT 1;";
$query2 = "SELECT listing_title FROM listings WHERE listing_type = :categoryId ORDER BY RAND() LIMIT 2;";
$db = $this->db;
$statement = $db->prepare($query);
$statement->execute();
$statement->setFetchMode(PDO::FETCH_ASSOC);
while($row = $statement->fetch())
{
$categoryId = $row['category_id'];
$statement2 = $db->prepare($query2);
$statement2->execute(array(':categoryId' => $categoryId));
$statement2->setFetchMode(PDO::FETCH_ASSOC);
while($row2 = $statement2->fetch())
{
$listingTitle = $row2['listing_title'];
echo $listingTitle;
}
}
This is not very good code. Why use a foreach loop when the Select query has Limit 1?
Since you are going for a category id where listing_id=1 (with LIMIT 1), your code could just be shortened to:
SELECT listing_title FROM listings WHERE listing_id=1 ORDER BY RAND() LIMIT 2
Also, ORDER BY RAND() is a big resource killer on large tables. I recommend finding a more proper way to order your results.
EDIT: Full code that I would use:
$db=$this->db; //Just so we don't have to keep referencing $this (assuming you are not in a class)
$sql="SELECT listing_title
FROM listings
WHERE listing_id=?
ORDER BY RAND()
LIMIT 2";
$statement=$db->prepare($sql);
$statement->execute(array(1));
while($result=$statement->fetchObject()){
echo $result->listing_title;
}