Re: db connect problems.

From: Date: Wed, 28 Nov 2001 22:15:15 +0000
Subject: Re: db connect problems.
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-14672@lists.php.net to get a copy of this message
Or what about: common_db.inc with just the following: (which you would for security reasons put in a directory outside the webroot, and rename the file to .common_db.inc (with a . in front of it)) <?php $dbhost = 'localhost'; $dbusername = 'root'; $dbuserpassword = ''; $default_dbname = 'mysql'; ?> show_more_db.php with <?php include "./common_db.inc"; mysql_connect($dbhost, $dbusername, $dbuserpassword) or die ("<b>Couldn't connect to specified server</b>"); mysql_select_db($default_dbname) or die ("<b>Error, no database found sweetheart</b>"); $sql = "SELECT * FROM user"; $result = mysql_query($sql); while ($row = mysql_fetch_row($result)) { echo ($row[1]."<br>\n"); } mysql_close(); ?> Groetend, Roel Mulder At 22:52 28-11-2001 +0100, you wrote:
well, I didn't debug this massive script, but maybe you just want to connect to a mysql database? then this is the short way: $user = "your_username_for_the_database"; $pass = "your_password_for_the_database"; $con = mysql_connect("your_host", $user, $pass); if(!$con) {
    print "no connection";
} I really don't know if this is what you're asking for.... Leo Kuiper "Building a map in order to find what's not lost but left behind." - Beth Orton ----- Original Message ----- From: Ian <Duxbuz@bbtinternet.com.hubris.nl> To: <php-db@lists.php.net> Sent: Wednesday, November 28, 2001 5:52 PM Subject: [PHP-DB] db connect problems. Hello, wonder if anyone can debug this script. I copied it from an example in a book. There is a .php file and a .inc file This is the .inc file: <?php //common_db.inc $dbhost = 'localhost'; $dbusername = 'root'; $dbuserpassword = ''; $default_dbname = 'mysql'; $MYSQL_ERRNO = ''; $MYSQL_ERROR = ''; function db_connect($dbname="") { global $dbhost, $dbusername, $dbuserpassword, $default_dbname, $dbname; global $MYSQL_ERRNO, $MYSQL_ERROR; $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword); if(!$link_id) { $MYSQL_ERRNO = 0; $MYSQL_ERROR = "Connection failed to the host $dbhost."; return 0; } else if(empty($dbname) && !mysql_select_db($default_dbname)) { $MYSQL_ERRNO = mysql_errno(); $MYSQL_ERROR = mysql_error(); return 0; } else return $link_id; } function sql_error() { global $MYSQL_ERRNO, $MYSQL_ERROR; if(empty($MYSQL_ERROR)) { $MYSQL_ERRNO = mysql_errno(); $MYSQL_ERROR = mysql_error(); } return "$MYSQL_ERRNO: $MYSQL_ERROR"; } ?> the .php file is like this: <?php //show_more_db.php include "./common_db.inc"; $link_id = db_connect('sample_db'); $result = mysql_query("SELECT * FROM user", $link_id); while($query_data = mysql_fetch_row($result)) { ?> <?php echo "'",$query_data[1],"' is also php ' known as ",$query_data[3],"<P>"; } ?> Sorry about poxy formatting, hope it is legible. I seem to have a problem passing the database to the db_connect() function within the .php script. The $default_dbname variable within the .inc file is always selected instead of the argument I pass. Anyone help me please? Ta.
Mulder Technisch Advies Postbus 69 NL-2740 AB WADDINXVEEN tel. 0182-640184 fax. 0182-640185 http://www.mta.nl

« previous php.db (#14672) next »