How to bind pdo key and value from Question mark? - database

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?

PDOpreapare statement is not support this query right row

Related

Query execution in codeigniter

I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.
If your query return single data from database then you need to use
$row = $query->row_array()
Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];

update query sql table in yii

How can I update row in table in yii? I am using the following code but it is not working
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = $user->accountID";
$command = $connection->createCommand($sql);
$command->execute();
My guess is that $user is being converted to a string and thus the ->accountID is not working. You have two methods, one unsafe and one safe.
Unsafe - Add {} around the $user->accountID.
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = {$user->accountID}";
Safer - Use a parametrized query:
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = :userid";
$command = $connection->createCommand($sql);
$command->execute(array(':userid' => $user->accountID))
ok I forgot to put commas around
'user->accountID'

Joomla DB Object with where and Now()

can someone help me with this? I want to query some data for my joomla 2.5
template from the db.
The known mysql syntax
$result = mysql_query("SELECT * FROM `jos_fieldsattach_values` WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY");
works as expexted but if I try to write this in the joomla db syntax the query fails
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$db->setQuery($query);
$query
->select(array('*'))
->from('#__fieldsattach_values');
->where('WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY');
$result = $db->loadObjectList();
print_r($result);
It seems somethings wrong with the where clause ?
Thankful for you answers,
tony
First of all. When you develop anything with Joomla you should turn on FULL Error reporting (Configuration/Server/Error Reporting/Development) then set system debugging (Configuration/System/Debug System/Yes). It will show all errors, those from system and those from php/mysql. But remember make sure website is not visible for other users to. Now, your code should look more like this:
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("*")->from('#__fieldsattach_values')->where('value > NOW() AND value < DATE_ADD(NOW(),INTERVAL 5 DAY)');
$db->setQuery($query);
$result = $db->loadObjectList();
print_r($result);

How to add IN statement with JOIN ON clause in tablegateway ZF2

Getting all result so far until I added a 'IN' statement with join ON clause
My code is something like this:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$select->join('user_category_subscriptions', 'user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)', array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
Error
ZEND\DB\ADAPTER\EXCEPTION\INVALIDQUERYEXCEPTION
File:E:\htdocs\myproj\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:Statement could not be executed
Used 'echo $select->getSqlString();' to print the query:
SELECT "categories".* FROM "categories"
LEFT JOIN "user_category_subscriptions"
ON "user_category_subscriptions"."category_id" = "categories"."category_id" AND "user_category_subscriptions"."status" IN ("1""," "2""," "3""," "4")
WHERE category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))
So the problem is zend's auto converting (1,2,3,4) into ("1""," "2""," "3""," "4")
Any idea to solve this? Thanks
I think you can pass an expression instead of the join string:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$join = new Expression('user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)');
$select->join('user_category_subscriptions', $join, array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
try this
$select->where
->AND->NEST->equalTo('category.user_id', 2)
->addPredicate(new Sql\Predicate\In('user_category_subscriptions.status', array(2,4)));

ZF Db Append to column

Im trying to make an update similar to this with Zend_Db:
UPDATE `TABLE` SET
column = column + 'new value'
WHERE
foo = 'bar'
any of you have done this before? is it possible?
Thanks
With the help of Zend_Db_Expr this is possible.
Example:
$newValue = 101;
$data = array('column' =>
new Zend_Db_Expr($db->quoteInto('column + ?', $newValue)));
$where = $db->quoteInto('foo = ?', 'bar');
$updated = $db->update('TABLE', $data, $where);
Resulting Query:
UPDATE `TABLE` SET `column` = `column` + 101 WHERE `foo` = 'bar';
If you are asking how to append a string, then the code is similar, but you cannot use the + operator when dealing with character data, instead use CONCAT().
For example, change the $data array to this:
$data = array('varcharCol' =>
new Zend_Db_Expr(
$db->quoteInto('CONCAT(varcharCol, ?)', ' append more text!')
));

Resources