快捷码、自定义php及其错误

时间:2020-08-11 作者:Bob

更新-在提出一些建议后,我将其改为使用$wpdb API。以下代码正在重新调整“1”,而不是实际结果计数,实际结果计数应为“2”。

//a function is a block of code we can call everything between { } is the functions code. The return is the output.
function reservations_yesterday() {
global $wpdb;

              //query DB… get_var should get the output from the db and store it in the results variable. 
$results = $wpdb->get_var ("SELECT SUM(`mattiscool`), booking_date FROM `wp_cbxrbooking_log_manager` WHERE `booking_date` = CURDATE() -1");

//return the results, sends the output back to the code that calls the function.
return $results;
}


// this is a function, and you that calls the reservations_yesterday functions, and you are assigning the shortcode tag of res-1
add_shortcode(\'res-1\', \'reservations_yesterday\');
原创-有人能带领一个人学习如何在这条船上钓鱼吗?我有一个customshort代码文件。我知道第一个代码的文件及其挂钩是正确的,因为它可以正常工作。我正在尝试添加一个带有SUM的SQL查询的短代码。我知道sql字符串是好的,因为它已经过验证。我在degudding上遇到了一些我没有遇到的错误。以前,我在插件中使用相同的php代码,但现在改为使用短代码,而不使用插件。

代码:

// how many reservations yesterday
function res_yesterday(){

$servername = "localhost";
$username = "site";
$password = "pass";
$dbname = "site";

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

$sql = "SELECT SUM(`mattiscool`) ,booking_date as \\\'total\\\' 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);


}
add_shortcode(\'res_1\', \'res_yesterday\');
错误:

警告:mysqli\\u query()需要至少2个参数,1个在/home/food/domains/xxx中给定。com/public\\u html/wp-content/themes/yummy/custom-shortcode。php第29行

警告:mysqli\\u fetch\\u assoc()要求参数1为mysqli\\u result,在/home/food/domains/xxx中为空。com/public\\u html/wp-content/themes/yummy/custom-shortcode。php第31行

警告:mysqli\\u close()要求参数1为mysqli,在/home/food/domains/xxx中为null。com/public\\u html/wp-content/themes/yummy/custom-shortcode。php第36行

有人能告诉我我做错了什么吗?

1 个回复
最合适的回答,由SO网友:brett 整理而成

The answer is:

function reservations_yesterday() {
global $wpdb;


$results = $wpdb->get_var ("SELECT SUM(`mattiscool`), booking_date FROM `wp_cbxrbooking_log_manager` WHERE `booking_date` = CURDATE() -1");

return $results;
}



add_shortcode(\'name\', \'reservations_yesterday\');

相关推荐

限制只在主页上发布帖子-理想情况下是在unctions.php

我想限制我的主页上显示的帖子数量为3篇,但其他所有的归档、分类、分页页面等都是我在;“阅读”;Wordpress仪表板中的设置。是否有一种很好的方法可以在函数中实现这一点。php?我尝试了以下方法,将主页上的帖子限制为3篇,但它也会创建到每个页面都有3篇帖子的分页链接。 add_action( \'pre_get_posts\', \'change_posts_number_home_page\' ); function change_posts_number_home_page( $q