use smf database conction variable with smf on another page

Started by glennmckenna, August 07, 2016, 12:40:31 PM

Previous topic - Next topic

glennmckenna

hi,
i'm trying to access a table in the smf database from a page that is useing SSI.php but i don't seem to be able to do it

is it this variable:
$smcFunc['db_query']("SELECT * FROM DB-table");

or is it another variable or is it not doable through SSi.php

the reason for me wanting to do it this way is so that i don't have to connect to the database twice
once to load the SSi stuff from the forum
once to load the information from to load other parts of the page

here a copy of the code that i'm trying to run
template_header();


$affiche = $smcFunc['db_query']("SELECT * FROM home-affiche-banner");

$aads = $affiche->fetch_all(MYSQLI_ASSOC);




template_footer()

Sorck

You're along the right lines. However, the database query function uses three parameters - the first one is almost always an empty string, the second is the body of your query, and the third is an associative array of key-value entries to substitute into your query.

Example:

$res 
$smcFunc['db_query']('''SELECT * FROM {db_prefix}topics WHERE id_topic = {int:my_id}',
                                                        [
'my_id' => 5]
                                                   );
while(
$row $smcFunc['db_fetch_aasoc']($res))
{
    
// Do what you want with your row here
}


For more info you may want to look at http://wiki.simplemachines.org/smf/$smcFunc#db_query

You won't be able to use a fetch all method like in your example as SMF uses it's own row retrieval system (as in thr above Example}.

NB: in the example, {db_query} transforms into my_db_name.my_prefix_.

glennmckenna

here is what i'm doing :
$affiche = $smcFunc['db_query']('', 'SELECT * FROM home-affiche-banner', '');
while($homeaff = $smcFunc['db_fetch_assoc']($affiche))
{
    // Do what you want with your row here
    print_r($homeaff);
}

and smf is reteurning a db error
so there for a assume that i need to put the name of ever colum in the third parameter ?
seeing that i need all the info out of this table

Sorck

It's always best to post what the error is - it's usually much easier then us listing all the possibilities.

However, I can see that you have a couple of problems.

First is that the dash/hyphen character shouldn't be in a table name. So home_affiche_banner would be better. If not then you need to quote the table name in backticks `home-affiche-banner` if you're using MySQL

Second, you probably need your database name prefixed in. If your table is in a database called mydb then you would have to add "mydb." before your table name in the query.

glennmckenna

Quote from: Sorck on August 14, 2016, 07:56:32 AM
It's always best to post what the error is - it's usually much easier then us listing all the possibilities.

However, I can see that you have a couple of problems.

First is that the dash/hyphen character shouldn't be in a table name. So home_affiche_banner would be better. If not then you need to quote the table name in backticks `home-affiche-banner` if you're using MySQL

Second, you probably need your database name prefixed in. If your table is in a database called mydb then you would have to add "mydb." before your table name in the query.
please attatched an image of the error

i shall change the name of the table

the table is located in the same database as the forum the name of the table will be home_affiche_banner (after being renamed)

Sorck

So it'd look like:


$affiche = $smcFunc['db_query']('', 'SELECT * FROM my_database.home_affiche_banner', array());
while($homeaff = $smcFunc['db_fetch_assoc']($affiche))
{
    // Do what you want with your row here
    print_r($homeaff);
}



glennmckenna

Quote from: Sorck on August 15, 2016, 08:03:01 AM
So it'd look like:


$affiche = $smcFunc['db_query']('', 'SELECT * FROM my_database.home_affiche_banner', array());
while($homeaff = $smcFunc['db_fetch_assoc']($affiche))
{
    // Do what you want with your row here
    print_r($homeaff);
}

why my_database.home_affiche_banner ?
can't i just put home_affiche_banner seeing that the table is in the same DB as smf ?

glennmckenna

ok so i do need the DB name but is there a way the smf variable $db_name ?

Illori

are you loading SSI on the page? if so you can use $db_name, and you should if necessary.

glennmckenna

Quote from: Illori on August 15, 2016, 08:50:13 AM
are you loading SSI on the page? if so you can use $db_name, and you should if necessary.
yes i am loading SSI
how would i put it though ?
cause i tried '  and " and ` but none of them worked

Advertisement: