I cant seem to get this code to work. It keeps giving me the error "sorry: query failed". There is connection to the database cause if i input a wrong password it will give the error "mysql connection failed". The table pet is already created and populated with data. Please I need help
<?php
$conn = mysql_connect ("localhost", "vistor", "visitor", "test")
or die ("sorry: Mysql connection failed");
$query = "select * from pet";
$result = mysql_query ($query,$conn)
or die ("sorry: query failed");
while($row = mysql_fetch_array($result)) {
echo $row['code'], " ", $row['name'];
echo "<br>";
}
mysql_close($conn);
?>
Donny gave me the below code and i added some css to make it appear as a table but i am suspecting i have the coding wrong as I am getting errors. thanks
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
echo "<table border = '2'>"
<tr>
<th>id</th>
<th>name</th>
</tr>
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>;
}
echo "</table>";
?>
</body>
</html>