转换mqsql_query已弃用

时间:2016-01-09 06:51:40

标签: php mysql

我已经意识到mysql_query已被弃用。 我需要做些什么来解决这个问题? 所有这些都是MySQL。

简单的事实是,由于功能已被弃用,我需要重新编写整个网站才能使用。

if (!$_GET['XID']) {
    $mystats = array();
    $s       = array();
    $mystats = mysql_query ("SELECT * FROM `members_personal` WHERE playerid = '{$_SESSION['playerid']}'");
    $s       = mysql_fetch_array($mystats);
    echo '<center><main>Personal stats</main>
      <hr width="93%">
           <table width = "75%" class = "rounded"><tr><td width=45% valign=top>
              ' . round_table('700') . '<table class = "vnm-table" width=100% cellspacing=1 cellpadding=3>
              <tr bgcolor=#999999><th align="left" width=70%>
              <b>Attacking</b>
              </th><th width=30%></th></tr>
              <tr bgcolor=#DFDFDF>
              <td>Attacks won:</td>
              <td>' . number_format($s['my_attwon']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Attacks lost:</td>
              <td>' . number_format($s['my_attlost']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Attacks stalemated:</td>
              <td>' . number_format($s['my_attstale']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Defends won:</td>
              <td>' . number_format($s['my_defwon']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Defends lost:</td>
              <td>' . number_format($s['my_deflost']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Defends stalemated:</td>
              <td>' . number_format($s['my_defstale']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Win/Loss ratio:</td>
              <td>';

    $attwon        = array();
    $attlost       = array();
    $first_number  = array();
    $second_number = array();
    $div_value     = array();
    $attwon        = $s['my_attwon'] + $s['my_defwon'];
    $attlost       = $s['my_attlost'] + $s['my_deflost'];

    $div_value = "0.0";
    if ($attlost == 0) {
        $attlost = '1';
    }
    $div_value = $attwon / $attlost;

    printf("%01.2f", $div_value);

    echo '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Times ran away:</td>
              <td>' . number_format($s['my_timesran']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Foes ran away:</td>
              <td>' . number_format($s['my_theyran']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Best kill streak:</td>
              <td>' . number_format($s['my_beststreak']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Current kill streak:</td>
              <td>' . number_format($s['my_currstreak']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Hits:</td>
              <td>' . number_format($s['my_hits']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Misses:</td>
              <td>' . number_format($s['my_misses']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Total damage made:</td>
              <td>' . number_format($s['my_totaldamage']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Best damage made:</td>
              <td>' . number_format($s['my_bestdamage']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>One hit kills:</td>
              <td>' . number_format($s['my_onehitkill']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Critical hits:</td>
              <td>' . number_format($s['my_crithits']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Rounds fired:</td>
              <td>' . number_format($s['my_rounds']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Stealth attacks:</td>
              <td>' . number_format($s['my_stealths']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Money mugged:</font></td>
              <td>$' . number_format($s['my_muggain']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Highest level beaten:</td>
              <td>' . number_format($s['my_bestlevel']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Total respect gained:</td>
              <td>' . number_format($s['my_respectgain']) . '</td>
              </tr>
           </table>

3 个答案:

答案 0 :(得分:2)

正如user3284463建议的那样,mysqli_*函数可以轻松更新旧代码而无需完全重写代码。他们通常会替换z()同行。

那就是说,如果你有时间,我会接受rehmat的建议并学习PDO。

答案 1 :(得分:1)

我建议PDO安全地处理数据库。要从旧代码迁移到PDO,首先要重写这样的数据库连接代码:

$dbhost = '';
$dbname = '';
$dbuser = '';
$dbpass = '';
$pdo = new PDO("mysql:host=".$dbhost.";dbname=".$dbname . ';charset=utf8', $dbuser, $dbpass);

然后重写代码的前几行将使其安全并与最新的PHP兼容:

<?php
    // just rewrite your query
    $query = $pdo->prepare("SELECT * FROM `members_personal` WHERE playerid = ?");
    $query->bindValue(1, {$_SESSION['playerid']}); // bind playerid securely
    $query->execute(); // execute your query
    $s = $query->fetchAll(); // fetch details from database as array
    // use the same code
    echo '<center><main>Personal stats</main>
      <hr width="93%">
           <table width = "75%" class = "rounded"><tr><td width=45% valign=top>
              ' . round_table('700') . '<table class = "vnm-table" width=100% cellspacing=1 cellpadding=3>
              <tr bgcolor=#999999><th align="left" width=70%>
              <b>Attacking</b>
              </th><th width=30%></th></tr>
              <tr bgcolor=#DFDFDF>
              <td>Attacks won:</td>
              <td>' . number_format($s['my_attwon']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Attacks lost:</td>
              <td>' . number_format($s['my_attlost']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Attacks stalemated:</td>
              <td>' . number_format($s['my_attstale']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Defends won:</td>
              <td>' . number_format($s['my_defwon']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Defends lost:</td>
              <td>' . number_format($s['my_deflost']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Defends stalemated:</td>
              <td>' . number_format($s['my_defstale']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Win/Loss ratio:</td>
              <td>';

    $attwon        = array();
    $attlost       = array();
    $first_number  = array();
    $second_number = array();
    $div_value     = array();
    $attwon        = $s['my_attwon'] + $s['my_defwon'];
    $attlost       = $s['my_attlost'] + $s['my_deflost'];

    $div_value = "0.0";
    if ($attlost == 0) {
        $attlost = '1';
    }
    $div_value = $attwon / $attlost;

    printf("%01.2f", $div_value);

    echo '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Times ran away:</td>
              <td>' . number_format($s['my_timesran']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Foes ran away:</td>
              <td>' . number_format($s['my_theyran']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Best kill streak:</td>
              <td>' . number_format($s['my_beststreak']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Current kill streak:</td>
              <td>' . number_format($s['my_currstreak']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Hits:</td>
              <td>' . number_format($s['my_hits']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Misses:</td>
              <td>' . number_format($s['my_misses']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Total damage made:</td>
              <td>' . number_format($s['my_totaldamage']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Best damage made:</td>
              <td>' . number_format($s['my_bestdamage']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>One hit kills:</td>
              <td>' . number_format($s['my_onehitkill']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Critical hits:</td>
              <td>' . number_format($s['my_crithits']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Rounds fired:</td>
              <td>' . number_format($s['my_rounds']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Stealth attacks:</td>
              <td>' . number_format($s['my_stealths']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Money mugged:</font></td>
              <td>$' . number_format($s['my_muggain']) . '</td>
              </tr><tr bgcolor=#DFDFDF>
              <td>Highest level beaten:</td>
              <td>' . number_format($s['my_bestlevel']) . '</td>
              </tr><tr bgcolor=#CCCCCC>
              <td>Total respect gained:</td>
              <td>' . number_format($s['my_respectgain']) . '</td>
              </tr>
           </table>

答案 2 :(得分:1)

你可以这样做:

$sql="SELECT * FROM `members_personal` WHERE playerid = '{$_SESSION['playerid']}'";
$mystats = mysqli_query($connection,$sql);
$row = mysqli_fetch_array($mystats,MYSQLI_ASSOC);