Page 1 of 1

PHP inside HTML not running

Posted: Thu Oct 17, 2024 9:26 am
by zxspectrum2
I have a HTML object and inside it is this PHP script, database details blanked out for here :-

Code: Select all

<?php
echo "DB Test";
// Database connection details
$servername = "";
$username = " ";
$password = "";
$dbname = "";
echo "DB Test connecting";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
echo "DB Test done";
echo $conn;
// Check connection
if ($conn->connect_error) {
   
    die("Connection failed: " . $conn->connect_error);
}
echo "<p>2ID: "  . " - title: " .   "</p>";
// Write a SQL query to select data from a table
$sql = "SELECT id, title FROM filedownloads";
$result = $conn->query($sql);

// Iterate through the result set and display data
if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<p>ID: " . $row["id"] . " - title: " . $row["title"] . "</p>";
    }
} else {
    echo "0 results";
}

// Close the connection
$conn->close();
?>
The PHP code is syntactically okay but whether or not it is good PHP is another question.
When I run it I get the following output.
DB TestDB Test connecting

Is there something I am missing?

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 9:56 am
by Pablo
First note that this is unrelated to the software.
The code will be inserted "AS IS" , so if it does not work then there is most likely something wrong with the code.
If there is no error in the browser then you can look it up in the PHP error log on the server.

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 10:05 am
by zxspectrum2
Okay thanks Pablo, I will need to test this outside of the web builder app.

Regards
Ken

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 10:09 am
by wwonderfull
Try to do things correctly if possible as it is database related code so any mistake will lead to information leakage. I hope you know what you are doing.

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 10:10 am
by BaconFries
I will need to test this outside of the web builder app.
You should note that you cannot "Preview" php locally in the software you will need to publish to your host/server before you will see the results. Or you will need to install the likes of xampp to set with on your PC.

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 11:42 am
by lummis
Looking at the code, I wonder if you can achieve your objective by using one of the Database/Tables extensions (seehttps://www.wysiwygwebbuilder.com/extensions.html) either paid or unpaid. With some you can insert your own MySQL query and you can rely upon the extension to deal with the connection to the database.

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 2:08 pm
by zxspectrum2
BaconFries wrote: Thu Oct 17, 2024 10:10 am
I will need to test this outside of the web builder app.
You should note that you cannot "Preview" php locally in the software you will need to publish to your host/server before you will see the results. Or you will need to install the likes of xampp to set with on your PC.
Yes, I know I have to use a web server to run PHP but how cool would it be to be able to preview PHP end result inside WB19?
:-)

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 2:09 pm
by zxspectrum2
lummis wrote: Thu Oct 17, 2024 11:42 am Looking at the code, I wonder if you can achieve your objective by using one of the Database/Tables extensions (seehttps://www.wysiwygwebbuilder.com/extensions.html) either paid or unpaid. With some you can insert your own MySQL query and you can rely upon the extension to deal with the connection to the database.
I did have a look at them but I couldn't see one that was able to list, and display images/URLs/ etc in list view?
thanks

Re: PHP inside HTML not running

Posted: Thu Oct 17, 2024 4:03 pm
by wwonderfull
zxspectrum2 wrote: Thu Oct 17, 2024 2:09 pm I did have a look at them but I couldn't see one that was able to list, and display images/URLs/ etc in list view?
thanks
Do you mean you need something like a dataviewer or a crud where you can see users data and their pictures along with url and other details that sort of thing?

Re: PHP inside HTML not running

Posted: Sat Oct 26, 2024 5:54 pm
by zxspectrum2
I found the issue I was having and can now see the data on screen - it was a database name typo!!!!
Thanks to all for your replies

PS It was a list of downloads which included an image on the same line, though that bit still needs work :-)