为了账号安全,请及时绑定邮箱和手机立即绑定

PHP调用MYSQL语句并获取输出

PHP调用MYSQL语句并获取输出

PHP
慕森卡 2023-09-22 16:50:52
在继续学习的过程中,我现在正在根据上面学到的内容编写不同的语句,但它没有给出任何结果。Workbench 显示查询语句良好,结果显示为“2”<?php$servername = "localhost";$username = "xxx";$password = "xxx";$dbname = "xxx";// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {  die("Connection failed: " . $conn->connect_error);}  $sql = "SELECT SUM(`mattiscool`),booking_date FROM `wp_cbxrbooking_log_manager` WHERE `booking_date` = CURDATE() -1";$result = mysqli_query($sql);while ($row = mysqli_fetch_assoc($result)){    echo $row['total'];}mysqli_close($con);?>我试图通过 php 调用 MySQL 语句并输出值。以下是我尝试过的。有人可以帮助我了解我做错了什么吗?<?php$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\',  \n"    . "              wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"    . "              wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"    . "              wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"    . "FROM wp_cbxrbooking_log_manager\n"    . "\n"    . "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY  \n"    . "ORDER BY `Time`  ASC";?>
查看完整描述

2 回答

?
天涯尽头无女友

TA贡献1831条经验 获得超9个赞

您的代码不完整,您必须建立与数据库的连接,然后执行查询才能获取输出。



查看完整回答
反对 回复 2023-09-22
?
达令说

TA贡献1821条经验 获得超6个赞

您需要执行查询并获取结果,如下所示:


<?php

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB";


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection

if ($conn->connect_error) {

  die("Connection failed: " . $conn->connect_error);

}


$sql = "SELECT DAYNAME(wp_cbxrbooking_log_manager.`booking_date`) as \'weekday\',  \n"


    . "              wp_cbxrbooking_log_manager.`party_size` as \'Party Size\',\n"


    . "              wp_cbxrbooking_log_manager.`booking_time` as \'Time\',\n"


    . "              wp_cbxrbooking_log_manager.`mattiscool` as \'# of Reservations\'\n"


    . "FROM wp_cbxrbooking_log_manager\n"


    . "\n"


    . "WHERE `booking_date` >= DATE(NOW()) - INTERVAL 7 DAY  \n"


    . "ORDER BY `Time`  ASC";


$result = $conn->query($sql);


if ($result->num_rows > 0) {

  // output data of each row

  while($row = $result->fetch_assoc()) {

    echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

  }

} else {

  echo "0 results";

}

$conn->close();

?>


查看完整回答
反对 回复 2023-09-22
  • 2 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信