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();
Related
Good Day Folks,
I have a children_tb and I JOIN residents_tbl to it. Then, on residents_tbl I created a view named family_head.
Is it possible to use family_head on another JOIN statement to children_tbl query statement?
Current query
$query = $this->db->select('*');
$query = $this->db->from('children_tbl');
$query = $this->db->join('residents_tbl', 'residents_tbl.residentID=children_tbl.childName', 'full');
---> ***I want to add another join here for view (family_head)***
$query = $this->db->where('childName', $id);
$query = $this->db->order_by('childID', 'DESC');
$query = $this->db->get('');
I try to use
$query = $this->db->join('family_head', 'family_head.residentID=children_tbl.familyHeadID', 'full');
but I got an error message:
Table 'db_name.family_head' doesn't exist
TIA,
C.M.
This issue was already solved.
table names are just mismatched.
I have a problem with my query and I need to join two tables from different databases now my problem is how can I execute my query. I got my syntax format from here
Please visit first this link so you could understand why my SQL syntax is like this http://www.x-developer.com/php-scripts/sql-connecting-multiple-databases-in-a-single-query
Im using CodeIgniter and here is an Idea of what my query looks like: Notice the way I'm selecting my columns: DATABASE_NAME.TABLE_NAME.COLUMN_NAME
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
$SELECT = "SELECT $ACCOUNTS.BALANCES_TABLE.IDNO, $ACCOUNTS.BALANCES_TABLE.balance";
$FROM = "FROM $ACCOUNTS.BALANCES_TABLE";
$WHERE = "$ACCOUNTS.BALANCES_TABLE.IDNO IN (SELECT $ENROLLEES.ENROLLEES_TABLE.IDNO FROM $ENROLLEES.ENROLLEES_TABLE)";
$SQL = $SELECT ." ". $FROM ." ". $WHERE;
MAIN PROBLEM: How to Execute my query?
If we do like this in codeIgniter:
$ENROLLEES->query($SQL); or $ACCOUNTS->query($SQL);
How can I execute my query that Im having multiple databases? What will I provide here[database]->query($SQL); ?
$sql="Select * from my_table where 1";
$query = $this->db->query($SQL);
return $query->result_array();
If the databases share server, have a login that has priveleges to both of the databases, and simply have a query run similiar to:
$query = $this->db->query("
SELECT t1.*, t2.id
FROM `database1`.`table1` AS t1, `database2`.`table2` AS t2
");
Otherwise I think you might have to run the 2 queries separately and fix the logic afterwards.
I can see what #Þaw mentioned :
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
CodeIgniter supports multiple databases. You need to keep both database reference in separate variable as you did above. So far you are right/correct.
Next you need to use them as below:
$ENROLLEES->query();
$ENROLLEES->result();
and
$ACCOUNTS->query();
$ACCOUNTS->result();
Instead of using
$this->db->query();
$this->db->result();
See this for reference:
http://ellislab.com/codeigniter/user-guide/database/connecting.html
http://www.bsourcecode.com/codeigniter/codeigniter-select-query/
$query = $this->db->query("select * from tbl_user");
OR
$query = $this->db->select("*");
$this->db->from('table_name');
$query=$this->db->get();
return $this->db->select('(CASE
enter code hereWHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
WHEN orderdetails.DealID = 0 THEN products.name
END) as product_name')
$this->db->select('id, name, price, author, category, language, ISBN, publish_date');
$this->db->from('tbl_books');
I have tried to make codeigniter query from mysql query
my mysql query is:
select *
from class_routine
where
semester='$semester'
and day='$day'
and time_schedule='$time_schedule'
and (batch='$batch' or section='$section' or teacher='$teacher' or room='$room');
for above query what will be the codeigniter query ?
I will use this query in model.
First, you should consider looking at the Documentation before asking any question, then specify what you've looked at and what you did try, posting your code.
Anyways, try this and let me know:
$this->db->where(array('semester' => $semester, 'day' => $day, 'time_schedule' => $time_scehdule));
$this->db->where("batch = $batch OR section = $section OR teacher = $teacher OR room = $room", NULL, FALSE);
$result = $this->db->get('class_routine')->result();
When in doubt is perfectly safe to use a normal query:
$result = $this->db->query("SELECT * FROM ....")->result();
How can I do a Query like this using the Zend framework
SELECT * FROM `productos` AS `p`
LEFT JOIN (SELECT SUM(cantidad) AS transferencias FROM transferencias WHERE upc = p`.`upc` and sucursal = 10)
AS `trans` ON trans.upc = p.upc AND trans.sucursal_clave_destino = 10
Thank you in advance.
You need to try this one.
I can't try it but the way of resolve it you can use from my query
$this->getAdapter()
->select()
->from(array('p' => 'productos'))
->joinLeft(array('trans' => $this->getAdapter()
->select()
->from('transferencias', 'SUM(cantidad)')
->where('upc IN (?)', $this->getAdapter()
->select()
->from('productos', 'upc')
)->where('sucursal = ?', 10)
), 'trans.upc = p.upc')
->where('trans.sucursal_clave_destino = ?', 10)
->query()
->fetchAll();
First of all, I'm afraid it's impossible for your query to run because the syntax is wrong. The correct way to write it is:
SELECT *, SUM(trans.cantidad) as cantidad
FROM productos AS p
LEFT JOIN transferencias AS trans
ON p.upc = trans.upc
WHERE trans.sucursal_clave_destino = 10 AND trans.sucursal = 10
First approach
I assume you have created your model in this manner: http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html
For example, in your Default_Model_ProductosMapper:
function getXXXX(){
$select = "[THE ABOVE QUERY]";
$result = $this->getDbTable()->getAdapter()->fetchAll($select);
return $result;
}
This is the most basic approach.
Second approach
By using Zend_Db functions, which is like prepare statement in database concept. Only use it to increase safety if you are passing parameters from user input (see SQL injection), otherwise it's safe to use the first approach.
Still, in your mapper:
function getXXXX(){
$query = $this->getDbTable()->getAdapter()->select();
$query->from('productos', array());
$query->joinLeft('transferencias', 'productos.upc = transferencias.upc', array('SUM(trans.cantidad) as cantidad));
$query->where("trans.sucursal_clave_destino = 10");
$query->where("trans.sucursal = 10");
// get result
$stmt = $this->getDbTable()->getAdapter()->query($query);
$result = $stmt->fetchAll();
return $result;
}
Third approach
If you are using Database ORM like Doctrine2, you can also write SQL or DQL (Doctrine Query Language) queries, which syntax is highly similar with SQL queries but absolutely NOT the same mechanism. The document is here. This document covers both approaches above for DQL and also will tell you where to put them.
I have a list of id's in PHP. For example:
$id = (4,6,3,2,5,1)
When I perform a query like this:
$query = mysql_query("SELECT * FROM table WHERE ID IN ($id)");
it retrieves the results I want but the items are returned in the insertion order.
Instead, I want to retrieve the results in the order given in my array:
information 4, information 6, information 3, information 2, information 5, information 1
How can I do that?
Thanks guys! solution is ORDER BY FIELD (ID, $id);
Try this
$query = "SELECT * FROM table WHERE ID IN ($id) ORDER BY FIELD(ID,$id) ";
you can see this http://marco-pivetta.com/mysql-custom-sorting-rule-mysql/
$query = "SELECT * FROM table WHERE ID IN ($id) ORDER BY ";
foreach ($id as $i) {
$query .= "ID = $i,";
}
$query = mysql_query(rtrim($query, ','));
Your code is vulnerable to injection. You should use properly parameterized queries with PDO or mysqli.
You can use FIELD on this,
SELECT *
FROM table
WHERE ID IN (4,6,3,2,5,1)
ORDER BY FIELD(ID, 4,6,3,2,5,1)
Custom Sorting using FIELD() example
$query="SELECT * FROM table WHERE ID IN ($id) ORDER BY FIND_IN_SET(ID,$id) DESC";
Also check this
Select query using IN() and without any sorting