Question

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.

Was it helpful?

Solution

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' ";

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top