November 25, 2014

PHP MySQL Connection

MySQLI Connection - I for Improved

Connect the database from php throug MySQLI
 
/*hostname - localhost, user - user, password - null, database - test*/

$db = new mysqli('localhost', 'user', '', 'test');

if($db->connect_errno > 0){
    die('Error in connecting database (' . $db->connect_error . ')');
}

Now doing the simple query to get records from employee table
 
/* Do Query */

$sql = "SELECT * FROM `employee`;

if(!$result = $db->query($sql)){
    die('Query error ' . $db->error);
}

Display the output by fethcing the table records
 
/* Fetching records */

while($row = $result->fetch_assoc()){
    echo $row['username'];
}

No comments:

Post a Comment