Fred, I am more confused now that I know the script works your end.
I have the sample_db setup correctly, as I can connect to it if I change the $default_dbname value to hold the sample_db. Whatever I put in the $default_dbname variable I can connect to no trouble. Passing the database as an argument is a different story though, it just won't work! I try all different variations of quotation marks on the db_connect() function, like this. function db_connect($dbname=" ") function db_connect($dbname=' ') and at other end: $link_id = db_connect("sample_db") $link_id = db_connect('sample_db') I wonder if it is the first part of include file: <?php //common_db.inc $dbhost = 'localhost'; $dbusername = 'root'; $dbuserpassword = ''; $default_dbname = 'mysql'; Did the version you used look like this? The one I originally posted was slightly different, but the one above is how mine looks. Thanks for your time. Ian. "Fred" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > I'm not sure what that script is supposed to accomplish, it seems a bit > obfuscated and useless to me, nevertheless it correctly used the supplied > database rather than the default one when I tested it. You may want to make > sure that your sample_db actually exists. > > Fred > > Ian <[EMAIL PROTECTED]> wrote in message > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > Fred, > > > > sample_db 'is' my database name, so this should work? > > > > Well it keeps choosing my $default_db value. > > > > Can you try my code and tell me if it works for you please? > > > > Thankyou. > > "Fred" <[EMAIL PROTECTED]> wrote in message > > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > > How are you passing the name of the database? Are you replacing > > 'sample_db' > > > in the line: > > > $link_id = db_connect('sample_db'); > > > in show_more_db.php? > > > > > > If so, then I do not see where there would be a problem. > > > > > > Fred > > > > > > Ian <[EMAIL PROTECTED]> wrote in message > > > [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > > > > Hello, > > > > > > > > Wonder if anyone can help me out. > > > > > > > > I am trying to run an example out of a book Beginning Php by wrox. > > > > > > > > One of the examples is supposed to allow me to connect to a database > by > > > > using a function that takes a database as an argument. It has an > include > > > > file that contains the function. > > > > > > > > Problem I get is that the argument never seems to get passed and the > > > > function always uses the default value. > > > > > > > > I would greatly appreciate if someone could tell me why this happens. > I > > > > would also appreciate if no one rewrites a completely different > script, > > as > > > I > > > > am after debugging this one. ;-) > > > > > > > > Here is the include file: > > > > > > > > <?php > > > > file://common_db.inc > > > > $dbhost = 'localhost'; > > > > $dbusername = 'phpuser'; > > > > $dbuserpassword = 'phppass'; > > > > $default_dbname = 'mysql'; > > > > > > > > $MYSQL_ERRNO = ''; > > > > $MYSQL_ERROR = ''; > > > > > > > > function db_connect($dbname=' ') { > > > > 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 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 if(!empty($dbname) && !mysql_select_db($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"; > > > > } > > > > ?> > > > > > > > > And here is the php script that uses this include file: > > > > > > > > > > > > <?php > > > > file://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>"; > > > > } > > > > ?> > > > > > > > > > > > > I hope someone can help me out please. > > > > > > > > Thankyou. > > > > > > > > > > > > > > > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]