Q:

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?

A:

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();
}

