0

Very new to using mysql, however, I'm trying to fix a bug in an old piece of code in a wordpress plugin - here is the original code:

        $sql = mysqli_query("SELECT count(`question_count`) as Qcount FROM `wp_posts` WHERE `question_count` = 1 and `question_date` = '".date("Y-m-d")."'") or die(mysql_error());  

        $no_of_questions = get_option( 'askme_setting_no_of_questions', 10 );
            if($row = mysql_fetch_array($sql)) {        
                $qry = $row['Qcount'];  
            }       
            if($qry >= $no_of_questions) {      
                $value = "The question limit for today has been reached";           
                $button = "disabled";   
            } else {        
                $value = "Add your question to the cart";           
                $button = " ";  
            }  

Which was giving the following error:

mysqli_query() expects at least 2 parameters, 1 given in

I have since changed the first line as follows to use Wordpress functions:

$sql = $wpdb->get_results( "SELECT count(`question_count`) as Qcount FROM `wp_posts` WHERE `question_count` = 1 and `question_date` = '".date("Y-m-d")."'" ); 

which now gives the following errors:

mysql_fetch_array() expects parameter 1 to be resource, array given in ...
Undefined variable: qry in ...

Is there something obvious that I am doing wrong here?

Sam Skirrow
  • 3,647
  • 15
  • 54
  • 101
  • Don't use `mysqli` for one. Use the [WordPress database layer WPDB](https://codex.wordpress.org/Class_Reference/wpdb) exclusively, don't mix and match. The real problem here is you're trying to use `mysqli` and instead are inadvertently using the crappy old `mysql_query` interface. – tadman Feb 21 '17 at 17:45
  • @tadman Totally agree with you - my aim is to replace the references to mysqli and the mysql_query interface with $wpdb (as you will see from my first change) - however, I don't know how to change the second part of my code to fit the first - if that makes sense – Sam Skirrow Feb 21 '17 at 17:48
  • You can't use `mysql_fetch_array` or anything like that. Forget those functions, they're not related and incompatible. Stick *exclusively* to the WPDB functions. In the documentation they refer to `get_row` for results, and so on. Use those. – tadman Feb 21 '17 at 17:50
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – aynber Feb 21 '17 at 18:00

3 Answers3

1

You're mixing things up.

mysql_ and mysqli_ are two completely different sets of functions in PHP. You can't send a query using the mysqli_ function, and manipulate the results with mysql_*.

Also, mysql_ functions were deprecated in later versions of PHP5, and removed altogether in PHP7.

Your best bet is to follow @tadman's advice and use WP's API for this.

Maxim
  • 52,561
  • 27
  • 155
  • 209
Lucas Krupinski
  • 682
  • 5
  • 16
  • I was going to write an answer, but this is basically what I was going to say. The only thing I'd add is that it looks like OP made a half-hearted attempt to convert from `mysql` to `mysqli`. It'd also be helpful to point out that the method signatures for `mysql_query` and `mysqli_query` are different (thus explaining the error message). – HPierce Feb 21 '17 at 17:54
1

You should make mysqli connection first and then use queries and fetch queries further. You can follow the below link to use mysqli fetch queries.

https://www.w3schools.com/php/func_mysqli_fetch_array.asp

Shashank Sharma
  • 585
  • 3
  • 14
0

Is the only line you changed the $sql = line?

From the wordpress codex:

global $wpdb;
$results = $wpdb->get_results( 'SELECT * FROM wp_options WHERE option_id = 1', OBJECT );

should return you an array or object of results per the documentation. so your code wouldn't need to do any of the fetch_assoc related methods. Wordpress is handling the actual DB connection and query parsing and just handing you back your results.

Possible Solution:

// formatting for readability.
$date = date("Y-m-d");
// Prepare query.
$sql = $wpdb->prepare(
  "SELECT count(`question_count`) as Qcount 
  FROM `wp_posts` 
  WHERE `question_count` = 1 
  AND `question_date` = %s",
  $date
);
// Execute query.
$results = $wpdb->get_results($sql);

// Get option for number of questions.
$no_of_questions = get_option( 'askme_setting_no_of_questions', 10); 

// Set base values to avoid usage of else condition.  
$value = "Add your question to the cart";
$button = " ";

// Check to ensure results returned.
if (!empty($results)) {
  // Evaluate result of query.
  if ($results[0]->Qcount >= $no_of_questions) {
    // Update values only if necessary.
    $value = "The question limit...";
    $button = "disabled";
  }
}
D Lowther
  • 1,609
  • 1
  • 9
  • 16