I have this simple code to select custom string from database:
protected function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__person');
$name = 'tom';
$query->where('name LIKE %'.$db->quote($name).'%');
return $query;
}
Unfortunately it gives me an error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean
given in xxx\public\libraries\joomla\database\database\mysql.php on
line 293
If I remove where call, so everything goes ok. Can I debug the datase query? I would like to see whats the final query goes to MySQL server.
Your help would be appreciated.
I've managed to work this for me:
protected function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__person');
$name = 'tom';
$name = $db->Quote('%'.$db->escape($name, true).'%');
$query->where($db->nameQuote('name').' LIKE '.$name);
//debug the query
// echo nl2br(str_replace('#__','prefix_',$query)); die;
return $query;
}
Related
I am gettin this error
Call to a member function getResultArray() on bool
in my pc run very good but in my hosting i have this error
What can I do? help me please
'public function obt_lista_prestamos($activo)
{
$db = \Config\Database::connect();
$db = db_connect();
$builder = $db->table('t_prestamo');
$builder->select('*');
$builder->join('t_clientes', 't_prestamo.fk_idcliente = t_clientes.id_cliente');
$query = $builder->get();
$query = $query->getResultArray();
return ($query);
}'
your code seems fine, but you can try with this:
'public function obt_lista_prestamos($activo)
{
$db = \Config\Database::connect();
$db = db_connect();
$builder = $db->table('t_prestamo');
$builder->select('*');
$builder->join('t_clientes', 't_prestamo.fk_idcliente =
t_clientes.id_cliente');
$query = $builder->get()->getResultArray();
return ($query);
}'
I had the same problem and this worked for me.
Note this line: $query = $builder->get()->getResultArray();
Mysql query executed in mysql but not in mysqli and mysql is deprecated so what syntax I have to use for mysqli in codeigniter:
$sql = "SELECT admin_email
FROM `tbl_admin`
WHERE `admin_email` = '" . $username . "' and `admin_password` = '" . $password . "'";
$query = $this->db->query($sql);
You can use Codeigniter Query Bulider
$this->db->select('admin_email');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();
I hope it works...
$this->db->select('*');
$this->db->from('tbl_admin');
$this->db->where('admin_email', $username);
$this->db->where('admin_password', $password);
$query = $this->db->get();
$result = $query->row();
return $result;
Try ...
$this->db->select('admin_email');
$this->db->where(
array(
'admin_email' => $username,
'admin_password' => $password
));
$query = $this->db->get('tbl_admin');
$result = $query->row();
Hopefully it works in your end...
$this->db->where('admin_email',$email);
$this->db->where('admin_password',$password);
$qry = $this->db->get('tbl_admin');
if($qry->num_rows() > 0){
return true;
}else{
return false;
}
It will work fine only if you set your Query in the Model then load that model in your controller. Directly setting the query from Controller seems to have this issue.
Example:
From the Model: Example 'Users.php'
class Users extends CI_Model {
public function __construct() {
parent::__construct();
}
function check_username($username) {
$query = $this->db->query("select * from `user_login` where `username`='$username'")
$query_result = $query->result_array;
if (!empty($query_result)) {
return $query_result;
}
return[];
}
}
From the Controller: Example 'Userauth.php'
class Userauth extends CI_Controller {
public function process_forgot(){
$CI =& get_instance();
$CI->load->model('Users'); //Model loaded here
$username = $this->input->post('username');
$check = $CI->Users->check_username($username);
echo '<pre>';
print_r($check);
echo '</pre>';
}
}
Try it.
How i can get some parameters from a disabled/not yet actived plugin in joomla 1.6/2.5?
$module = JPluginHelper::getPlugin('system','myplugin');
$moduleParams = new JParameter($module->params);
$val = $moduleParams->get("key");
This method didn't work becouse i need to use within an element JFormField generator.
Thanks for help!
With JPluginHelper::getPlugin it's possible to access only enabled plugins, so here's the code for direct access to database.
// Build query
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select( 'params' )
->from( '#__extensions' )
->where( 'type = ' . $db->q('plugin') )
->where( 'folder = ' . $db->q('authentication') ) // Plugin type
->where( 'element = ' . $db->q('gmail') ) // Plugin element
;
// Execute query
$db->setQuery($query);
try
{
$result = $db->loadResult();
}
catch (RuntimeException $e)
{
return false;
}
// Parse parameters
if (!empty($result))
{
$params = new JRegistry($result);
$val = $params->get('key', 'defaultValue');
}
You may store query results in in the JFormField Object so save database queries in case field is availabile multiple times.
protected $results = null;
Perhaps you may want to try this:
// Get plugin parameters
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('`params`')
->from ('`#__extensions`')
->where ("`type` = 'plugin'")
->where ("`folder` = 'system'")
->where ("`element` = 'myplugin'");
$db->setQuery($query);
$res = json_decode($db->loadResult(), true);
$val = $res['key'];
Just find the answer by myself.
$data = null;
foreach ((array) $this->form as $k => $v) {
if($val instanceof JRegistry){
$data = &$v;
break;
}
}
$data = $data->toArray();
$val = $data['params']['key'];
Thanks! Bye!
I want to add pagination to my component, i've created a simple model with query. I must be missing something. What else do I need here ?
MODEL
jimport('joomla.application.component.modellist');
class PaieskaModelPradinis extends JModelList
{
public function getListQuery()
{
$db = JFactory::getDBO();
$query = "SELECT * FROM #__content";
$db->setQuery( $query );
$db->query( $query );
$result = $db->LoadObjectList();
return $result;
}
}
VIEW
jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
protected $items;
protected $pagination;
function display ($tpl = null)
{
$this->items = $this->get('ListQuery');
$this->pagination = $this->get('Pagination');
parent::display($tpl);
}
}
TPL
foreach ($this->items as $item) {
echo $item->title;
}
EDITED:
I edited a bit code, so now it works fine, almost. Button display(number of rows to display) is not working. And I wonder if this part can be done in a different way ?
$limit = JRequest::getVar('limit' , 25);
$start = JRequest::getVar('start' , 0);
$query = "SELECT * FROM #__content LIMIT $start, $limit";
-
class PaieskaModelPradinis extends JModelList
{
public function getItems()
{
$db = JFactory::getDBO();
$limit = JRequest::getVar('limit' , 25);
$start = JRequest::getVar('start' , 0);
$query = "SELECT * FROM #__content LIMIT $start, $limit";
$db->setQuery( $query );
$db->query( $query );
$lists = $db->LoadObjectList();
return $lists;
}
function getPagination()
{
$main = JFactory::getApplication();
$db = JFactory::getDBO();
$limit = JRequest::getVar('limit' , 25);
$limitstart = JRequest::getVar('limitstart', 0);
$query = "SELECT count(title) FROM #__content";
$db->setQuery( $query );
$total = $db->loadResult();
// include a pagination library
jimport('joomla.html.pagination');
$pagination = new JPagination($total, $limitstart, $limit);
return $pagination;
}
}
VIEW
jimport( 'joomla.application.component.view');
class PaieskaViewPradinis extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
$this->pagination = $this->get('pagination');
parent::display($tpl);
}
}
Going off your original code, not the edited version.
The getListQuery method only builds your database query, so you don't execute your query here. Use com_weblinks as an example for building out your model: https://github.com/joomla/joomla-cms/blob/2.5.x/components/com_weblinks/models/category.php
follow this link http://docs.joomla.org/J1.5:Using_JPagination_in_your_component properly.
I have used Joomla pagination. You'll be able to use Joomla pagination easily, if you follow documentation properly. BTW its very simple.
how to show data from 3 tables in one view, because using JTable i can show data only bind to that JTable, please help me with this one.
my code so far(not working) in models:
public function getEntireProject(){
$item_id = $this->getItem()->id;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item_id);
$db->setQuery($query);
return $db->loadResult();
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getEntireProject();
}
return $data;
}
try to overwrite getItem function.This will also be helpful if you are calling get('Item') in view. -
public function getItem($pk = null){
if ($item = parent::getItem($pk)) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item->id);
$db->setQuery($query);
$item = $db->loadAssoc();
}
return $item;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
For Multi-Row Results use loadRowList(), loadAssocList(), loadAssocList($key), loadObjectList(), loadObjectList('key'). $db->loadResult() only load one result. Read more.
If I understand your question right this should fix your problem. If you not please ask.