Jump to content

[SOLVED] "No Database Selected"


stublackett

Recommended Posts

Hi,

 

Just tried a PHP Script which inserts data into an SQL Table titled "user_info"

 

<?php

####################################################################

################ DATABASE CONNECT ##############################

####################################################################

$hostname = "localhost";

$db_user = "username";

$db_password = "password";

$db = "d4066435";

$db_table = "user_info";

 

 

# STOP HERE

####################################################################

# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE

$db = mysql_connect($hostname, $db_user, $db_password);

mysql_select_db($db_table,$db);

?>

 

<?php

if (isset($_REQUEST['Submit']))

{

# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE

$sql = "INSERT INTO $db_table(user_name,user_email) values ('$user_name','$user_email')";

if($result = mysql_query($sql ,$db))

{

echo "Thank you, Your information has been entered into our database";

}

else

{

echo "ERROR: ".mysql_error(); }

 

}

else

{

?>

 

The script seems to work until I get to the "ERROR : ".mysql_error();" Which states that there is no Database Selected

 

Am I missing some syntax or a variable out from the top connecting bit?

I've changed the values of username and password.

 

Cheers

 

Link to comment
https://forums.phpfreaks.com/topic/88358-solved-no-database-selected/
Share on other sites

this

<?php
####################################################################
################ DATABASE CONNECT ##############################
####################################################################
$hostname = "localhost";
$db_user = "username";
$db_password = "password";
$db = "d4066435";
$db_table = "user_info";


# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($db_table,$db);
?> 

 

should be

 

<?php
####################################################################
################ DATABASE CONNECT ##############################
####################################################################
$hostname = "localhost";
$db_user = "username";
$db_password = "password";
$dbname = "d4066435";
$db_table = "user_info";


# STOP HERE
####################################################################
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($dbname,$db);
?> 

 

you where ovewriting your variable also you were giving $db_table as the name of the database instead of $db however you overwrote $db with the mysql connect statement however the above should work like a charm

 

hope its helpful...

Spot on that works, Thanks!

 

Once its executed, Its not putting the data from the form into the table

 

Its picked up that there are "x" amount of user_name's and user_email's but no data to go with them

 

Any ideas on why its doing that?

 

Thanks

you've used $user_name, $user_email but i've not seen them initialized anywhere... if register_globals are off it will not work so you should

 

$user_name = $_POST['user_name'];
$user_email = $_POST['user_email'];

 

after

 

if (isset($_REQUEST['Submit']))
{ 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.