No Database Selected Error In Mysql Php
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow no database selected phpmyadmin the company Business Learn more about hiring developers or posting ads with us Stack no database selected error in php mysqli Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of
No Database Selected Php Mysqli
6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up PHP MySQL - Error: No Database selected up vote -3 down vote favorite I am trying to read
Select Command Denied To User ''@'localhost' For Table
and write to a database. Here is the code I have so far: $mysql = mysqli_connect("example.com", "johndoe", "abc123"); // replace with actual credidentials $sql = "CREATE DATABASE IF NOT EXISTS dbname"; if (!mysqli_query($mysql, $sql)) { echo "Error creating database: " . mysqli_error($mysql); } if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_close($mysql); $mysql = mysqli_connect("example.com", "johndoe", "abc123", "dbname"); // replace with actual credidentials $sql = "CREATE TABLE no database selected phpmyadmin import IF NOT EXISTS Users(ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), username CHAR(15), password CHAR(15), email CHAR(50))"; if (!mysqli_query($mysql, $sql)) { echo "Error creating table: " . mysqli_error($mysql); } $sql = "INSERT INTO Customers(username, password, email) VALUES(" . $username . ", " . $password . ", " . $email . ")"; if (!mysqli_query($mysql, $sql)) { echo "Error: " . mysqli_error($mysql); } mysqli_close($mysql); However, when I try to run it, it has an error: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' , )' at line 1 Could anybody tell me how to fix this? php mysql create-table share|improve this question edited Feb 23 '14 at 18:12 asked Feb 23 '14 at 18:01 Oliver 5073923 3 You have to select the db before creating the table.. –Abhik Chakraborty Feb 23 '14 at 18:02 add a comment| 6 Answers 6 active oldest votes up vote 0 down vote accepted First check mysqli_select_db if it returns false then create database. try like this: $mysql = mysqli_connect("example.com", "johndoe", "abc123") or die(mysqli_connect_error()); // replace with actual credidentials if (!mysqli_select_db($mysql,'hardestgame_accounts')) { $sql = "CREATE DATABASE IF NOT EXISTS hardestgame_accounts"; if (!mysqli_query($mysql, $sql)) { echo "Error creating database: " . mysqli_error($mysql); } }
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about
Mysql_select_db() Expects Parameter 2 To Be Resource
Stack Overflow the company Business Learn more about hiring developers or posting ads with no database selected codeigniter us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is php select database a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Mysql Error could not enter data. No database selected up vote 0 down vote http://stackoverflow.com/questions/21972146/php-mysql-error-no-database-selected favorite This is my Php file code. When i insert the data using input fields in HTML i get the Error could not enter data. No database selected. $dbhost="localhost"; $dbuser="root"; $dbpass=""; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'INSERT INTO sale '. '(Name, Contact, Email, Property_type, Price, Location) '. 'VALUES ( "$a", "$b", "$c", "$d", "$e", "$f" )'; mysql_select_db('gharghar_gharastee'); $retval http://stackoverflow.com/questions/38167912/mysql-error-could-not-enter-data-no-database-selected = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not enter data: ' . mysql_error()); } echo "Entered data successfully\n"; mysql_close($conn); ?> php mysql share|improve this question asked Jul 3 at 8:00 Sheikh Noman Mehmood 66 What happens if you run query direct database? –Perdeep Singh Jul 3 at 8:09 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''(Name, Contact, Email, Property_type, Price, Location) '. VALUES ( "oman", "' at line 3 –Sheikh Noman Mehmood Jul 3 at 8:17 So theres the problem your query got issue could you please print full query here that you tried to run on db. –Perdeep Singh Jul 3 at 8:19 Warning: You are using an obsolete database API which has been removed entirely from the latest version of PHP. You should use a modern replacement. –Quentin Jul 3 at 16:55 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote Try checking the connection to your db // make foo the current db $db_selected = mysql_select_db('gharghar_gharastee', $conn); if (!$db_selected) { die ('Can\'t use gharghar_gharastee : ' . mysql_error()); } A
the error below in red: You are connectedrunning query:INSERT INTO domains(domain, sex, mail) values('website', https://www.sitepoint.com/community/t/no-database-selected-mysql-error/5244 'male/female', 'email')INSERT error : No database selected I can access this db in mysql console & phpmyadmin as the root user with no password.All privileges is granted to the root user. Here is my php code, "; } $query = "INSERT no database INTO domains(domain, sex, mail) values('website', 'F', 'mail')"; print "running query:
\$query
\"; mysql_query($query, $connect) or die ("INSERT error: ".mysql_error()); mysql_close($connect); ?> All help appreciated! AnthonySterling 2009-07-13 09:30:17 UTC #2 Does the following work? kingr 2009-07-13 09:51:05 UTC #3 SilverBulletUK said: Does the following work? php
$rConn = mysql_connect('localhost', 'root');
if((false === is_resource($rConn)) || (false === mysql_select_db('p24', $rConn)))
{
echo mysql_error();
exit;
}
if(false === mysql_query("INSERT INTO domains (domain, sex, mail)VALUES('website', 'F', 'mail')"))
{
echo mysql_error();
exit;
}
mysql_close($rConn);
?>
thanks for the rapid response. No, now i get this Unknown database 'p24' Seems like i cant access the db...anymore suggestions AnthonySterling 2009-07-13 10:01:28 UTC #4 OK, what do you get with the following code... ', $aRecord['database'], '
', $aRecord['database'], '
'; should this not display my db name? AnthonySterling 2009-07-13 10:31:27 UTC #6 Sorry, there's a typo in the code I supplied.