UPDATE- After some suggestions I changed this over to using the $wpdb API. The following code is retuning a '1' instead of the actual result count which should be '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');
Original - Can someone please lead a man to learn how to fish on this one? I have a customshort codes file. I know that the file and its hook for the first code is right since it functions. I am trying to add a shortcode with an SQL query with a SUM. I know that the sql string is good since it has been validated. I am getting errors on degudding that I dont get. Previously I was using the same php code with a plug in but am now moving to shortcodes and not using the plugin.
code:
// 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');
errors:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 29
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 31
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 36
Can someone please tell me what I am doing wrong?
UPDATE- After some suggestions I changed this over to using the $wpdb API. The following code is retuning a '1' instead of the actual result count which should be '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');
Original - Can someone please lead a man to learn how to fish on this one? I have a customshort codes file. I know that the file and its hook for the first code is right since it functions. I am trying to add a shortcode with an SQL query with a SUM. I know that the sql string is good since it has been validated. I am getting errors on degudding that I dont get. Previously I was using the same php code with a plug in but am now moving to shortcodes and not using the plugin.
code:
// 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');
errors:
Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 29
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 31
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in /home/food/domains/xxx/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 36
Can someone please tell me what I am doing wrong?
Share Improve this question edited Aug 12, 2020 at 17:48 Bob asked Aug 11, 2020 at 22:37 BobBob 134 bronze badges 5 |1 Answer
Reset to default 0The 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');
mysqli
would be better asked on stackoverflow as it isn't a WP API or question. Also, shortcode functions don't echo their content, they return it as a string. You'll have your values being output in the wrong place if youecho
. I edited the code to fix the missing"
but that's only so people viiewing the question can read the code, it isn't a solution to your issue. Also, WP has a WPDB class, you don't have to usemysqli
functions – Tom J Nowell ♦ Commented Aug 11, 2020 at 22:46mysqli
functions, WP provides APIs for doing this. As an aside, have you tried just talking to CBXR restuarant manager plugin support? – Tom J Nowell ♦ Commented Aug 12, 2020 at 9:07