This is my query
select count(payment_id) from payment where booking_id='$booking_id';
When i run this query in phpmyadmin i get count value 2 but i want access that value..
$sql="select count(payment_id) from payment where booking_id='$booking_id'";
$query = $this->db->query($sql);
echo $query->num_rows();
if($query->num_rows() > 0){
do something....
}
If count is 1 then do something and if count is greater than 1 then do something...
Try this:
$sql="select count(payment_id) as count_value from payment where booking_id='$booking_id'";
$query = $this->db->query($sql);
foreach ($query->result() as $row)
{
$count = $row->count_value;
}
if($count> 0){
do something....
}
count('coulme name') means, It always return result as a single row
Count() always returns 1 row. you need to do
$row = $query->result();
and then access the value of count from there...
This is a model function you can do it inside or return the count from the function and do the actions in your controller.
function count_payer($booking_id) {
$this->db->select ( 'payment_id' );
$this->db->from ( 'payment' );
$this->db->where ( 'booking_id', $booking_id );
$query = $this->db->get ();
$value = $query->num_rows ();
if ($value == 1) {
// do something and return
} else {
// do something else and return
}
}
If you want payment id then the query would be:
select sql_calc_found_rows payment_id from payment where booking_id='$booking_id';
select FOUND_ROWS();
Related
I have 2 table
Donasi and sistem_list_Date.
This is my query
$this->db->SELECT("sistem_list_tanggal.tanggal, COUNT(id) AS jumlah")
->FROM('sistem_list_tanggal')
->JOIN('donasi','donasi.tanggal_ambil=sistem_list_tanggal.tanggal','LEFT')
->WHERE('sistem_list_tanggal.tanggal >=', '2019-12-01')
->WHERE('sistem_list_tanggal.tanggal <=', '2019-12-31')
->WHERE('status',1)
->GROUP_BY('sistem_list_tanggal.tanggal');
$query = $this->db->get();
$output = array('data' => array());
if($query->num_rows() > 0){
return $query->result_array();
}else{
return array();
}
Why this query returning only date with value (jumlah).
I want query return all dates 2019-12-01 until 2019-12-31, if null data return 0
I have followed the tutorial here https://stackoverflow.com/a/14336188/11540418
YOU have to use left join for the same query
I'm trying to do a filter search functionality in codeigniter. I have a table named products and my system has the functionality to filter these products by category and by date. I have a mysql code in mind which looks something like this:
SELECT * from products WHERE product_category='Cloth'
INTERSECT
SELECT * from products WHERE ('insert date logic here')
So it should return records (via id) from the same table named products. However, there's no INTERSECT in mysql so I don't know how to do this. Any help would be greatly appreciated!
This is my code just for the part of the product category
$this->db->limit($limit,$start);
$query = $this->db->query('SELECT * from product_advertised WHERE quantity > 0 AND prodcatid='.$prodcats[0].' LIMIT '.$start.','.$limit);
if(sizeof($prodcats > 1)) {
$query_str = "SELECT * FROM product_advertised WHERE quantity>0 AND ";
$str="";
for($i = 0;$i < sizeof($prodcats);$i++) {
if($i != sizeof($prodcats)-1) {
$str = $str. "prodcatid=".$prodcats[$i]." OR ";
}
else {
$str = $str. "prodcatid=".$prodcats[$i]." LIMIT ".$start.",".$limit;
}
}
$query_str .= $str;
$query = $this->db->query($query_str);
}
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
Why wouldn't you just use and?
SELECT *
from products
WHERE product_category = 'Cloth' AND
('insert date logic here');
I'm new to php and I've searched for the past hour and read all the documentation I could find and nothing is helping. I have a table that has a bunch of rows of data. I'm trying to pick one column from the whole table and add them all together. Here is what I got. All this tells me is how many rows there are that match my query, not the total sum of column I want. Any help is appreciated.
$res1 = $db->prepare('SELECT sum(distance) FROM trip_logs WHERE user_id = '. $user_id .' AND status = "2"');
$res1->execute();
$sum_miles = 0;
while($row1 = $res1->fetch(PDO::FETCH_ASSOC)) {
$sum_miles += $row1['distance'];
}
echo $sum_miles;
You're only returning one row in this instance. Modify your summed column to have an alias:
SELECT SUM(distance) AS totDistance FROM trip_logs ....
Now you can can fetch the row -
$row = $res1->fetch(PDO::FETCH_ASSOC);
echo $row['totDistance'];
No need to loop.
You can use SUM() without explicitely grouping your rows because if you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
If however you want to use the SUM() function for something slightly more complicated you have to group your rows so that the sum can operate on what you want.
If you want to get multiple sums in a single statement, for example to get the distance for all users at once, you need to group the rows explicitely:
$res1 = $db->prepare("
SELECT
SUM(distance) AS distance,
user_id
FROM trip_logs WHERE status = '2'
GROUP BY user_id
");
$res1->execute();
while ($row = $res1->fetch(PDO::FETCH_ASSOC))
{
echo "user $row[user_id] has runned $row[distance] km.\n";
}
This will return the sum of distances by user, not for all users at once.
Try this if you are using a Class :
class Sample_class{
private $db;
public function __construct($database) {
$this->db = $database;
}
public function GetDistant($user_id,$status) {
$query = $this->db->prepare("SELECT sum(distance) FROM trip_logs WHERE user_id =? AND status =?");
$query->bindValue(1, $user_id);
$query->bindValue(2, $status);
try{ $query->execute();
$rows = $query->fetch();
return $rows[0];
} catch (PDOException $e){die($e->getMessage());}
}
}
$dist = new Sample_class($db);
$user_id = 10;
$status = 2;
echo $dist->GetDistant($user_id,$status);
Earlier this day a asked a question about an update query. But now i want to select some things ( and it is working ) but I also want to order them and put a limit on it.
This is the code to select all the food :
public function getFood($id)
{
$id = (int)$id;
$rowset = $this->tableGateway->select(array('kindOfFood_id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
But how can i do this :
Select * from KindOfFood ==> order by kindOfFood_votes DESC ?
I saw on the documentation you can do something like this, but it doesn't work with me?
$rowset = $artistTable->select(function (Select $select) {
$select->where->like('name', 'Brit%');
$select->order('name ASC')->limit(2);
});
Are you looking to return only single row or multiple rows.
Try this for multiple rows -
use Zend\Db\Sql\Select; //at the top of the page among other use statements.
public function getFood($id)
{
$id = (int) $id;
$select = new Select(TABLE_NAME); //CHANGE TABLE_NAME as per needs
$select->where('kindOfFood_id = ' . $id);
$select->order('kindOfFood_votes DESC');
$resultSet = $this->tableGateway->selectWith($select); //Will get array of rows.
//$row = $rowset->current(); THIS IS FOR RETURNING ONLY SINGLE ROW NOT ALL ROWS
if (!$resultSet) {
throw new \Exception("Could not find rows with food id - $id");
}
return $resultSet;
}
Can access the returned resultSet via loop. Eg: foreach
foreach($resultSet as $row) {
echo $row->kindOfFood_id; //or something
}
Note:
If you need only
Select * from KindOfFood order by kindOfFood_votes DESC
then remove the $select->where('kindOfFood_id = ' . $id); line from above.
My simple query:
$list = Doctrine_Query::create()
->from('liste l')
->where('l.id =?', $id)
->fetchOne();
$id = 123;
I know that there is no entry with the $id =123 in my database. When I count $list, I get the result 1. How do I know with my query or the result of my query that there is no entry with the $id = 123 in my database?
var_dump gives me false!
So I just do a quick:
if ($list == FALSE) {
....}