Php code issue
Autor: Errol W.
Visitado 849,
Seguidores 1,
Compartilhado 0
I am trying to get data from a database. Here is the code I am using from W3 school:
$servername = "localhost";
$username = "Dreamer";
$password = "Dream1";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT order_id, label, value FROM cartinvoice_addresses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["order_id"]. " - Name: " . $row["label"]. " " . $row["value"]. "
";
}
} else {
echo "0 results";
}
$conn->close();
?>
When I run the file, I get what's in the image file. I did go to page properties and convert to php format
Publicado em
Hello Errol,
The code you've posted is missing an opening PHP tag... the closing tag is present, but the opening one is absent.
So you need to add the following at the start/top of your code:
<?php
Kind regards,
Paul
Search the WebSite X5 Help Center
Just an advice :
Never use security information in a script where php and html is mixed.
Instead make a php file with the database information, and then make another php file where you include this file.
Doing it this way, you will not expose connection information/passwords if the php file with also html has an error.
If you make an error that does that the executed file is not treated as php, then unwanted information could be exposed.
I guess that is also what you intend - it was just to point it out
Autor
Hello Paul,
The opening tag you mention is in the code. I did not include it with my cut and paste. In any regard, I still have the problem.
John S, you absolutely correct on the security issue. That was a bone head mistake.
Autor
Hello All, Here is what I discovered, The reason I was getting the issue is only when using the preview mode. After updating the server I don't have that problem. Now I just have to sort out why I am getting 0 results. SQL is new to me.
That makes sense, Errol... WebSite X5 doesn't utilise PHP in Preview mode at all. Any PHP code in your projects will only run once uploaded to the server.
Autor
Hey Paul,
Back to my code issue. Here is what I am using.
<?php
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT order_id, label, value FROM cartinvoice_addresses";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["order_id"]. " - Name: " . $row["label"]. " " . $row["value"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
I know there are 72 records in the table cartinvoice_addresses. The code should simply print the selected columns, order_id, label, and value. But... I only get 0 results. I do notice that function num_rows does not have () after it. When I put that in, then I just get a blank page with no data. Please advise.
The first thing I notice is that you only have three parameters for the database connection:
$servername, $username, $password
There should be a fourth... the name of the database itself... e.g. $database_name
What is most likely happening is that your connection to the server is OK, but the query is failing, which would give you a Boolean 'false' value for $result.
Add the fourth parameter to the end of your database connection string and see how you get on from there.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database_name);
Autor
Hello Paul,
It working now. Thanks for your assistance.
You're welcome, Errol... and thanks for letting us know.