how to increase joomla article hit counter when accessing via sql? - database

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);

Related

Laravel8 : get column name from table SQL Server

I've tried this code:
//controller
$model = new Mymodel;
$table = $model->getTable();
$columns = Schema::getColumnListing($table);
//view.blade.php
{{print_r($columns)}}
but the result only give this output: Array ( [0] => * ) 1
More Information:
I need to connect to SQL Server database with Forticlient VPN. I think it shouldn't be the problem. But, tell if there's something I've missed.
After struggling looking for any answer, and looking for answer generally about how to get the column name straight from SSMS, I've made my temporary own solution. Here's the code:
$model = new Mymodel;
$table = $model->getTable();
$schema = DB::table('INFORMATION_SCHEMA.COLUMNS')
->select('COLUMN_NAME')
->where("TABLE_NAME", $table)
->get();
My own answer is only improvement from the base query to get column name straight from the SSMS, here the query:
select COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = 'mytable_name'

Laravel 5.5 can't get record from DB

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.

Display database result in a joomla module

I am trying to have the module check a table in the database and return the higest amount in the column. (using sourcerer plugin to use php in a wysiwyg module). However I just get..nothing. No error, but no returned info either.
<?php
$db = new mysqli('localhost', 'user', 'pass', 'db');
$query = 'SELECT MAX(Amount) AS Amount FROM nud9b_auction';
$result = $db->query($query);
$row = $result->fetch_row();
echo $row['Amount'];
$result->close();
?>
$db = JFactory::getDbo();
$db->setQuery('SELECT MAX(Amount) AS Amount FROM #__auction');
echo $db->loadResult();
You need not write joomla table prefix in qquery.
Full guide here:
https://docs.joomla.org/Selecting_data_using_JDatabase

Cake 2.X retrive data from rawQuery

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);

How do you use delete, update, insert in joomla mvc from the file you create

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);

Resources