<!DOCTYPE html>
<html>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "db_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, field1,field2 FROM tableName";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<br> id: ". $row["id"]. " - Field1: ". $row["field1"]. " - Field2:" . $row["field2"] . "<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
?>
</body>
</html>
The connection details can of course be held in a separate file, with the following being added to the above php file;
<?php require_once('Connections/mysqltest.php'); ?>
The mysqltest.php file would look something like;
<?php # FileName="Connection_php_mysql.htm" # Type="MYSQL" # HTTP="true"$servername = "localhost";
$mysqltest = mysql_pconnect($servername, $usernamet, $password) or trigger_error(mysql_error(),E_USER_ERROR);
$username = "username";
$password = "password";
$dbname = "db_name";
?>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.