Author |
Message |
tommyb
Hangin' Around

Joined: May 31, 2006
Posts: 49
|
Posted:
Wed Jul 05, 2006 4:54 am |
|
Hello all,
When a user creates a script and they have a query such as Code:$query="select * form table";
$result=mysql_query($query, $dbi);
| What is the purpose of the $dbi? I know it is the database connection info in config.php but surely if the config.php file is already included why would you have need for this?
Many Thanks |
|
|
|
 |
kguske
Site Admin

Joined: Jun 04, 2004
Posts: 6437
|
Posted:
Wed Jul 05, 2006 5:57 am |
|
Although that is the old style connection variable (the new one is $db and isn't mySQL-specific), it contains the connection information necessary to access the database. Without it, you would need to provide connection information (e.g. user, password) every time you wanted to access the database. |
_________________ I search, therefore I exist...
Only registered users can see links on this board! Get registered or login! |
|
|
 |
tommyb

|
Posted:
Wed Jul 05, 2006 6:57 am |
|
So if I had module that refers to $dbi should that still connect to ravennuke or should it be changed to $db?And does it only need to be referenced once for all queries to work or if mysql_close() is encountered? Sorry if this seems basic I'm just trying to work things out by looking at various modules etc and trying to understand them more. |
|
|
|
 |
fkelly
Former Moderator in Good Standing

Joined: Aug 30, 2005
Posts: 3312
Location: near Albany NY
|
Posted:
Wed Jul 05, 2006 8:09 am |
|
I'm not a real expert on object oriented programming but if you look at a couple of programs in the /db directory you can see how this works. First look at db.php and you will see that it includes a different php file for each database type that you could possibly access in nuke. Then in the included file (let's say mysql4.php) there are a series of objects created that essentially "abstract" the details of database access and queries. So, if you read thru the sql_query function you will see all the details that it takes care to let you call a query "simply" in your program.
Then if you look at a simple query say from mainfile.php you will see something like this:
$result = $db->sql_query('SELECT * FROM '.$prefix.'_config');
and I believe that what it's doing is using the sql_query object from the appropriate program in the /db directory and because that object already has built in the query methods you don't need to implement the details in each database call.
I'm struggling to say that in english but it's what I think is going on.
Probably should add that a lot of the database "base" info that the programs in the /db directory use are stored in config.php. |
|
|
|
 |
tommyb

|
Posted:
Wed Jul 05, 2006 8:12 am |
|
I think I get it $db refers to the database type therefore if it is mysql
$result = $db->sql_query() would relate to
$result= mysql_query()
or $result = equivalent_query(function) for db type.
And making it this way allows for users of various database types without them having to troubleshoot why it is not working. |
|
|
|
 |
gregexp
The Mouse Is Extension Of Arm

Joined: Feb 21, 2006
Posts: 1497
Location: In front of a screen....HELP! lol
|
Posted:
Wed Jul 05, 2006 9:53 am |
|
correct, It also puts the variable in an array declaring it, never to be redeclared(I believe). |
_________________ For those who stand shall NEVER fall and those who fall shall RISE once more!! |
|
 |
 |
kguske

|
Posted:
Wed Jul 05, 2006 10:30 am |
|
Actually, $db is an object that represents the database connection, regardless of which database you use (e.g. MySQL, Postgres, SQL Server, etc.). sql_query is a function that executes a database query on that database, again, regardless of which database.
The rest of your comparison is correct. But it also simplifies database access for all modules and addons by providing a consistent way to access data. |
|
|
|
 |
gregexp

|
Posted:
Wed Jul 05, 2006 10:35 am |
|
so sql_query is a new function redifined in nuke and $db is the database connection?
now thats somethin new.
I just thought that $db was earlier defined as MY or pos or whatever typ(mine being mysql)
and when placed like that
it actually read mysql_query.
Thanx, learning things twice. |
|
|
|
 |
|