I want to retrieve data from a database with PHP and show it on a website.
This code does not work correctly. I want to display all cars that are Chevy on my database.
<?php
$db = mysqli_connect("localhost","myusername",
"mypassword","database");
if (mysqli_connect_errno()) {
echo("Could not connect" .
mysqli_connect_error($db) . "</p>");
exit(" ");
}
$result = mysqli_query($query);
if(!$result){
echo "<p> Could not retrieve at this time, please come back soon</p>" .
mysqli_error($dv);
}
$data = mysql_query("SELECT * FROM cars where carType = 'Chevy' AND active = 1")
or die(mysql_error());
echo"<table border cellpadding=3>";
while($row= mysql_fetch_array( $data ))
{
echo"<tr>";
echo"<th>Name:</th> <td>".$row['name'] . "</td> ";
echo"<th>ImagePath:</th> <td>".$row['imagePath'] . " </td></tr>";
echo"<th>Description:</th> <td>".$row['description'] . "</td> ";
echo"<th>Price:</th> <td>".$row['Price'] . " </td></tr>";
}
echo"</table>";
?>
How do I get data from the database with PHP?
you are not querying to database so it wont give you result
this is how it works
connect to the database by
mysql_connect()mysql_connect("localhost", "username", "password") or die(mysql_error());
than select the database like
mysql_select_db()mysql_select_db("Database_Name") or die(mysql_error());
you need to use
mysql_query()like
to see if error
and than
mysql_fetch_array()if($result){
while($row= mysql_fetch_array( $result )) {
//result
}
}
so try
**Note:**
`Mysql_*` function are deprecated so use `PDO` or `MySQLi` instead . I would suggest PDO its lot more easier and simple to read you can learn here [PDO Tutorial for MySQL Developers][5] also check [Pdo for beginners ( why ? and how ?)][6]