Database connection

Dear All

in the examples how to connect to a database I always find this for the beginning:

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->insert('tablename')->set('field = ' . $db->quote('bar'))
->set('field2 = ' . $db->quote('{tablename___elementname}'));
$db->setQuery($query);
$db->execute();

I am not familiar with this kind of semantic. I guess I am more the procedural kind of guy and prefer this style:
$db = mysqli_connect($host,$user,$password,$database)
or die ("Abfrage konnte nicht durchgeführt werden 1 <br>\n");
$query = "SELECT SUM(e_netto) AS s_netto, SUM(e_netto)+1000 AS s_brutto FROM jahresrechnung_18_repeat WHERE parent_id = " . $myid;
$result = mysqli_query($db,$query);

Two questions please:

(1)
How can I translate
$db = JFactory::getDbo();
$query = $db->getQuery(true);
into my style

(2)
How can I extract the username, host, password and database from the $db variable.

I really appreciate your help.
Best regards
René
 
Hi

again many thanks. I used print_r and can see what is in the variable. I don't know the syntax to read it out. E.g. the beginning of the read-out for $db is as follows:

JDatabaseDriverMysqli Object
(
[name] => mysqli
[serverType] => mysql
...
[options:protected] => Array
(
[prefix] => lvb_
....

I understand that print_r ($db->name) gives me mysqli ....

But - how can I read the value of [prefix]?

Thanks
René
 
That's the whole point of "protected": to not get it that easily.

Instead, try:
Code:
$app = JFactory::getApplication();
$pre = $app->get('dbprefix');
 
Some simple examples in "your style" mentioned above for e.g. calc element.

1) Get data from several elements in one row:
Code:
$mydb = JFactory::getDBO();
$some_element = '{mytable___myelement_raw}';

$mydb->setQuery("SELECT item_code, description FROM mytable WHERE id = ".$mydb->Quote($some_element));
$myrow = $mydb->loadRow();

$item_code = $myrow['0'];
$item_desc = $myrow['1'];

return $item_code." - ".$item_desc;

2) Get data from multiple rows / elements:
Code:
$mydb = JFactory::getDBO();

$mydb->setQuery("SELECT item_code, description FROM mytable");
$rows = $mydb->loadObjectList();

$myhtml = "";
foreach ($rows as $row) {
   $myhtml .= $row->item_code." - ".$row->description."<br>";
}

return $myhtml;

Using these examples doesn't mean there's no need to learn the principles from the links lousyfool referred to :)
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top