1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Database connection

Discussion in 'Community' started by rene.brodmuehler@gmail.com, Jun 12, 2021.

  1. rene.brodmuehler@gmail.com

    rene.brodmuehler@gmail.com New Member

    Level: Community
    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é
     
  2. lousyfool

    lousyfool Active Member

    Level: Community
  3. rene.brodmuehler@gmail.com

    rene.brodmuehler@gmail.com New Member

    Level: Community
    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é
     
  4. lousyfool

    lousyfool Active Member

    Level: Community
    That's the whole point of "protected": to not get it that easily.

    Instead, try:
    Code (Text):

    $app = JFactory::getApplication();
    $pre = $app->get('dbprefix');
     
     
  5. rene.brodmuehler@gmail.com

    rene.brodmuehler@gmail.com New Member

    Level: Community
    Will do, thanks. I guess the best is I learn this "chaining" language. ;) .... many thanks Rene
     
  6. juuser

    juuser Well-Known Member

    Level: Community
    Some simple examples in "your style" mentioned above for e.g. calc element.

    1) Get data from several elements in one row:
    Code (Text):
    $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 (Text):
    $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: Jun 12, 2021
    hominid4 likes this.

Share This Page