Friday 6 May 2011

Connecting to the database in php Mysql




Connecting to the database in php Mysql


PHP is providing a library to connect with the Mysql database. That is “extension=php_mqsql.dll”. This library is providing number of functions.

i). Mysql_connect: This function is used to establish a connection between php application and Mysql database. We need to provide the arguments like server name, username, and password.

How to connect to the Mysql database?

How to establish a connection between mysql database and php application?

How to create a database through php script?

<?php
f($con=mysql_connect("localhost","root",""))
echo "connected to The Mysql Server";
else
echo "not Connected to The MYsql Server";
if (mysql_query("create database mynewdb", $con))
echo "DataBase Created";
else
echo "DataBase not created";
?>

Now open the http://localhost/phpmyadmin/ and check whether the data base is created or not on the name “mynewdb”.


-*-*-

ii).Mysql_error: It returns an error massage, if any error accrued at the time of application executions.

 iii). Mysql_errno: It returns the error number, if any error accrued at the time of script executions.

How to know the error reports in php application?

How to know the error number in the php script?


<?php
$con=mysql_connect("locahost","root","");
mysql_select_db("mynewdb",$con);
$sqlstt="create table tbl_user(uname varchar(100),pwd varchar(100))";
if(mysql_query($sqlstt))
echo "Table is Created";
else
echo Mysql_error();
?>

If you execute this script you will get the below output.


Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'locahost' (11004) in C:\xampp\htdocs\dinnu.php on line 2

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in C:\xampp\htdocs\dinnu.php on line 3

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\dinnu.php on line 5

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\xampp\htdocs\dinnu.php on line 5
Access denied for user 'ODBC'@'localhost' (using password: NO)


*Because you provided the wrong password and username. 


Whenever we are passing string value to the database it should be in a single quotation.


The below script is very very important. This script is how to store the username and password values in database .


How to store username and password in database:



<?php
if(isset ($_POST['sub']))
{
$Uname=$_POST['txtUname'];
$Pwd=$_POST['txtPwd'];
$con=mysql_connect("localhost","root","");
mysql_select_db("mynewdb",$con);
$sqlstt="insert into tbl_user values('$Uname','$Pwd')";
if(mysql_query($sqlstt))
echo "record is Inserted";
else
echo mysql_error();
}
?>
<form method="post" action="">
Username : <input type="text" name="txtUname">
<br>
Password : <input type="text" name="txtPwd">
<br>
<input type="submit" name="sub" value="Reg">
</form>
<script>
function funcheck()
{
Uname=document.getElementByid('txtUname').value
Pwd=document.getElementByid('txtPwd').value
if($Uname.lenght==0)
{
alert("Enter Username")
return false;
}
else
if ($Pwd.lenght==0)
{
alert("Enter Password")
return false;
}
}
</script>


1 comment:

  1. On Thursday, Jackson php notes I drove to Jacksonville, AL, to meet up with some more close friends. Beth, Lyndsay, php notes Emily were all in ADPi 2 years above me at Auburn, php notes I look up to php notesm php notes respect php notesm so much! Three of us have had boys within weeks of each ophp notesr, php notes Emily is php notes pro with 3 girls under php notes age of 3

    ReplyDelete