PHP Mysql connectivity
Use the mysql_connect( ) functiontoestablishedconnectiontothe MySQLserver.To accessthe
database functionalitywe have tomake a connectiontodatabase usingPhp.mysql_connect() function
isusedto establishthe connectiontomysql server.
Four argumentsneedtobe passedtomysql_connect() function.
Hostname : if you are working on local system , you can use localhost or you can also
provide ip address or server name.
username : if there is a existing user , you can provide username. default username is
'root'.
password : by default password is blank or null.
dbname : it is a optional field . it is basically a name of the database that need to be
connected.
mysql_connect(host, username, password, dbname);
Parameter Description
host(Server name) Either a host name(server name) or an IP address
username The MySQL user name
password The password to log in with
dbname Optional. The database to be used when performing queries
Note : There are more available parameters, but the ones listed above are the most
important. In the following example we store the connection in a variable ($con) for later
use in the script
<?php
// Create connection
$con=mysql_connect("localhost","root","") or die(mysql_error());
?>
Here localhost is server name. root is MySQL default user name. default password is blank and
database name is my_db. mysql_error( ) function provides mysql connectivity error message.
MySQL Close Connection
<?php
// Create connection
$con=mysql_connect("localhost","root","","my_db") or die(mysql_error());
//code to be executed...
// Close connection
mysql_close($con);
?>
after work with the database is done we have to close the connection using mysql_close()
function in which the connection to the database is passed.
Mysqli PHP Connectivity
How to connect PHP through mysqli. Create a registration form with 4 four fields
name,email id, mobile number and address. Fill all the fields click on save data submit
button to save your data inside "users" table using mysqli prepared statements insert.
To display all the values from "users" table click on disp data button it will display the
values using mysqli prepared statements select.
Create Database and Table
Database name:demo
Table:user
PHP Script(PHP mysqli connection )
<?php
//mysqli connectivity
$connect= new mysqli("localhost","root","","demo") or die("ERROR:could not
connect to the database!!!!");
extract($_POST);
if(isset($save))
{
$query="insert into users values('','$n','$e','$m','$add')";
if($connect->query($query))
{
echo 'congrates!! data saved';
}
else
{
echo 'data not saved '.$connect->error;
}
}
//display data
if(isset($disp))
{
$query="select * from users";
$result=$connect->query($query);
echo "<table border=1>";
echo "<tr><th>Name</th><th>Email</th><th>Mobile<
/th><th>Address</th></tr>";
while($row=$result->fetch_array())
{
echo "<tr>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['email']."</td>";
echo "<td>".$row['mobile']."</td>";
echo "<td>".$row['address']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
HTML Form
<style>
input,textarea{width:200px}
input[type=submit]{width:100px}
</style>
<form method="post">
<table width="218" border="0">
<tr>
<td width="208"><input type="text" name="n" placeholder="Enter your
name"/></td>
</tr>
<tr>
<td><input type="text" name="e" placeholder="Enter your email"/></td>
</tr>
<tr>
<td><input type="text" name="m" placeholder="Enter your mobile"/></td>
</tr>
<tr>
<td><textarea name="add" placeholder="Enter your address"></textarea></td>
</tr>
<tr>
<td>
<input type="submit" name="save" value="Save Data"/>
<input type="submit" name="disp" value="Display Data"/>
</td>
</tr>
</table>
</form>
Php mysql connectivity

Php mysql connectivity

  • 1.
    PHP Mysql connectivity Usethe mysql_connect( ) functiontoestablishedconnectiontothe MySQLserver.To accessthe database functionalitywe have tomake a connectiontodatabase usingPhp.mysql_connect() function isusedto establishthe connectiontomysql server. Four argumentsneedtobe passedtomysql_connect() function. Hostname : if you are working on local system , you can use localhost or you can also provide ip address or server name. username : if there is a existing user , you can provide username. default username is 'root'. password : by default password is blank or null. dbname : it is a optional field . it is basically a name of the database that need to be connected. mysql_connect(host, username, password, dbname); Parameter Description host(Server name) Either a host name(server name) or an IP address username The MySQL user name password The password to log in with dbname Optional. The database to be used when performing queries Note : There are more available parameters, but the ones listed above are the most important. In the following example we store the connection in a variable ($con) for later use in the script <?php // Create connection $con=mysql_connect("localhost","root","") or die(mysql_error()); ?> Here localhost is server name. root is MySQL default user name. default password is blank and database name is my_db. mysql_error( ) function provides mysql connectivity error message. MySQL Close Connection <?php // Create connection $con=mysql_connect("localhost","root","","my_db") or die(mysql_error()); //code to be executed...
  • 2.
    // Close connection mysql_close($con); ?> afterwork with the database is done we have to close the connection using mysql_close() function in which the connection to the database is passed. Mysqli PHP Connectivity How to connect PHP through mysqli. Create a registration form with 4 four fields name,email id, mobile number and address. Fill all the fields click on save data submit button to save your data inside "users" table using mysqli prepared statements insert. To display all the values from "users" table click on disp data button it will display the values using mysqli prepared statements select. Create Database and Table Database name:demo Table:user PHP Script(PHP mysqli connection ) <?php //mysqli connectivity $connect= new mysqli("localhost","root","","demo") or die("ERROR:could not connect to the database!!!!"); extract($_POST); if(isset($save)) { $query="insert into users values('','$n','$e','$m','$add')"; if($connect->query($query)) { echo 'congrates!! data saved'; }
  • 3.
    else { echo 'data notsaved '.$connect->error; } } //display data if(isset($disp)) { $query="select * from users"; $result=$connect->query($query); echo "<table border=1>"; echo "<tr><th>Name</th><th>Email</th><th>Mobile< /th><th>Address</th></tr>"; while($row=$result->fetch_array()) { echo "<tr>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['email']."</td>"; echo "<td>".$row['mobile']."</td>"; echo "<td>".$row['address']."</td>"; echo "</tr>"; } echo "</table>"; } ?> HTML Form <style>
  • 4.
    input,textarea{width:200px} input[type=submit]{width:100px} </style> <form method="post"> <table width="218"border="0"> <tr> <td width="208"><input type="text" name="n" placeholder="Enter your name"/></td> </tr> <tr> <td><input type="text" name="e" placeholder="Enter your email"/></td> </tr> <tr> <td><input type="text" name="m" placeholder="Enter your mobile"/></td> </tr> <tr> <td><textarea name="add" placeholder="Enter your address"></textarea></td> </tr> <tr> <td> <input type="submit" name="save" value="Save Data"/> <input type="submit" name="disp" value="Display Data"/> </td> </tr> </table> </form>