Re: db connect
| From: | Fred | Date: | Fri, 30 Nov 2001 00:50:42 +0000 |
| Subject: | Re: db connect | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-76130@lists.php.net to get a copy of this message | ||
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 <Duxbuz@bbtinternet.com> wrote in message
news:20011129220704.55551.qmail@pb1.pair.com...
> 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" <fred.nospam.please@querymagic.com> wrote in message
> news:20011129184157.1385.qmail@pb1.pair.com...
> > 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 <Duxbuz@bbtinternet.com> wrote in message
> > news:20011129115833.22923.qmail@pb1.pair.com...
> > > 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.
> > >
> > >
> >
> >
>
>