更新-在提出一些建议后,我将其改为使用$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行
有人能告诉我我做错了什么吗?