Can't connect to database in wamp

I created a simple HTML form to add student details. It contains text boxes with first name, last name, age, phone number and email. And I created a database in wamp phpmyadmin and created a table named stud with fname, lname, age, phone, email fields and my PHP code to add data from the form to the stud table is below.

<?php
$host="localhost";
$username="";
$password="";
$db_name="student";


$tbl_name="stud"; 
$connect=mysqli_connect("$host", "$username", "$password")or die("cannot connect");
mysqli_select_db($connect,$db_name)or die("cannot select DB");
$fname=$_POST['fname'];    
$lname=$_POST['lname'];
$age=$_POST['age'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$sql="INSERT INTO $tbl_name(fname, lname, age, phone, email)VALUES('$fname', '$lname' ,'$age', '$phone',  `'$email')";`
$result=mysqli_query($connect,$sql);

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='student.php'>Back to main page</a>";
}
else { 
echo "ERROR";
}
?>
<?php

mysqli_close($connect);
?> 

      

when i run this code in browser it shows "unable to connect db". But when I created a table named stud in the test database, it worked. Please help me find a way to connect to my stud table in the student database in wamp.

+3


source to share


2 answers


You can also use below

$connect=mysqli_connect("localhost","username","password","db_name")or die("Error in database connection");

      

OR



Check your beacause username, default username in wamp:

root

+3


source


There is a bug in your code. use
mysqli_select_db($db_name, $con)

instead mysqli_select_db($con, $db_name)



0


source







All Articles