mogogogo
mogogogo

Reputation: 109

MySQL: no database selected error

hi i have an sql error says: no database selected this is my KK.php file:

$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$message = mysqli_real_escape_string($link, $_POST['input1']);

$sql="INSERT INTO demo (message)
VALUES ('$message')";

if (!mysqli_query($link,$sql)) {
die('Error: ' . mysqli_error($link));
 }
echo "1 record added";

 mysqli_close($link);
 ?> 

my database is "MyDB", and my demo.php file:

<form action="KK.php" method="post" />
<p>Message: <input type="text" name="input1" /></p>
<input type="submit" value="Submit" />
</form>

so what is the problem?, how do i select database?

Upvotes: 2

Views: 1261

Answers (3)

Ne Ma
Ne Ma

Reputation: 1709

Further to my comment above here is the code you are missing.

$user_name = "root";
$password = "";
$database = "MyDB";
$server = "localhost";

// This
$link = mysqli_connect($server,$user_name,$password,$database); // This
// This

// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

Upvotes: 1

Harikrishnan
Harikrishnan

Reputation: 9979

You didn't call mysqli_connect

$con = mysqli_connect("localhost","my_user","my_password","my_db");

Upvotes: 0

John Boker
John Boker

Reputation: 83729

Are you forgetting to call the mysqli_connect to get your connection?

ie:

$con = mysqli_connect("localhost","my_user","my_password","my_db");

Upvotes: 2

Related Questions