PHP: use declared variable with data from MySQL inside an included file [closed]

74
November 06, 2021, at 00:20 AM
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed yesterday.

This post was edited and submitted for review yesterday.

Improve this question

htmlfile.php contais all the HTML
phpfile.php contains all the PHP and includes htmlfile.php
MySQL table called BOOKS has the columns: ID and TITLE
phpfile.php works like this:

$conn = new mysqli(server, user, password, db);
include "htmlfile.php";
//Here i store my titles from the table inside an array
$sql = "SELECT TITLE FROM BOOKS";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
          $titles[] = $row['TITLE'];
        }
    } else {
        echo "0 results";
    }

In my included htmlfile.php i want to use an element of the array inside a paragraph:

<body>
<p><?php echo $titles[2]; ?></p>
</body>

When i start the phpfile.php in my browser, the paragraph is empty.
So i thought about switching the places like this:

$conn = new mysqli(server, user, password, db);
$sql = "SELECT TITLE FROM BOOKS";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
          $titles[] = $row['TITLE'];
        }
    } else {
        echo "0 results";
    }
include "htmlfile.php";

But now most of the content in my HTML isn`t even showing up on the site. It looks completely messed up. I already tried to connect the database with my htmlfile.php but it didn't change anything. The array itself works correctly when i echo in phpfile.php
So how can i do this?

Answer 1

Your title is already stored in a variable. It's stored an an element in the $row array. If you want to output it in a paragraph element you can do this:

    while($row = $result->fetch_assoc()) {
        echo '<p>' , $row["TITLE"] , '</p>';
    }

If you want to store the element for use later you can do this (but this won't work well if you retrieve more than one title):

    while($row = $result->fetch_assoc()) {
        $book_1_title = $row['TITLE'];
    }
    echo '<p>' , $book_1_title , '</p>';

If you're storing several titles you might do this:

    $titles = []; // Create an array
    while($row = $result->fetch_assoc()) {
        $titles[] = $row['TITLE'];
    }

Note that the while loop will terminate when fetch_assoc() returns false. With a loop structured like that $row will contain false after the loop terminates, and your title(s) won't be available from there.

If your intention is to retrieve exactly one title then you don't need a while loop at all.

There's more than one way to do things, and your choices will be guided by what else you need to do.

READ ALSO
OPC UA Client(C#) with a GUI and a node js server

OPC UA Client(C#) with a GUI and a node js server

So I have created a nodejs Server from this tutorial: https://github

136
My Tflite model works perfectly in jupyter notebook. However shows me the wrong output when I try to run it in Android studio

My Tflite model works perfectly in jupyter notebook. However shows me the wrong output when I try to run it in Android studio

So I created my custom model for disease recognition based on the symptoms entered by the patientAnd it works perfectly on python, However after I converted it to tflite and ran it on android studio, It gives me wrong predictions irrespective of the input values

83
MySQL Header exclusion

MySQL Header exclusion

I'm trying to return all columns from a group of tables I'm joiningWe have some replacement data in columns to not publicly disclose certain information, each of those columns contains a very specific string after the columns original name

79