-1

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?

4
  • Not sure wether the $res is correct. I found this in a tutorial and tried it with my own, but never used this $res again. Maybe u can explain me Commented Mar 1, 2022 at 19:14
  • 4
    I don't see anything in your site.php that includes or requires config.php. It's likely that your database connection code is not being invoked. Commented Mar 1, 2022 at 19:17
  • 1
    Indeed, $res is the result from the query, but you don't show any code that fetches rows of data from that result. I assume that's a missing step to assign values to other variables like $nom and $description and others, but you don't show code for it. Anyway, it's a secondary problem because the first problem is that you aren't connecting. Commented Mar 1, 2022 at 19:19
  • thanks i included the connection. Now i get the following problems: Warning: Undefined variable $nom in C:\xampp\htdocs\Version\site.php on line 47 Warning: Undefined variable $description in C:\xampp\htdocs\Version\site.php on line 48 nom and description are columns in my table "dish", how can i display them belonging to an id @BillKarwin Commented Mar 1, 2022 at 19:46

1 Answer 1

1

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.

in addition the code need some way to assign the values from the result-set. yo can try with some like this

<?php
 while($obj = $res->fetch_object()){
            $description =$obj->description;
            $nom =$obj->nom;
            $did =$obj->did;
?>
            <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>
<?php
        } 
        $res->close();
?>

That assuming your table columns names are description, nom and did you can read the manual of the result object https://www.php.net/manual/en/class.mysqli-result.php

Sign up to request clarification or add additional context in comments.

2 Comments

thanks i included the connection. Now i get the following problems: Warning: Undefined variable $nom in C:\xampp\htdocs\Version\site.php on line 47 Warning: Undefined variable $description in C:\xampp\htdocs\Version\site.php on line 48 nom and description are columns in my table "dish", how can i display them belonging to an id @PCJ
i just add my comments in the answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.