PDA

View Full Version : How to include/require mysql_connect


Smiley_AB
6-8-02, 06:08 PM
Can some of you more seasoned php cgi users please comment on the following. Thank you in advance for your help.

Scenario:
- a hack that is familiar with php module behavior, but not php cgi
- finally got past the ascii/chmod thing
- multiple php pages that call mysql
- can't access the mysql link resource

Wish List:
- put $lr=mysql_connect ("mystrings") in dbconnect.php
- use include/require ("dbconnect.php") in let's say db_test.php (and other php pages) which connects to mysql, thereby avoiding using the connect string on each page that calls mysql
- have the 'link resource' object ($lr) available in the calling page that required the dbconnect.php, so that in any other page that includes/requires dbconnect.php, we can use $x=mysql_query ("query", $lr)

Attempted:
- renamed dbconnect.php to dbconnect.inc
- 500 error (db_test.php can't load dbconnect.inc)
- dbconnect.inc code is visible in browser (not parsed)

HalfaBee
6-8-02, 06:36 PM
I use include("db_open.php") where db_open.php has

$link=mysql_connect( "localhost","user","passwd");

in it.

This is chmod to 500 so only scripts on the server can read the file.


Fairly hack proof

Smiley_AB
6-8-02, 08:13 PM
Thanks for the prompt reply and pointing out the chmod 500. You leave me with the impression that you successfully use/reuse the link resource object that is created by dbconnect.php (the required page), in db_test.php (the page that called dbconnect). I still get the old "not a valid MySQL-Link resource" in /htdocs/db_test.php. Calling db_test.php, I would expect to see two lines in a browser, something like this:
coname
coname

Can you please point out what is wrong/missing in the following snipets?

*** db_test.php ***
#!/usr/local/bin/php
<html><head><title>DB Test</title></head><body>
<?php
$host = $HTTP_SERVER_VARS["HTTP_HOST"];
require("http://$host/cgi-bin/dbconnect.php");

$rs = mysql_query("select id, coname from company where id=1", $cn);
$row = mysql_fetch_row($rs);
echo "Test from db_test: ".$row[1];
?>
</body>
</html>
*** end db_test.php ***


*** dbconnect.php ***
#!/usr/local/bin/php
<?php
$debug = 1;

if (!$cn = mysql_connect ("server", "user", "pass")) MyErrr("cn");
if (!mysql_select_db ("dbase", $cn)) MyErrr("select");

if ($debug)
{
$rs = mysql_query("select id, coname from company where id=1", $cn);
$row = mysql_fetch_row($rs);
echo "Test from connect: ".$row[1];
}
?>
*** end dbconnect.php ***

HalfaBee
6-8-02, 10:58 PM
When testing for returned values it is better to check for 'false'

PHPreturns a different type of value you should use

$cn = mysql_connect();
if( $cn===false ) error();

Yes I did mean === this tests for a correct type not just a value

On error mysql_connect returns a BOOLEAN not a resource.


if you don't need the script to run any more you can use

$cn= mysql_connect() or die( "Could not connect to database" );

not very subtle but works.

Another thread actually had mysql_connect() return an error of
"to many connections" ( there is a limit of 100 ) my response to that was to loop and try again for a good connection.

I can't imagine this happening too often but it obviously can.


Hope this fixes the problem

Smiley_AB
6-9-02, 03:18 AM
Thanks HalfaBee for taking a swing at this.

My trouble is the $cn is valid only within the page I call with require().

It is not valid in the calling page.

ps. got your autoresponder test message

HalfaBee
6-9-02, 04:26 AM
Once the link is established you shouldn't need to refer to $cn again.

PHP uses the link that is currently open even if you call mysql_connect again.

the link is not required in most functions.


I have a db_open script that opens the mysql link and I just include or require this into any scripts that need to acess the database.

halfabee

PS
thanks for testing it I just wanted a few tests to check that I decoded the return address correctly

Sindol
6-9-02, 01:16 PM
Could try requiring a class as well.

Make a file called database or something like that, and in it put:


class Database {

var $host = "www.yourhost.com"; // Change this
var $username = "yourusername"; // Change this
var $password = "yourpassword"; // Change this
var $database = "databasename"; // Change this
var $linkID = 0;

function Database($autostart = true) {

if($autostart) {

$this->connect();

}
}

function connect() {

$this->linkID = @mysql_connect($this->host, $this->username, $this->password) or die("Could not connect to SQL Database");

if($this->linkID) {

@mysql_select_db($this->database);

}
}

function get_linkID() {

return $this->linkID;

}
}

$db = new Database(); /* $db may be any name you want, change new Database() to new Database(false) if you do not want the class to automatically connect when it is initialized */
$cn = $db->get_linkID();


Now just require your database file.
There may be easier solutions, but when it comes to SQL and anything in PHP where you repeat something OVER and OVER, its much better to go with classes, plus this simple little class could be expanded, you could add mysql_query, ect into it.