shortcodes, custom php and their errors

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.com/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.com/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.com/public_html/wp-content/themes/yummy/custom-shortcodes.php on line 36

Can someone please tell me what I am doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x