2

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?

3
  • 2
    You have no mysqli_connect statement before mysqli_connect_errno(). Thus you have not selected a database to work with. Commented May 7, 2014 at 14:40
  • Where's the code where you use $database? Commented May 7, 2014 at 14:41
  • You need to connect to Mysql followed by selecting a DB which is missing in the code php.net/manual/en/function.mysqli-connect.php php.net/manual/en/mysqli.select-db.php Commented May 7, 2014 at 14:41

3 Answers 3

2

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

ie:

$con = mysqli_connect("localhost","my_user","my_password","my_db");
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You didn't call mysqli_connect

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.