2

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?

14
  • PHP's mysql adapter all only process the first statement in the sql argument handed over. That has security reasons to prevent sql injection attacks. That means your set statement is executed, the SELECT statement is silently dropped. Commented Jun 11, 2016 at 5:40
  • Ok, is there a way to run this script with: set lc_time_names = 'nl_NL';? Commented Jun 11, 2016 at 5:54
  • Run it as two separate statements in sequence. The mysql connection has a state. Commented Jun 11, 2016 at 5:55
  • How can I do that? Commented Jun 11, 2016 at 5:56
  • 1
    Ah, and obviously those two assignments to the same variable $sql do not make any sense. The second string replaces the first one. Commented Jun 11, 2016 at 6:14

3 Answers 3

2

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";
   }
    ?>
Sign up to request clarification or add additional context in comments.

Comments

2

You are using mysqli_query (which executes exactly ONE query) and thus your script is only ever getting to the set variable statement and ending at the semicolon.

Follow the advice below--the 2nd example shows using multi_query and setting the variable value just like you do in phpMyAdmin.

Try with:

<?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 = "SELECT date_format(date, '%e %M %Y') AS date FROM table WHERE id='1'";
 $result = $conn->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";
  }
?>

Or use multi_query:

<?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'; 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";
  }
?>

4 Comments

Its working, but I need set lc_time_names = 'nl_NL';
It is not working again when I use $result = $conn->multi_query($sql);
@Fernando. Multi query gives me the same result
@Fernando Rodriquez no problem, I am glad you helped him, because I am no longer at the computer and I couldn't update my answer. Also, please take my upvote
1

You have formatted the sql script.. So instead of writing $row['date'] just put like this $row[0]

Hope this works for you..

1 Comment

FYI this would also work--but he is perfectly capable of calling the data columns by name rather than index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.