'How can i display my SELECT data correctly in PHP website? [duplicate]
My database is working fine, but i am facing problems using the data and display them in my site.php
So in my config.php file, i have the following code:
<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'xxx');
/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>
In my site.php i am using the following code:
<?php
$sql = "SELECT * FROM dish";
$res = mysqli_query($link, $sql);
?>
<div class="col-md-3">
<div class="thumbnail">
<a href="index.php">
<img src="img/gallery/01.jpg" class="rounded-top">
</a>
<h4 class="text-center my-3"><?php echo $nom ?></h4>
<p class="text-center"><?php echo $description ?></p>
<a href="index.php?dish_did<?php echo $did; ?>" class="btn btn-success" role="button">Rate Now!</a>
</div>
</div>
In my localhost site.php i get the following error
Warning: Undefined variable $link in C:\xampp\htdocs\Version\site.php on line 39
Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in C:\xampp\htdocs\Version\site.php:39 Stack trace: #0 C:\xampp\htdocs\Version\site.php(39): mysqli_query(NULL, 'SELECT * FROM d...') #1 {main} thrown in C:\xampp\htdocs\Version\site.php on line 39
line 39 is this:
$res = mysqli_query($link, $sql);
How can i fix this and use my database correct in PHP?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
