Squeakasaur Squeakasaur - 6 months ago 60
MySQL Question

Error No Database Selected PHP/mySQL

I have read a bunch of the other posts about similar issues but I am very new to this so I am likely missing something. Most of the other questions I found were a lot more complicated than mine. I am trying to follow the w3schools tutorial for this and am testing locally using XAMPP. Right now I am just trying to get this to work successfully and eventually I am planning to submit data into a mySQL database from a web form.


// Create connection
$conn = mysqli_connect($servername, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

$sql = "INSERT INTO owner_table (ownerFirst, ownerLast, mobile)
VALUES ('John', 'Doe', '1111111111')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);



The connection is not failing from what I can tell. Removing everything after the first if statement, and adding else { echo "success" } outputs success. Again though, I am new to PHP.

If someone knows that the answer has been given in another post PLEASE PLEASE comment here and ill close this out.



You should follow the proper syntax:

$conn = mysqli_connect(<host Name>, <username>, <password>, <database name>);

Reference: http://php.net/manual/en/function.mysqli-connect.php#refsect1-function.mysqli-connect-examples

Add two more parameters- username and password here:

$username = "root";  // Default values
$password = "";      // Default values

$conn = mysqli_connect($servername, $username, $password, $dbname);