1

I am wanting to connect to a SQL Server Instance in my article, and I have the below syntax to do such. My issue is that when I view the article it writes

".$row['book_name']."
"; } ?>

onto the screen instead of interpreting it as part of the code. This is the code that I have, what do I need to alter so that the Joomla Article interprets this properly?

<?php
//provide your hostname, username and dbname
$host=""; 
$username="";  
$password="";
$db_name=""; 
//$con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
$con=mysql_connect("$host", "$username", "$password");
mysql_select_db("$db_name");
$book_name = $_POST['book_name'];
$sql = "select book_name from book_mast where book_name LIKE '$book_name%'";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result))
{
echo "<p>".$row['book_name']."</p>";
}
?>

EDIT
Updated to use JFactory

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('Top 1 employer')));
$query->from($db->quoteName('#[Roster].[dbo].[Employers]')); 
$db->setQuery($query); 
$row = $db->loadRow();
print_r($row);

But how do I pass in the server and credentials in this set-up?

EDIT 2
I have updated my code to use the JDatabaseDriver since my Joomla Install and the server I want to query are on different instances. I added in this syntax:

    <?php
    $option = array(); //prevent problems

    $option['driver']   = 'mysql';            // Database driver name
    $option['host']     = 'db.myhost.com';    // Database host name
    $option['user']     = 'fredbloggs';       // User for database authentication
    $option['password'] = 's9(39s£h[%dkFd';   // Password for database authentication
    $option['database'] = 'bigdatabase';      // Database name
    $option['prefix']   = 'abc_';             // Database prefix (may be empty)

    $db = JDatabaseDriver::getInstance( $option );
    $query = $db->getQuery(true);
    $query->select($db->quoteName(array('Top 1 employer')));
    $query->from($db->quoteName('#[Roster].[dbo].[Employers]')); 
    $db->setQuery($query); 
    $row = $db->loadRow();
    print_r($row);
?>

And now I get displayed on screen on page load everything after

getQuery(true);

What am I missing here?

  • 1
    Firstly, don't use mysql_* as it's deprecated. Secondly, use the JDatabase class to connect to your database. Joomla has wrappers for a reason, so please use them. Have a read of the doucmentation: docs.joomla.org/Selecting_data_using_JDatabase – Lodder Feb 22 '17 at 13:48
  • How do I pass in host, username, password, dbname using JDatabase? – Yohan Greenburg Feb 22 '17 at 13:53
  • Is the table you want to get the data from the same database your Joomla site uses? – Lodder Feb 22 '17 at 13:54
  • No. My Joomla site exists on a MySQL server, I am wanting to query a MSSQL server instance. – Yohan Greenburg Feb 22 '17 at 13:56
2

I would recommend to use this plugin here Regular Labs Sourcerer.

Took this from the description of the plugin page:

Sourcerer enables you to place PHP and any kind of HTML style code (including CSS and JavaScript) right into your content! Not only in your articles, but also in sections, categories, modules, components, META tags, etc.

You can now just place your original codes right into your WYSIWYG editor. The only thing you have to do is surround the code with the Sourcerer tags. Easy peasy!

So now you can also use PHP scripts in your content. That opens up a great deal of possibilities.

Most Joomla! Text Editors will strip parts of your HTML code, like JavaScripts (think of statistics scripts) and movie embed tags. With Sourcerer you won't have these limitations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.