As show at CakePHP 2.1.x - Run a query without any models in AppController
I have a query,
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$qr = $db->rawQuery($q);
ok (!), it works... But, how to get my data?? Where the tutorial examples?
I need something like $data = $qr->fetchAll() method or $id = getMyData($qr) function.
I believe this may be the solution or at least a point in the right direction.
$q = "select id from table where id=123";
$db = ConnectionManager::getDataSource('default');
$myData = $db->query($q);
Related
I try to learn Laravel 5.5, but I have problem to use DB;
DB Table have records.
Following code return some records.
$result = DB::select(DB::raw("select * from todays where FROM_UNIXTIME(login, '%Y/%m/%d') = '2019/03/04'"));
But following code doesn't return any records. What is wrong this ?
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')", DB::raw('2019/03/04')))->get();
Please help me.
Thanks
Your query seems to be wrong.
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), DB::raw('2019/03/04'))->get();
or
$result = DB::table('todays')->where(DB::raw("FROM_UNIXTIME(login, '%Y/%m/%d')"), '2019/03/04')->get();
First one is more of an error-prone while the second one is used as pdo statement.
on my joomla page i have a menu point that gives me the latest article, i wrote an extension for that. it works well, but for obvious reasons the article hit counter is not updated.
how can i fix that?
$db = JFactory::getDBO();
$query = "SELECT * FROM `#__content` WHERE `catid` = {$catid} AND `state` = 1 AND publish_up <= DATE_SUB(NOW(),INTERVAL 2 hour) ORDER BY `id` DESC LIMIT 1";
$db->setQuery($query);
$rows = $db->loadObjectList();
for the output:
echo ($rows[0]->introtext);
echo ($rows[0]->fulltext);
I recommend to use the JTable class
$table = JTable::getInstance('Content');
$table->hit($rows[0]->id);
I'm trying to figure out how to have the execute method of an ZF2 SQL Statement class to return an array of objects rather than an array of arrays. The documentation/reference seems to lack severely in this area and the code isn't documented properly.
This is what I have (I'm using the PDO MySQL driver but that should not be relevant):
$sql = new Sql( $db );
$query = $sql->select()
->from('users')
->where('id > 1');
$stmt = $sql->prepareStatementForSqlObject( $query );
$results = $stmt->execute();
$results is now an array of arrays but I need it to be an array of objects instead. How can I specify this?
I think that most people use doctrine for ORM
http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/
I have a table in which comma seprated id of another table i want to use the following query in cakephp in proper form with find function
"select * from special_offers where find_in_set('".$storeId."', stores) and user_id = '". $userId ."'";
Use like this
$data = $this->SpecialOffer->find('all',array('conditions' => array('SpecialOffer.user_id' => $userId ,'FIND_IN_SET(\''. $storeId .'\',SpecialOffer.stores1)')));
Hope this may help you
I have been able to create simple MVC app to display the form and use information from the database. [ so sql I use SELECT ].
However, when I use 'update' or 'delete' the data in the database stay the same. I don't understand!?!
[ I use controller.php call model and pass id to match with the row in the table]
I check data that pass in there, and it did echo the value of id.
The page I load from the controller that call method in model has complete without the error (it could be that the syntax is right, but logic is wrong).
so i start to change sql statement to SELECT and it gives me the value that match with $id param'
I don't understand? any clue?
$query = JFactory::getDbo()->getQuery(true);
$db =& JFactory::getDBO();
$query = "DELETE FROM `menutables` WHERE `id2` = $id ";
$db->setQuery($query);
$db->query(); // this line I also take it out to test, it is fine but no data change either
Do you try to modify a Joomla database or a personal one?
Look at this pages :
http://docs.joomla.org/How_to_connect_to_an_external_database
http://docs.joomla.org/How_to_use_the_database_classes_in_your_script#Tips.2C_Tricks_.26_FAQ
try to use either
$query = "DELETE FROM #__menutables WHERE id2 = '$id' ";
or
$query = "DELETE FROM #__menutables WHERE id2 = '".$id."' ";
it'll work fine then.
Deleting a Record
delete query in Joomla 2.5.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// delete all custom keys for user 1001.
$conditions = array(
$db->quoteName('user_id') . '=1001',
$db->quoteName('profile_key') . '=\'custom.%\''
);
$query->delete($db->quoteName('#__user_profiles'));
$query->where($conditions);
$db->setQuery($query);