无法弄清楚这个mysql到mysqli的变化

时间:2014-01-25 17:08:53

标签: php mysql mysqli

您好我还在尝试更改为mysqli,而且我可以让某些事情顺利进行。 我所拥有的最重要的是mysqli_result,我尝试过其他人做过的事情,但似乎没有用。 以下是代码:

$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if(mysql_result($result, 0) != "" ){
    $referer = mysql_result($result, 0);
    $result = mysqli_query($con, "SELECT referer FROM users WHERE userId = $referer'");
    if(mysql_result($result, 0) != "" ){
        $result2 = mysqli_query($con, "SELECT refered FROM users WHERE userId = $referer'");
        $newRefs = mysql_result($result2, 0) + 1;
        mysqli_query($con, "UPDATE users SET refered = '$newRefs' WHERE userId = '$referer'");
        $result3 = mysqli_query($con, "SELECT userName FROM users WHERE userId = '$key'");
        $refered = mysql_result($result3, 0);
    }
}

帮助将不胜感激。

亲切的问候 乍得

3 个答案:

答案 0 :(得分:0)

你不能使用mysql_result! 试着这样做:

$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if( mysqli_num_rows($result, 0) ) {
    list($referer) = mysqli_fetch_row($result);
....

您可以使用面向对象的样式:

$Result = $Con->query("SELECT referer FROM users WHERE userId = '$key'");
if( $Result->num_rows ) {
    list($referer) = $Result->fetch_row();

答案 1 :(得分:0)

如果你正在进行转换,你应该直接进入PDO,而不是mysqli。

mysqli vs pdo - stackoverflow

答案 2 :(得分:0)

您无法混合mysql_mysqli_这样的功能。此外,mysql_result是严肃的旧学校。 mysqli中没有等价物(这是一件好事)。我切换到mysqli_fetch_assoc,它接受​​您的查询并返回一个关联数组,字段名称为键。为了统一,我把它全部保持在程序上(我讨厌将OOP与程序混合)。我应该注意到你的代码在编写时非常复杂(例如$key并未在任何地方定义)。最好避免重复使用像你一样的变量。我还强烈建议切换到全对象代码库。

$result = mysqli_query($con, "SELECT referer FROM users WHERE userId = '$key'");
if($row = mysqli_fetch_assoc($result)){
    $result2 = mysqli_query($con, "SELECT referer FROM users WHERE userId = '" . $row['referer'] . "'");
    if($row2 = mysqli_fetch_assoc($result2)){
        $result3 = mysqli_query($con, "SELECT refered FROM users WHERE userId = '" . $row2['referer'] . "'");
        $newRefs = mysqli_fetch_assoc($result3);
        mysqli_query($con, "UPDATE users SET refered = '" . $newRefs['refered'] . "' WHERE userId = '" . $row['referer'] . "'");
        $result4 = mysqli_query($con, "SELECT userName FROM users WHERE userId = '$key'");
        $refered = mysqli_fetch_assoc($result4);
    }
}