Обсуждение: sql command

Поиск
Список
Период
Сортировка

sql command

От
PJourdan
Дата:
Could someone explain to me why sql commands work in php with pgsql? I have
a curious situation where the author used sql commands like sqlconnect to
access the database rather than pg-connect as well as other commands that
are either psql or sql - I'm a litle confused here. This is in a .conf file:
sqlconnect(array('server'=>'tester.videotron.ca','user'=>'mum',
     'database'=>'mum','password'=>'123456' ) );
I'm looking for the theory behind all this. :-)
Thanks
Phil Jourdan


Re: sql command

От
Scott Marlowe
Дата:
Your machine almost certain is using some kind of universal connection
class / include file somewhere you don't know about.

try adding this to a simple script:

print implode("<BR>",get_included_files());

and see what it lists and look through the include files it shows (use
'locate include_file' where include_file is the name of the include file
shown in the php script...)

On Thu, 2 May 2002, P. Jourdan wrote:

> Could someone explain to me why sql commands work in php with pgsql? I have
> a curious situation where the author used sql commands like sqlconnect to
> access the database rather than pg-connect as well as other commands that
> are either psql or sql - I'm a litle confused here. This is in a .conf file:
> sqlconnect(array('server'=>'tester.videotron.ca','user'=>'mum',
>      'database'=>'mum','password'=>'123456' ) );
> I'm looking for the theory behind all this. :-)
> Thanks
> Phil Jourdan
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>


Re: sql command

От
Chris
Дата:
Hi Phil,

>Could someone explain to me why sql commands work in php with pgsql?

?? Why wouldn't they? :P

>I have a curious situation where the author used sql commands like
>sqlconnect to access the database rather than pg-connect as well as other
>commands that are either psql or sql - I'm a litle confused here. This is
>in a .conf file:
>sqlconnect(array('server'=>'tester.videotron.ca','user'=>'mum',
>     'database'=>'mum','password'=>'123456' ) );
>I'm looking for the theory behind all this. :-)
>Thanks

It looks like the author wrote a function called 'sqlconnect' which takes
an array of data to (obviously) connect to the server.

The function would probably look something like this:

function sqlconnect($connectionarray) {
pg_connect("host=$connectionarray['server'] user=$connectionarray['user']
password=$connectionarray['password'] dbname=$connectionarray['database']");
}

(with more error checking and comments of course).

Do a grep on your code tree for 'function sqlconnect' and look at the file
involved.

It's just a small wrapper function for the full command :)


-----------------
      Chris Smith
http://www.squiz.net/


Re: sql command

От
"P. Jourdan"
Дата:
At 09:08 AM 5/3/2002 +1000, Chris Smith wrote:


>>Could someone explain to me why sql commands work in php with pgsql?
>
>?? Why wouldn't they? :P

I'm too new at this to understand. That's why I ask. What I don't
understand is this: is sql generic as opposed to specific comands in mySQL
and/or postgreSQL?
I now understand that in my problem below you are right, there was an
sqlconnect function created as you suggested below.
Then, what would be the reason to use this sqlconnect function rather than
pg_connect? Can't parameters be passed on to the pg_connect in the same way
as they are passed on to the created sqlconnect? It seems like extra work
or a bit of redundancy in creating a function that exists?

>It looks like the author wrote a function called 'sqlconnect' which takes
>an array of data to (obviously) connect to the server.
>
>The function would probably look something like this:
>
>function sqlconnect($connectionarray) {
>pg_connect("host=$connectionarray['server'] user=$connectionarray['user']
>password=$connectionarray['password'] dbname=$connectionarray['database']");
>}
>
>(with more error checking and comments of course).

This is what I found (the MySQL references are there because the db was
migrated from MySQL to PostgreSQL):

function SQLConnect($params='') {
     global $dbh;
     /*
      * MySQL equivalent:
      *  $dbh=mysql_pconnect( $db_server,$db_user,$db_password );
      */
     if(isset($params['server'])) {
         $connect="host=".$params['server'];
     }
     else {
         $connect='host=localhost';
     }
     if(isset($params['port'])) {
         $connect.=' port='.$params['port'];
     }
     else {
         $connect.=' port=5432';
     }
     if(isset($params['user'])) {
         $connect.=' user='.$params['user'];
     }
     if(isset($params['database'])) {
         $connect.=' dbname='.$params['database'];
     }
     if(isset($params['password'])) {
         $connect.=' password='.$params['password'];
     }
     $dbh=pg_pconnect($connect);
}

Thanks,
Philip


Re: sql command

От
Chris
Дата:
Hi Philip,

>>>Could someone explain to me why sql commands work in php with pgsql?
>>
>>?? Why wouldn't they? :P
>
>I'm too new at this to understand. That's why I ask. What I don't
>understand is this: is sql generic as opposed to specific comands in mySQL
>and/or postgreSQL?

SQL is generic and specific. Sorry to be confusing :)

Each database system has a slightly different syntax for things but the
basics remain (to give really simple examples) :

INSERT INTO table_name (field1, field2, field3) VALUES (value1, value2,
value3) ..
UPDATE table_name SET field1=value1, field2=value2 WHERE field3=value3 ..
DELETE FROM table_name WHERE field1=value1 ..
SELECT field1, field2 FROM table_name WHERE field3=value3 ..

Differences come in with things like auto-increment fields, dates and times.
Example: MySQL uses an 'auto_increment' datatype, PostgreSQL uses a
'serial' datatype.

>I now understand that in my problem below you are right, there was an
>sqlconnect function created as you suggested below.
>Then, what would be the reason to use this sqlconnect function rather than
>pg_connect? Can't parameters be passed on to the pg_connect in the same
>way as they are passed on to the created sqlconnect? It seems like extra
>work or a bit of redundancy in creating a function that exists?

Sort of right. When you initially set it up, it can be a little bit of
extra work, but you can get a lot of advantage (time-saving) out of it.

You can now use it all over the place in your code, without worrying about
typos, you know it works and how it works etc. If you want to change it to
do something else (eg add logging), you only have to do it in one place,
instead of searching through all your code looking for places that need it.

The same reasons for any sort of function in any code exist here :)


-----------------
      Chris Smith
http://www.squiz.net/