John John - 5 months ago 7
PHP Question

PHP script returns no result

I have a script where I try to get the

date
from my database.

The script needs to show: The date in the database is (date). Click here to continue. When I run the SQL query in phpMyAdmin, the SQL query returns the date. When I run it in my script I get no result.

Here is my script:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "set lc_time_names = 'nl_NL';";
$sql = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
$result = $conn->multi_query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "The date in the database is:";
echo " " . $row['date'] . ". ";
echo "Click here to continue.";
}
} else {
echo "0";
}
?>


When I run this script I get
0
. When I change
echo "0";
with
echo " " . $row['date'] . ". ";
I get a empty page.

What am I doing wrong? How can I fix this?

Answer

I had another edit that isn't approved...does this work?

Just splitting your queries/variables into 2 separate ones--only only worry that first value may not persist for you.

Like so:

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "";
 $dbname = "db";

 // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
 // Check connection
  if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
 }  

$sql = "set lc_time_names = 'nl_NL'";
$sql2 = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE            id='1'";
 $resultZ = $conn->query($sql);
 $result = $conn->query($sql2);
 if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
   echo "The date in the database is:";
    echo " " . $row['date'] . ". ";
    echo "Click here to continue.";
   }
   } else {
    echo "0";
   }
    ?>