Help - Search - Members - Calendar
Full Version: Trouble selectiing a database in PHP
Invision Power Services > Community Forums > Community Web Design and Coding
Lemon Head
Hi all,
I'm having some problems selecting my database after I have successfully connected to it in PHP.

I keep getting the message "Warning: mysql_select_db(): 3 is not a valid MySQL-Link resource in /home/username/public_html/db/display.php on line 18
Can't use database :"

Here's my code if you can help:
CODE
<?
# Select database
$db_selected = mysql_select_db($db_name, $con);
if (!$db_selected) {
  die ('Can\'t use database : ' . mysql_error());
}
echo 'Graham, your database has been selected successfully';
?>


$dbname is my database name and $con is the variable which holds my database connection details used in the connection page which works fine.

I can connect to the database fine but am unable to select it prior to running a query to show all data contained in one of its tables.

Can someone spot what's wrong in the above code, please?

Thanks!
princetontiger
use this:

CODE
<?php

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
  die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
  die ('Can\'t use foo : ' . mysql_error());
}
?>


you need to add both parts.
princetontiger
woah

I FOUND A BUG. w00t.

when I posted this, I got a MySQL error. so, I posted it again, and got a MySQL error...

I was using quick reply...

I'll make a post about.
Lemon Head
Hi,
Thanks for the quick help. I've been looking at the code in the PHP manual website.

I have used a include before the code to get the connection which works fine.

This is the full code:
CODE
<?
# Get Global Config and connect to DB
require_once('./db_inc.php');
?>
<br>

<?
# Select database
$db_selected = mysql_select_db($db_name, $con);
if (!$db_selected) {
  die ('Can\'t use database : ' . mysql_error());
}
echo 'Graham, your database has been selected successfully';
?>


and this is what's in the db_inc.php file that sets up the connection:
CODE
<?
# DB config - it's more secure to keep this in an external PHP file, not publically accessible.
# Set appropriate $db_name, $db_hostname, $db_username and $db_password variables
$db_name = 'jackal_blahblah';
$db_username = 'jackal_blahblah';
$db_password = 'blah';
$db_hostname = 'localhost';

$con = mysql_connect( $db_hostname, $db_username, $db_password );
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($con);
?>


I figured if I had the connection to the database working okay, the mysql_select_db command would work okay based on the last connection which was successful?

Thanks again!
Lemon Head
QUOTE(rwx @ Apr 10 2005, 12:36 PM)
woah

I FOUND A BUG. w00t.

when I posted this, I got a MySQL error. so, I posted it again, and got a MySQL error...

I was using quick reply...

I'll make a post about.
*


Same thing happened to me when making this topic, it created two by accident!
princetontiger
everything looks fine... I am not sure...

your IPB must be working, though, so you know the database and everything work?
Rikki
QUOTE(Graham P. Smith @ Apr 10 2005, 12:41 PM)
Hi,
Thanks for the quick help. I've been looking at the code in the PHP manual website.

I have used a include before the code to get the connection which works fine.

This is the full code:
CODE
<?
# Get Global Config and connect to DB
require_once('./db_inc.php');
?>
<br>

<?
# Select database
$db_selected = mysql_select_db($db_name, $con);
if (!$db_selected) {
  die ('Can\'t use database : ' . mysql_error());
}
echo 'Graham, your database has been selected successfully';
?>


and this is what's in the db_inc.php file that sets up the connection:
CODE
<?
# DB config - it's more secure to keep this in an external PHP file, not publically accessible.
# Set appropriate $db_name, $db_hostname, $db_username and $db_password variables
$db_name = 'jackal_blahblah';
$db_username = 'jackal_blahblah';
$db_password = 'blah';
$db_hostname = 'localhost';

$con = mysql_connect( $db_hostname, $db_username, $db_password );
if (!$con) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($con);
?>


I figured if I had the connection to the database working okay, the mysql_select_db command would work okay based on the last connection which was successful?

Thanks again!
*


Why are you connecting then closing the connection, then trying to select a database? You need to have the connection open for the duration of everything you want to do on it original.gif Just remove the mysql_close() line.
Lemon Head
QUOTE(Rikki @ Apr 10 2005, 12:53 PM)
Why are you connecting then closing the connection, then trying to select a database? You need to have the connection open for the duration of everything you want to do on it original.gif Just remove the mysql_close() line.
*



That's brilliant Rikki! Well spotted! I leave it open in the conenction page and close it after I finish running my queries. Nice one!

Graham.
Rikki
PHP should close the connection for you as part of its cleanup, but it's good practice to do it yourself at the end of your script.
Lemon Head
Thank Rikki, that's useful to know. I'm just learning and following some tutorials.
I don't want to write the next IPB, just get some text from a mySQL database and show it on a webpage formatted with CSS!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.