For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > February 2005 > mysql_connect succeeds but select_db fails









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author mysql_connect succeeds but select_db fails
Jared

2005-02-20, 3:55 pm

This is very strange.

I'm running MySQL 4.1.10, and php 4.3.10, on a FreeBSD 4.10 box. The
database has been created, and permissions are set right. I can access
the database from bin/mysql without a problem, too, using -uweb
-pblahblahblah.

<?php

$connection = @ mysql_connect("localhost", "web", "blahblahblah") ||
die( mysql_error());

printf("conn = %d<p>", $connection);

mysql_select_db("somedb", $connection) || die (mysql_error() );

?>

which yields:

conn = 1

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link
resource in /usr/local/apache8001/htdocs/scratch/index.php on line 7

I am so at a loss, and could use any help you could offer.

Andy Hassall

2005-02-20, 3:55 pm

On 20 Feb 2005 06:16:17 -0800, "Jared" <jared.t.boehm@gmail.com> wrote:

>This is very strange.
>
>I'm running MySQL 4.1.10, and php 4.3.10, on a FreeBSD 4.10 box. The
>database has been created, and permissions are set right. I can access
>the database from bin/mysql without a problem, too, using -uweb
>-pblahblahblah.
>
><?php
>
>$connection = @ mysql_connect("localhost", "web", "blahblahblah") ||
>die( mysql_error());


Actually you probably want the 'or' operator - not ||. There's a subtle
difference related to operator precedence.

$connection = @mysql_connect("localhost", "web", "blahblahblah")
or die(mysql_error());

http://uk.php.net/manual/en/languag...tors.precedence

>printf("conn = %d<p>", $connection);
>
>mysql_select_db("somedb", $connection) || die (mysql_error() );
>
>which yields:
>
>conn = 1


Should have been something like 'MySQL Link Resource #1'- but you'd have to
use %s in the format string anyway.

>Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link
>resource in /usr/local/apache8001/htdocs/scratch/index.php on line 7
>
>I am so at a loss, and could use any help you could offer.


--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Frank East

2005-02-28, 8:56 pm

On 2005-02-20 09:16:17 -0500, "Jared" <jared.t.boehm@gmail.com> said:

> This is very strange.
>
> I'm running MySQL 4.1.10, and php 4.3.10, on a FreeBSD 4.10 box. The
> database has been created, and permissions are set right. I can access
> the database from bin/mysql without a problem, too, using -uweb
> -pblahblahblah.
>
> <?php
>
> $connection = @ mysql_connect("localhost", "web", "blahblahblah") ||
> die( mysql_error());
>
> printf("conn = %d<p>", $connection);
>
> mysql_select_db("somedb", $connection) || die (mysql_error() );
>
> ?>
>
> which yields:
>
> conn = 1
>
> Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link
> resource in /usr/local/apache8001/htdocs/scratch/index.php on line 7
>
> I am so at a loss, and could use any help you could offer.


You do want to use 'or' and also be certain that you've granted rights
to the user you are logging in as to access and do stuff with that
database...

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com