admin管理员组

文章数量:1326129

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 I guess you're learning, but 1) You can get an almost immediate answer by copying and pasting the error message you have into a search engine, The first result for 'Warning: mysqli_query() expects at least 2 parameters, 1 given in' seems to be what you're looking for. 2) You need to read the docs for the tools you're using. At absolute minimum, for PHP you can read about all the functions by googling them or looking them up on php. If you look up mysqli_query you'll quickly get information which will help you here. – mozboz Commented Aug 11, 2020 at 22:43
  • 1 There's a missing quote on the password varables value, I'm surprised any of it runs. Also keep in mind that how to use 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 you echo. 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 use mysqli functions – Tom J Nowell Commented Aug 11, 2020 at 22:46
  • @Tom J Nowell Thanks - the " was just accidentally removed when I took out the real password. Where I am confused is that this worked outside of a shortcode without issues whatsoever. Because it works as PHP, wouldnt this be a WP releated thing? Given it worked in before without issues, what is the issue? Should I make the shortcode call a file instead that has all of this? – Bob Commented Aug 11, 2020 at 23:21
  • it sounds like the issue is that when you ran it standalone all the same warnings happened there too, but they weren't printed out to the browser so you were unawares. It's also possible that you created a new connection, but then didn't use it, so the queries executed on WP's database connection instead, and your code just assumes things work so when it failed you got PHP warnings instead. Eitherway I strongly recommend yoou do not use raw mysqli 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
  • @Tom that is absolutely possible. Ive switched now to the $wpdb functions but seem to be struggling there too, – Bob Commented Aug 12, 2020 at 17:44
Add a comment  | 

1 Answer 1

Reset to default 0

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

本文标签: functionsshortcodescustom php and their errors