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
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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.