Joomla - Independent SQL query - php

I'm wondering if someone can give me a bit of a hand.
I'm trying to write a query inside a PHP file in Joomla and its not really working that well, nothing is being output.
I'm very new to this Joomla stuff, so if someone could let me know where I'm going wrong that would be great.
My code is as follows:
$db =& JFactory::getDBO();
$query = "SELECT fullname FROM jos_jxzine_authors WHERE published = '1'";
$db->setQuery($query);
$column = $db->loadResultArray();
echo JHTML::_('select.options', $column, 'value', 'text', $this->categoryMap);
Cheers,

Please use this query
$query = "SELECT fullname FROM `#__jxzine_authors` WHERE published = '1'";
joomla will itself add db prefix. So you must use #_ instead of jos

Related

How to view value custom field community builder

i am using joomla 2.5 and install component community builder with work properly, and on the customize field i am add some field cb_nip, how to i get and show values cb_nip from php from user login, because i need values cb_nip for custom condition for me.
i tried get cb_nip values with script, but nothing shown anything :
$user = JFactory::getUser();
$cb_nip = JUserHelper::getProfile($user->cb_nip);
echo $comprofiler->comprofiler['cb_nip'];
please help me for this.
You need to query the database to do this.
$user = JFactory::getUser();
$db = $this->getDbo();
$query = 'SELECT cb_nip FROM #__comprofiler WHERE user_id='.$user->id;
$db->setQuery($query);
$cb_nip = $db->loadResult();
echo $cb_nip;

Pulling settings from MySQL using Object-Orientated PHP

Ok, so I am slowly migrating from Procedural to OOP, and I'm finding it all pretty straight forward apart from one thing.
I used to use this method for pulling my settings data from a simple two-column settings table comprising of a row for each setting, defined with 'setting' and 'value' fields:
$query = mysql_query("SELECT * FROM `settings`");
while ($current_setting = mysql_fetch_array($query)) {
$setting[$current_setting['setting']] = $current_setting['value'];
}
As you can see, I manipulated it so that I could simply use $setting['any_setting_name'] to display the corresponding 'value' within that setting's row. I'm not sure if this is a silly way of doing things but no matter, I'm moving on anyway..
However, since moving to object orientated PHP, I don't really know how to do something similar..
$query = $mysqli->query("SELECT * FROM `settings`");
while ($current_setting = $query->fetch_object()) {
echo $current_setting->setting; // echo's each setting name
echo $current_setting->value; // echo's each setting value
}
As you can see, I'm perfectly able to retrieve the data, but what I want is to be able to use it later on in the form of: $setting->setting_name; which will echo the VALUE from the row where setting is equal to 'setting_name'..
So basically if I have a row in my settings table where setting is 'site_url' and value is 'http://example.com/', I want $setting->site_url; to contain 'http://example.com/'.. Or something to the same effect..
Can anyone help me out here? I'm at a brick-wall right now.. Probably something really stupid I'm overlooking..
Since you are working with a resource it has a pointer. Once you get to the end you can't use it anymore. I think you can reset it but why not mix the new with the old?
I don't think you're going to have too much overhead, any that matters anyway, to do something like this:
$settings = array();
$query = $mysqli->query("SELECT * FROM `settings`");
while ($current_setting = $query->fetch_object()) {
$settings[$current_setting->setting] = $current_setting->value;
}
Now you can use $settings as much as you want.
UPDATE
Haven't tested this or used it but are you looking to do something like this? Consider the following pseudo code and may not actually work.
$settings = new stdClass();
$query = $mysqli->query("SELECT * FROM `settings`");
while ($current_setting = $query->fetch_object()) {
$settings->{$current_setting->setting} = $current_setting->value;
}

Close session after executing database query

I have very little experience with joomla and sql and I would really appreciate your help!
I am using joomla 2.5 and I am querying data from the database and storing it in memory with the following code:
function getList()
{
$mainframe = JFactory::getApplication('site');
$db = JFactory::getDBO();
$query = " SELECT
*
FROM
#__ListUser
WHERE
$db->setQuery( $query );"
$rows = $db->loadObjectList();
return $rows;
}
I have 3 questions,
When I query the database, a new DB session is opened, Do I need to close it after or is automatic?
Do you know of a more efficient way to achieve this method (a user session memory size is about 11MB!)
Is there any security issue with accessing the database using this method?
Thank you very much! any help would be very appreciated!
The code should look like this (I don't see how it can work now):
function getList()
{
// $mainframe = JFactory::getApplication('site'); // you don't need this line!
$db = JFactory::getDBO();
$query = " SELECT
*
FROM
#__ListUser
WHERE
1=1"; // just some condition to extract selected rows
$db->setQuery( $query ); // this sets the query and it's joomla, not sql.
$rows = $db->loadObjectList();
return $rows;
}
Please note the WHERE .... needs a condition (else if you want all the rows, remove WHERE and what follows)
You don't need to close it
11Mb is not necessarily due to that query, try adding LIMIT 0,1 (to return just one row) you'll see your memory doesn't change much. Turn on debug in the global configuration, and reload the component. At the very bottom of the page you'll see which extensions are eating up your memory. 11Mb is acceptable though on most installations.
Should you create your WHERE condition using input params, just make sure you $db->quote() any values to prevent SQL-injection.
Try
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quote('*')
->from($db->quoteName('#__Listuser') // Do you really have upper case there?
->where('your condition with proper quoting');
$db->setQuery($query);
$rows = $db->loadObjectList();
1.
UNCOOL:
If you want to close or disconnect the database-session, you may use:
$db->disconnect(); // See: http://api.joomla.org/cms-3/classes/JDatabaseDriver.html#method_disconnect
But i guess, that the database-connection for every other module, plugin or template that want to use JFactory::getDBO(); is also closed then and needs to be reopened.
BETTER:
You should use FREE RESULT instead after a query is transfered to a PHP-Variable: http://api.joomla.org/cms-3/classes/JDatabaseDriverMysql.html#method_freeResult
$db->freeResult();

retreaving data from a mysql database

I have a mysql database which was created for a website using wordpress, when posting some text using wordpress it stores it on that database. When I try pulling this text using a php file, it returns null, but if I delete this text and write it in manually(no wordpress) it pulls it fine. is there anything specific i need to do in order to pull this text since it was created using wordpress? the structure of the table is as follows, field: post_content type: longtext. Thanks for the help
Code:
$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result))
$output[] = $row;
print(json_encode($output));
Add some error handling:
$result = mysql_query($query) or die(mysql_error());
This will cause an error message to be printed if something goes wrong in mysql_query. Hopefully the error message will help.
Also, the documentation for mysql_query says:
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.
So I'd advise using the libraries that the PHP authors recommend.
Also:
$query = "SELECT post_content FROM meiplay_posts WHERE post_type = 'portfolio'";
Are your sure that columns with post_type = 'portfolio' exist? Try doing the select without the WHERE part and see if you get anything. Maybe it's slightly different, like Portfolio instead of portfolio?
Wordpress gives you a class that lets you run your queries on the database more easily.
Reference: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column
Try:
$result = $wpdb->get_col($wpdb->prepare("SELECT `post_content` FROM `meiplay_posts` WHERE post_type = 'portfolio'"));
if ($result)
{
echo $result->post_content;
echo json_encode($result);
};

Joomla, Mysql error

I have uploaded a page with the code below to my joomla root directory.
<?php
$value = trim($_POST['opts']);
if ($value){
$db = "my_db";
$link = mysql_connect('localhost',$me,$my_password);
if(!$link) die("Error 1 ".mysql_error());
mysql_select_db($db);
**$query = "SELECT introtext,fulltext FROM jos_content WHERE title='$value' ";**
$result = mysql_query($query);
**if(!$result) die("Error 2 ".mysql_error());**
$obj = mysql_fetch_array($result);
$obj_f = $obj[0];
$lenght = strlen($obj_f);
$header2 = strpos($obj_f, "Did you know");
$header3 = strstr($obj_f, "Summary");
$third_part = $header3;
$first_part = substr($obj_f, 0, ($header2 - 1));
$second_part = substr($obj_f, $header2,((strpos($obj_f, "Summary")) - $header2) );
}
?>
the problem is that when i change my select(http://sanatural.co.za/sanp/test.php) i get this error message:
Error 2 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fulltext FROM jos_content WHERE title='Arthritis'' at line 1
The code highlighted in bold is where i think the problem might be. Please help.
Fulltext is a mysql keyword and you must escape it. Replace:
$query = "SELECT introtext,fulltext FROM jos_content WHERE title='$value' ";
with
$query = "SELECT `introtext`,`fulltext` FROM jos_content WHERE title='$value' ";
This is a bit off topic, but an easy way to use PHP in Joomla is through the PHP Component.
http://www.fijiwebdesign.com/products/joomla-php-pages.html
This allows you to put add PHP in Joomla as if it were a Joomla Component.
If you want something quick, then you can also use the PHP Module.
http://www.fijiwebdesign.com/products/joomla-php-module.html
Just install either, add your PHP, and add it to the Joomla menu.
You can then use the Joomla API which will simplify what you want to do within Joomla.
For example, your database queries could be:
// Joomla already has a connection to the DB
// available here as a Singleton in the Factory pattern
$Db =& JFactory::getDBO();
// querying the db
$Db->setQuery('SELECT `introtext`,`fulltext` FROM #__content WHERE title='.$Db->Quote($value).' LIMIT 1';
// retrieving a single row as an object
$article = $Db->loadObject();
// handle errors
if($Db->getErrorNum()) {
JError::raiseError( 500, $Db->stderr());
}
//Then accessing each column/property would look something like:
$intro = $article->introtext;
$text = $article->fulltext;
The full Database API is documented here:
http://api.joomla.org/Joomla-Framework/Database/JDatabase.html

Categories