Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PHP/MySQL查询转换为wpdb_Php_Mysql_Wordpress - Fatal编程技术网

将PHP/MySQL查询转换为wpdb

将PHP/MySQL查询转换为wpdb,php,mysql,wordpress,Php,Mysql,Wordpress,我一直在WordPress网站上使用以下代码,没有遇到很多问题(使用扩展代码段和短代码),直到几天前它停止工作 <?php //function to add an ordinal value to the number function showOrdinal($number) { // first convert to string if needed $num = (string) $number; // now we grab the last digit

我一直在WordPress网站上使用以下代码,没有遇到很多问题(使用扩展代码段和短代码),直到几天前它停止工作

<?php

//function to add an ordinal value to the number
function showOrdinal($number)
{
    // first convert to string if needed
    $num = (string) $number;
    // now we grab the last digit of the number
    $last_digit = substr($number, -1, 1);
    // if the string is more than 2 chars long, we get
    // the second to last character to evaluate
    if (strlen($number)>1)
    {
        $next_to_last = substr($num, -2, 1);
    }
    else
    {
        $next_to_last = "";
    }
    // now iterate through possibilities in a switch
    switch($last_digit)
    {
        case "1":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="st";
            }
            break;
        case "2":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="nd";
            }
            break;
        // if last digit is a 3
        case "3":
            // testing the second from last digit here
            switch($next_to_last)
            {
                case "1":
                    $number.="th";
                    break;
                default:
                    $number.="rd";
            }
            break;
        // for all the other numbers we use "th"
        default:
            $number.="th";
            break;
    } 

    // finally, return our string with it's new suffix
    return $number;
}

//assign the week number
$weekno = 1;

//build query
$sql = "SELECT rank_number, player_id, name, score ";
$sql .= "FROM ( ";
$sql .= "   SELECT @rank:=@rank+1 AS rank_number, player_id, name, score ";
$sql .= "   FROM ( ";
$sql .= "       SELECT player_id, name, en.score AS score ";
$sql .= "       FROM users ";
$sql .= "       JOIN entries en ON player_id = en.player_id";
$sql .= "       WHERE score != 0 ";
//here we only want score pulled out for that particular weeks game
if ($weekno > 0){
    $sql .= "AND en.week_no = " . $weekno . " ";
}
$sql .= "       GROUP BY player_id ";
$sql .= "       ORDER BY score DESC";
$sql .= "   ) AS rankings, (SELECT @rank:=0) AS r ";
$sql .= ") AS overall_rankings ";
$sql .= "LIMIT 0, 100; ";

// Perform Query
$result = mysql_query($sql);

//Shows some user details and the current top 100 ranks
while ($row = mysql_fetch_assoc($result)) {

    echo '<div class="one-fifth column  column_our_team" style="float: left;" >';
    echo '<div style="border: 1px solid #d4e1ea; background: #fff; padding: 20px; height: 250px; border-radius: 10px; box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.08);">';


    echo '<div class="team team_circle ">';
    echo '<h2 class="title" style="text-align:center;"><strong>'.  showOrdinal($row['rank_number']) . '</strong></h2>' ;
    echo '<hr class="hr_color">';
    echo '<div class="image_frame no_link scale-with-grid">';
    echo '<div class="image_wrapper">';
    global $userdata; get_currentuserinfo(); echo get_avatar( $userdata=($row['player_id']), 100); 
    echo '</div>';
    echo '</div>';
    echo '<div class="desc_wrapper">';
    echo '<h4>'.  $row['name'] . '</h4>';
    echo '<hr class="hr_color">';
    echo '<h4><strong> Score: ' .  $row['score'] . '</strong></h4>';
    echo '</div>';
    echo '</div>';
    echo '</div>';

    echo '</div>';
  }
//free the result
mysql_free_result($result);
?>
我决定创建一个WP函数可能是一个好主意,因为我在一些地方使用这个脚本(输出略有变化)。但是,我在尝试将PHP构建的查询转换为与wpdb兼容的查询时遇到了麻烦

    //build query
$sql = "SELECT rank_number, player_id, name, score ";
$sql .= "FROM ( ";
$sql .= "   SELECT @rank:=@rank+1 AS rank_number, player_id, name, score ";
$sql .= "   FROM ( ";
$sql .= "       SELECT player_id, name, en.score AS score ";
$sql .= "       FROM users ";
$sql .= "       JOIN entries en ON player_id = en.player_id";
$sql .= "       WHERE score != 0 ";
//here we only want score pulled out for that particular weeks game
if ($weekno > 0){
    $sql .= "AND en.week_no = " . $weekno . " ";
}
$sql .= "       GROUP BY player_id ";
$sql .= "       ORDER BY score DESC";
$sql .= "   ) AS rankings, (SELECT @rank:=0) AS r ";
$sql .= ") AS overall_rankings ";
$sql .= "LIMIT 0, 100; ";

// Perform Query
$result = mysql_query($sql);

非常感谢您的指导

什么麻烦?实际上,您应该能够使用$wpdb->get_results($sql)代替mysql_query($sql)。如果您有错误,请将其包含在您的问题箱中,以用于回答刻度盘b,用于回答刻度盘b的刻度盘。这正是我的想法和做法,但没有奏效。使用if(!mysql_select_db(“db_name”){echo”无法连接时出错:“.mysql_error();exit;}Output=拒绝访问用户“root”@“localhost”(使用密码:否)。但正如我上面所说的,我知道我可以使用另一个代码片段访问
    //build query
$sql = "SELECT rank_number, player_id, name, score ";
$sql .= "FROM ( ";
$sql .= "   SELECT @rank:=@rank+1 AS rank_number, player_id, name, score ";
$sql .= "   FROM ( ";
$sql .= "       SELECT player_id, name, en.score AS score ";
$sql .= "       FROM users ";
$sql .= "       JOIN entries en ON player_id = en.player_id";
$sql .= "       WHERE score != 0 ";
//here we only want score pulled out for that particular weeks game
if ($weekno > 0){
    $sql .= "AND en.week_no = " . $weekno . " ";
}
$sql .= "       GROUP BY player_id ";
$sql .= "       ORDER BY score DESC";
$sql .= "   ) AS rankings, (SELECT @rank:=0) AS r ";
$sql .= ") AS overall_rankings ";
$sql .= "LIMIT 0, 100; ";

// Perform Query
$result = mysql_query($sql);