PHP脚本不返回任何结果

时间:2016-06-11 05:30:31

标签: php sql

我有一个脚本,我尝试从我的数据库中获取date

脚本需要显示:数据库中的日期是(日期)。点击此处继续。当我在phpMyAdmin中运行SQL查询时,SQL查询返回日期。当我在我的脚本中运行它时,我得不到任何结果。

这是我的剧本:

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

当我运行此脚本时,我得到0。当我用echo "0";更改echo " " . $row['date'] . ". ";时,我会看到一个空页。

我做错了什么?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

您正在使用mysqli_query(它只执行一个查询),因此您的脚本只能访问set变量语句并以分号结束。

遵循以下建议 - 第二个示例显示使用multi_query并像设置phpMyAdmin一样设置变量值。

尝试:

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

或使用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";
  }
?>

答案 1 :(得分:2)

我还有另一个未经批准的编辑...这有用吗?

只需将您的查询/变量分成两个独立的 - 只担心第一个值可能不会持续存在。

像这样:

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

答案 2 :(得分:1)

你已经格式化了sql脚本..所以不要写$ row [&#39; date&#39;]就像这样$ row [0]

希望这对你有用..