Something like this:
$connection->executeQuery('check table historial1 in database $connection->executeQuery('show tables')')
$sql = "SHOW TABLES 'Table_Name' ";
if($this->is_connected)
{
$query = $this->pdo->query($sql);
return $query->fetchAll(PDO::FETCH_COLUMN);
}
return FALSE;
Related
in store procedure we can check record is exist or not using following query for fast performance
if EXISTS ( Select 1 from Table_Name where id=#id )
But what about Linq query.
right now i have to store whole data in object like this
UserDetail _U=db.UserDetails.where(x=>x.id==1).FirstOrDefault();
Any Solution?
Use Linq's Any ie bool exist = db.UserDetails.Any(x => x.id == 1);
if(db.UserDetails.Any(x => x.id == 1)) {
var userDetail = db.UserDetails.FirstOrDefault(x => x.id == 1);
}
bool exist = db.UserDetails.Where(x=>x.id==1).Any();
if(exist){
//your-code
}else{
//your-code
}
Just check
if(_U == null)
This way you will get what you want in single query and you not need to execute addition query like
db.UserDetails.Any(x => x.id == 1)
I think, it does not require to fire two query. It can be accomplish by single query.
UserDetails objUserDetails =db.UserDetails.FirstOrDefault(x => x.id == 1);
if(objUserDetails==null)
{
// Do something
}
else
{
// do something with objUserDetails
}
var qry = db.User_Detail.Where(x => x.User_Id == 1).FirstOrDefault();
if(qry !=null)
{
// Do something
}
else
{
return false;
}
Try This...
The standard way to make a query against a database table is to create a model and create a function within it , for example :
<?php
use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Query;
class Client extends Model {
function lireParCritere($critere) {
$sSQL = "
SELECT c.clt_id as clt_id,c.clt_cin_pass,c.clt_nom,c.clt_prenom,c.clt_tel,
c.clt_adresse,c.clt_comment,CONCAT_WS(
' ',
c.clt_nom,
c.clt_prenom
) AS noms
FROM Client as c WHERE 1 = 1 ";
if(isset($critere["clt_id"]) && $critere["clt_id"] != "") {
$sSQL .= "AND c.clt_id = '" . $critere["clt_id"] . "' ";
}
$sSQL .= " ORDER BY noms";
$query = new Query($sSQL,$this->getDI());
$ret = $query->execute();
return $ret;
}
}
?>
What is the way to make a query against a database view ?
A database view is basically the output of a SQL query stored in another table. You can think of a view as an alias for a specific SQL query that you can then run other queries on top of. (kind of like SELECT * FROM (SELECT * FROM table_one, table_two, table_n);)
This means that you can treat the view as a regular table and pull from it without any issues.
So lets say you have a database view that has three columns id, col_one, col_two. You could do something similar to the following.
<?php
/**
* Model representing the database view
*/
class Example extends \Phalcon\Mvc\Model
{
public $id;
public $col_one;
public $col_two;
public function getSource()
{
return 'view_name_in_database';
}
public function columnMap()
{
return array(
'id' => 'id',
'col_one' => 'col_one',
'col_two' => 'col_two'
);
}
}
// example query on the model
$examples = Example::query()
->where('id = :id:')
->bind(array(
'id' => 55
))->execute();
?>
I am trying to create an array containing results from an SQL table. But my foreach loop does not appear to be function correctly.
Here is my code:
$stmt->bind_param("i", $id);
$stmt->execute();
while ($stmt->fetch()) {
}
$stmt->close();
$i = 0;
foreach ($connected_items as &$value) {
print_r ($connected_items[$i]);
$stmt->bind_param("i", $connected_items[$i]);
$stmt->execute();
while ($stmt->fetch()) {
$result[] = array(,
);
}
$stmt->close();
$i++;
}
unset($value);
)
)
It appears to be running the for loop on my print statement and then MySQL statement, using the last connected_items_id, why is that? And why is it not returning the associated values for that id?
remove this [] at line number 6:
while ($stmt->fetch()) {
$connected_items = array($connected_items_id);
}
instead of this:
while ($stmt->fetch()) {
$connected_items[] = array($connected_items_id);
}
Try this,
"connected_item_id" => $connected_items[0],
instead of
"connected_item_id" => $connected_items_id,
Try
foreach ($connected_items as $value) {
......................
......................
$stmt = $this->db->prepare('SELECT name, serial FROM `glpi_monitors` WHERE id =?');
$stmt->bind_param("i", $value[0]);
................................
................................
}
Also you can use IN keyword instead of looping and querying each time.
$sql = "SELECT name, serial FROM `glpi_monitors` WHERE id IN('".implode("','",$connected_items)."'");
$stmt = $this->db->prepare($sql);
eg :
I am starting with Zend Framework 2 , I want to make a routing choice with the role of My user and I must write getRoleByID($id) ,
then
How can'I write
" Select 'role' from user where ('id' = $id) " with Zend\Db\Sql
Example Using Select:
$select = new \Zend\Db\Sql\Select('user');
$select->columns(array('role'));
$where = new Where();
$where->equalTo('id', $id);
$select->where($where);
/**
* Simple example of executing a query...
*/
$stmt = $this->getSql()->prepareStatementForSqlObject($select);
$results = $stmt->execute();
/* #var $results \Zend\Db\Adapter\Driver\Pdo\Result */
if( ! $results->count()) {
// do something, none found...
}
$row = $results->current();
return $row['role'];
// if you had multiple results to iterate over:
//$resultSet = new \Zend\Db\ResultSet\ResultSet();
//$resultSet->initialize($results);
//$array = $resultSet->toArray();
//foreach($resultSet as $row) { /* ... */ }
This statement is to check if the user is existing in the database.
public boolean isExisting(int userId) {
String sql = "{call isExistingUser(?)}";
Session session = null;
boolean isExisting = false;
try {
session = getSession();
SQLQuery query = session.createSQLQuery(sql);
query.setParameter(0, userId);
List<?> list = query.list();
isExisting = list.get(0) != null ? (Boolean) list.get(0) : false;
} finally {
if (session != null)
session.close();
}
return isExisting;
}
This is the stored procedure:
CREATE DEFINER=cbsadmin#% PROCEDURE isExistingUser(IN userId int)
BEGIN
SELECT USER_ID FROM USER_LOGIN_STATUS WHERE USER_ID = userId;
END
It is not possible to query a Stored procedure using detached criteria in NHibernate.
You need to use SQL query only.
See here.