Connecting php with MySQL and creating a database
For every program that using database, we need to connect php with MySQL. We can only access the database after builting a successful connection with the database. Don't forget to start the MySQL process in the XAMPP control panel if you are using the XAMPP software.
Connecting program:
Output:
The default username for XAMPP is root and there is no password in XAMPP. Here we created a database named mydb.
eg:
The $conn->connect_error returns the error message related to the connection with the database.we need to close the connection between the database and php. For that purpose,we are using $conn->close() to close this connection.
Connecting program:
<?php $servername="localhost"; $username="username"; $password="password"; //connecting... $conn=new mysqli($servername,$username,$password); //checking... if($conn->connect_error) { die("connection failed:".$conn->connect_error); } $sql="create DATABASE mydb"; if($conn->query($sql)==TRUE) { echo "database created sucessfully"; } else{ echo "error in creating database:".$conn->error; } $conn->close(); ?>
Output:
database created successfully
The default username for XAMPP is root and there is no password in XAMPP. Here we created a database named mydb.
eg:
$username="root";
$password="";
$password="";
The $conn->connect_error returns the error message related to the connection with the database.we need to close the connection between the database and php. For that purpose,we are using $conn->close() to close this connection.
⇐Prev
Next⇒
Comments
Post a Comment