0

Hi there Im still trying to change to mysqli, and I just can get things to go right some times. The biggest thing I have is the mysqli_result, ive tried what other people have done, and doesnt seem to work. Here is the code below:

$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);
    }
}

Help would be appreciated.

Kind Regards Chad

Machavity
  • 28,730
  • 25
  • 78
  • 91
chad
  • 5
  • 2

3 Answers3

0

You cannot use mysql_result! Try to do it like this:

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

You can use object oriented style:

$Result = $Con->query("SELECT referer FROM users WHERE userId = '$key'");
if( $Result->num_rows ) {
    list($referer) = $Result->fetch_row();
P.W-S
  • 168
  • 8
0

If you're in the process of switching, you should go straight to PDO, not mysqli.

mysqli vs pdo - stackoverflow

Community
  • 1
  • 1
StringsOnFire
  • 2,446
  • 4
  • 21
  • 43
0

You can't mix mysql_ and mysqli_ functions like that. Also, mysql_result is serious old school. There is no equivalent in mysqli (and that's a good thing). I switched to mysqli_fetch_assoc, which takes your query and returns an associative array with the field names as keys. I kept it all procedural for the sake of uniformity (I hate mixing OOP with procedural). I should note that your code is horribly convoluted as written (for instance $key isn't defined anywhere). It's better to avoid reusing variable named like you have. I also HIGHLY recommend switching to an all-object codebase.

$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);
    }
}
Machavity
  • 28,730
  • 25
  • 78
  • 91
  • When I try that code, I get this error: Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home3/clfarmer/public_html/confirm.php on line 45; which is the line $newRefs = mysqli_fetch_assoc($result3); – chad Jan 25 '14 at 17:42
  • Added the missing `i` and another space that was causing that query to fail. Try now – Machavity Jan 25 '14 at 17:49