user2420874
user2420874

Reputation: 33

No Database selected when executing MySQL in php

I'm having problems with this php code which needs to create a table in the database specified by the user. But whenever I try to execute the SQL it tells me no database selected. My code is as follow

<?php  
$con = mysql_connect("127.0.0.1","peter")
or die('Error connecting to mysql'); // Check connection  
// Create table 
mysql_select_db("USE Ebooks");//Select Database

$foldername = mysql_real_escape_string($_POST['foldername']);//Obtain Folder Name 

$sql = sprintf("CREATE TABLE %s (ID CHAR(3) ,Books CHAR(30))", $foldername);  
mysql_query($sql) or die(mysql_error()); 
mysql_close($con);  
?>

Upvotes: 3

Views: 9945

Answers (2)

Tamil Selvan C
Tamil Selvan C

Reputation: 20209

Use

mysql_select_db( "Ebooks" ) or die( 'Error'. mysql_error() );

Upvotes: 3

user4035
user4035

Reputation: 23729

Use this code:

mysql_select_db("Ebooks");//Select Database

Upvotes: 1

Related Questions