Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i display my SELECT data correctly in PHP website?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        <?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?

>Solution :

It seams like you are not using any framework, so if that is true, you need to add an include statement at the top of your site.php, something like this

<?php require 'config.php'; ?>

so that the config.php file is executed before the content of your site.php file is.

however, I must add that ( beyond didactic purposes ) what you are doing is not a good practice. It would be better to use a framework like "laminas" or any other, that allows you do Separation of concerns, Dependency injection, Inversion of Control, etc.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading