ProAnswers.org

how to connect mysql database using php script?

I want to write some info into mysql database and read info from database. please give some simple example.

1 Like

to connect mysql database from php

$DBconnect = mysql_connect (“localhost”,“username”,“password”);

mysql_select_db(“yourDatabaseName”, $DBconnect);

once it connect to database you can create tables using

$sqlWrite = “INSERT INTO newTable VALUES(‘name’, ‘age’)”;

mysql_query($sqlWrite);

to read data from mysql

$sqlRead = “SELECT * FROM newTable”;

$result = mysql_query($sqlRead);

if (mysql_num_rows($result ) == 0)
{
echo “no data”;
} else
{
while ($dbRecord=mysql_fetch_array($result)){
$val1 = $dbRecord[‘data1’];
$val2 = $dbRecord[‘data2’];

echo $val1;
echo $val2;
}

}