Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No Database selected when executing MySQL in php

Tags:

php

mysql

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);  
?>
like image 625
user2420874 Avatar asked Mar 16 '26 01:03

user2420874


2 Answers

Use

mysql_select_db( "Ebooks" ) or die( 'Error'. mysql_error() );
like image 141
Tamil Selvan C Avatar answered Mar 17 '26 13:03

Tamil Selvan C


Use this code:

mysql_select_db("Ebooks");//Select Database
like image 26
user4035 Avatar answered Mar 17 '26 15:03

user4035



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!