[thelist] mySQL and PHP troubles

travis forden travis at treevis.com
Sun Sep 2 23:11:16 CDT 2001


At 11:47 pm 9/2/01, you wrote:
>Better yet can you post all the relevant code including your includes/functions?

I have been able to connect to the DB with this function from the include db_connect(). I created the DB 'sample_db' and the table 'user', populating it through PHP.

Here is the code: (from the file show_db.php)

<?php
//show_more_db.php
//include "common_db.inc.php";

//common_db.inc.php starts here

$dbhost = 'localhost';
$dbusername = 'xxx';
$dbuserpassword = 'xxx';
$default_dbname = 'test';  // could this be causing a problem - specifying this then using a different DB?

$MYSQL_ERRNO = '';
$MYSQL_ERROR = '';

function db_connect() {
        global $dbhost, $dbusername, $dbuserpassword, $default_dbname;
        global $MYSQL_ERRNO, $MYSQL_ERROR;
                                 
        $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);
        if(!$link_id) {
            $MYSQL_ERRNO = 0;
                $MYSQL_ERROR = "connection failed to 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";
}
// common_db.inc.php ends here


$link_id = db_connect('sample_db');
$result = mysql_query("select * from user", $link_id);

while($query_data = mysql_fetch_row($result)) {
        echo "<p>'",$query_data[1],"' is also known as ",$query_data[3],"</p>";
}
?>




More information about the thelist mailing list