I want to fetch intermediate rows from by database.
Like for last 10 rows i will use limit :
return Doctrine_Query::create()
->select('v.*')
->from('Video v')
->where("v.community_id='$community_id' AND v.user_id='$user_id' AND v.published='$published'")
->orderBy('v.id DESC')
->limit(10)
->execute();
but what if i want 110-120 rows?Can anybody tell me about that? how to write this kind of query in doctrine
Use the offset() clause.
You could use a Doctrine_Pager
$page = 10;
$limit = 10;
$query = Doctrine_Query::create()
->select('t.*')
->from('SomeTable t')
$pager = new Doctrine_Pager(
$query,
$page,
$limit
);
$rows = $pager->execute();
For rows 110-120, you want
LIMIT 109, 10
use offset clause...chk dis
Related
Well, I have a list of jobs that I want to display so candidates can apply for the job. The problems is, the listing keep showing the old jobs and not the new one. I try to put the DESC query but it's become error.
This is what I have on my database;
and when I input the original query, it's working
original;
<?php
$limit = 4;
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
$row = $result->fetch_assoc();
$total_records = $row['id'];
$total_pages = ceil($total_records / $limit);
} else {
$total_pages = 1;
}
?>
and this one is after I add the DESC
$sql = "SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY DESC";
but this error came out
Trying to get property of non-object in C:\xampp\htdocs\jobportal\jobs.php on line 148
and the result doesn't come out in descending order.
what should I do to resolve this?
Thank you in advance.
The Syntax is:
SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY column1 DESC;
Here column1 refers to the column that is to be sorted.
You need column name in your order by clause.
SELECT COUNT(id_jobpost) AS idCount FROM job_post ORDER BY idCount DESC
you have missed the column you want to order your result by:
"SELECT COUNT(id_jobpost) AS id FROM job_post ORDER BY id DESC"
I want make a pagination than i find this problem. pagination limit query not working properly.
if($this->input->get('page'))
{
$start = abs(($this->input->get('page', TRUE)-1)*$config['per_page']);
}else{
$start = 0;
}
$condition = array();
if (preg_match('/WMV/', $whatFind)) {
$condition = array('registerId' => $whatFind);
}
$results = $this->db->
group_by('registerId')->
//($start, $config['per_page'])->
order_by('id', 'desc')->
get_where('tbl_applications', $condition)->
result();
You need to use the limit
$this->db->limit(X);
where X is the limit rows to return
$results = $this->db
->group_by('registerId')
->limit($start)
->order_by('id', 'desc')
->get_where('tbl_applications', $condition)
->result();
If you want to limit with a fixed digit (10), then you should do this in your query
->limit(10)
Hello you need to define limit and add in you query.
May below link will help you
Codeigniter Pagination From Database limit, offset
I need your help please. I have this SQL query :
SELECT * , COUNT( * ) AS count FROM mytable GROUP BY email ORDER BY id DESC LIMIT 0 , 30
But I would like to do this in Symfony2 with Doctrine and the createQueryBuilder().
I try this, but didn't work :
$db = $this->createQueryBuilder('s');
$db->andWhere('COUNT( * ) AS count');
$db->groupBy('s.email');
$db->orderBy('s.id', 'DESC');
Can you help me please ? Thanks :)
You need to run 2 queries:
$db = $this->createQueryBuilder();
$db
->select('s')
->groupBy('s.email')
->orderBy('s.id', 'DESC')
->setMaxResults(30);
$qb->getQuery()->getResult();
and
$db = $this->createQueryBuilder();
$db
->select('count(s)')
->groupBy('s.email')
//->orderBy('s.id', 'DESC')
->setMaxResults(1);
$qb->getQuery()->getSingleScalarResult();
I tried to use
$query->select('COUNT(DISTINCT u)');
and it's seem to work fine so far.
$db = $this->createQueryBuilder('mytable');
$db
->addSelect('COUNT(*) as count')
->groupBy('mytable.email')
->orderBy('mytable.id', 'DESC')
->setFirstResult(0)
->setMaxResults(30);
$result = $db->getQuery()->getResult();
Hope it helps even if it's an old stack
In zend it is written like
$table = $this->getTable();
$select = $table->select()->where('status = ?',1)
->where('columnOne= ?', 1)
->order('columnTwo')
->limit(1);
similar to where, order, limit conditions how can I condition for LIKE?
My query is
SHOW TABLE STATUS LIKE 'tableName'
I tried in this way
$table = $this->getTable();
$query= $table->select("TABLE STATUS")
->like($table);
$id = mysql_query($query);
then I found that no method for LIKE is available in ZEND.
Then How can I write above query in Zend framerk?
Thanks in advance.
This works for me, so hopefully it should give you what you want:
$stmt = $dbAdapter->query("SHOW TABLE STATUS LIKE '$tableName';");
$tableStatus = $stmt->fetchObject();
I want to find out how many rows are in a table. The database that I am using is a MySQL database. I already have a Db_Table class that I am using for calls like fetchAll(). But I don't need any information from the table, just the row count. How can I get a count of all the rows in the table without calling fetchAll()?
$count = $db->fetchOne( 'SELECT COUNT(*) AS count FROM yourTable' );
Counting rows with fetchAll considered harmful.
Here's how to do it the Zend_Db_Select way:
$habits_table = new Habits(); /* #var $habits_table Zend_Db_Table_Abstract */
$select = $habits_table->select();
$select->from($habits_table->info(Habits::NAME), 'count(*) as COUNT');
$result = $habits_table->fetchRow($select);
print_r($result['COUNT']);die;
Proper Zend-Way is to use Zend_Db_Select like this:
$sql = $table->select()->columns(array('name', 'email', 'status'))->where('status = 1')->order('name');
$data = $table->fetchAll($sql);
$sql->reset('columns')->columns(new Zend_Db_Expr('COUNT(*)'));
$count = $table->getAdapter()->fetchOne($sql);
This is how it's done in Zend_Paginator. Other option is to add SQL_CALC_FOUND_ROWS before your column list and then get the number of found rows with this query:
$count = $this->getAdapter()->fetchOne('SELECT FOUND_ROWS()');
You could do a
SELECT COUNT(*)
FROM your_table
$dbo->setFetchMode( Zend_Db::FETCH_OBJ );
$sql = 'SELECT COUNT(*) AS count FROM #table';
$res = $dbo->fetchAll( $sql );
// $res[0]->count contains the number of rows
I'm kind of a minimalist:
public function count()
{
$rows = $db->select()->from($db, 'count(*) as amt')->query()->fetchAll();
return($rows[0]['amt']);
}
Can be used generically on all tables.
Add count capability to your Zend_DB Object To count all table rows
public function count()
{
return (int) $this->_table->getAdapter()->fetchOne(
$this->_table->select()->from($this->_table, 'COUNT(id)')
);
}