php - 将 mysql_query 转换为 $wpdb->get_results

标签 php mysql wordpress

在服务器上更新 php 后,我遇到了 mysql_query 的一些问题。您的代码中有 2 个函数在当前版本的 PHP 中已被弃用: mysql_fetch_array() mysql_query()

   global $table_prefix, $wpdb;
// caching database query per comment
$ck_cache = array('ck_ips'=>"", 'ck_comment_id'=>0, 'ck_rating_up'=>0, 'ck_rating_down'=>0); 

$table_name = $table_prefix . "comment_rating";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
{
    temckrating_install();
}

function temckrating_install() //Install the needed SQl entries.
{
   global $table_prefix, $wpdb;

   $table_name = $table_prefix . "comment_rating";

   $sql = 'DROP TABLE `' . $table_name . '`';  // drop the existing table
   mysql_query($sql);
   $sql = 'CREATE TABLE `' . $table_name . '` (' //Add table
      . ' `ck_comment_id` BIGINT(20) NOT NULL, '
      . ' `ck_ips` text NOT NULL, '
      . ' `ck_rating_up` INT,'
      . ' `ck_rating_down` INT'
      . ' )'
      . ' ENGINE = myisam;';
   mysql_query($sql);
   $sql = 'ALTER TABLE `' . $table_name . '` ADD INDEX (`ck_comment_id`);';  // add index
   mysql_query($sql);

  // echo "comment_rating tables created";

   $ck_result = mysql_query('SELECT comment_ID FROM ' . $table_prefix . 'comments'); //Put all IDs in our new table
   while($ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC)) //Wee loop
   {
      mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $ck_row['comment_ID'] . "', '', 0, 0)");
   }
}

function temckrating_comment_posted($ck_comment_id) //When comment posted this executes
{
   global $table_prefix, $wpdb;
   $table_name = $table_prefix . "comment_rating";
   mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $ck_comment_id . "', '" . getenv("REMOTE_ADDR") . "', 0, 0)"); //Adds the new comment ID into our made table, with the users IP
}

// cache DB results to prevent multiple access to DB
function temckrating_get_rating($comment_id)
{
   global $ck_cache, $table_prefix, $wpdb;

   // return it if the value is in the cache
   if ($comment_id == $ck_cache['ck_comment_id']) return;

   $table_name = $table_prefix . "comment_rating";
   $ck_sql = "SELECT ck_ips, ck_rating_up, ck_rating_down FROM `$table_name` WHERE ck_comment_id = $comment_id";
   $ck_result = mysql_query($ck_sql);

   $ck_cache['ck_comment_id'] = $comment_id;
   if(!$ck_result) { 
      $ck_cache['ck_ips'] = '';
      $ck_cache['ck_rating_up'] = 0;
      $ck_cache['ck_rating_down'] = 0;
      mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $comment_id . "', '', 0, 0)");
   }
   else if(!$ck_row = mysql_fetch_array($ck_result, MYSQL_ASSOC)) {
      $ck_cache['ck_ips'] = '';
      $ck_cache['ck_rating_up'] = 0;
      $ck_cache['ck_rating_down'] = 0;
      mysql_query("INSERT INTO $table_name (ck_comment_id, ck_ips, ck_rating_up, ck_rating_down) VALUES ('" . $comment_id . "', '', 0, 0)");
   }
   else {
      $ck_cache['ck_ips'] = $ck_row['ck_ips'];
      $ck_cache['ck_rating_up'] = $ck_row['ck_rating_up'];
      $ck_cache['ck_rating_down'] = $ck_row['ck_rating_down'];
   }
}

我尝试用 $wpdb->get_results 替换 mysql_query 但仍然出现错误

最佳答案

mysql_query 返回 资源。因此,您可以使用 $wpdb->get_results 得到此错误。

mysql_query() 在成功时返回 TRUE,在错误 时返回 FALSE。返回的结果资源应该传递给mysql_fetch_array()以及其他处理结果表的函数来访问返回的数据。

您将替换 $wpdb->get_results 的 2 个函数 mysql_fetch_array()mysql_query 实例。您将定义返回类型 - ARRAY_A、ARRAY_N、OBJECT、OBJECT_K

global $wpdb;

$ck_sql = $wpdb->get_results("SELECT ck_ips, ck_rating_up, ck_rating_down FROM `$table_name` WHERE ck_comment_id = $comment_id", ARRAY_A);
foreach($ck_sql as $row) {
    echo $row['ck_ips'] . ' ' . $row['ck_rating_up']. ' ' . $row['ck_rating_down '];
}

关于php - 将 mysql_query 转换为 $wpdb->get_results,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33937047/

相关文章:

php - Mysql根据点赞数从多个表中选择

PHP - 类似 Gmail 的电子邮件内容分离

javascript - 如果存在验证错误,则保持检查单选按钮无法正常工作

php - 如何检查 PHP 中是否存在 shell 命令

mysql - 通过批处理脚本创建mysql odbc连接

php - WooCommerce 自定义变体下拉列表

php - Strato 服务器 PHP Zend PDOException 套接字

mysql - SQL查询字符串以获取表中的下N条记录

mysql - 有没有办法查询 mysql 并且只提取数字结果?

mysql - Docker,无法将 WordPress 连接到 MySQL