-1

In this piece of code, I am attempting to extract the email_address. For some reason, the code, that I expect to work does not yield the value of the email_address.

   // GET THE EMAIL ADDRESS FROM THE ACTIVATION CODE
    $sql = "SELECT email_address FROM userTable WHERE activate_code = '$safe_activate_code' LIMIT 1";
    if (!$res = $mysqli->query($sql))
    {
        $err
        = "QUERY FAIL: "
        . $sql
        . ' ERRNO: '
        . $mysqli->errno
        . ' ERROR: '
        . $mysqli->error
        ;
        trigger_error($err, E_USER_ERROR);
    }
    if ( $res->num_rows == 0 ) die("SORRY - YOUR ACTIVATION CODE WAS NOT FOUND");

    // SET UP THE EMAIL ADDRESS HINT - billy@gmail.com HINTS bill? gmail com
    echo "<br />num_rows = $res->num_rows" . PHP_EOL;
    $row = mysql_fetch_assoc($res);
    echo "<br />email_address = " . $row["email_address"] . PHP_EOL;

The result of the final num_rows is 1; But, the value for the email address is an empty string.

Why does $row["email_address"] not give me the email_address, which I know is not empty by inspecting the table for the activation code stored in the table for that email address.

John Wooten
  • 685
  • 1
  • 6
  • 21
  • Possibly, just didn't see the statement that produced the mix. Accepted atymic's answer below. Helpful. – John Wooten Jul 30 '19 at 20:25
  • You should still change your queries to use prepared statements, because you are vulnerable to SQL injection, and if you use input which contain certain characters you could break your program. Prepared statements would solve that problem completely. – Dharman Jul 30 '19 at 21:24

1 Answers1

1

You are using the mysql_fetch_assoc function to try and fetch the rows, however the rest of your code is using mysqli_* functions.

You can fix this by switching it out to correctly fetch the row from the result, rather than using the mysql_ function:

// code above
if ( $res->num_rows == 0 ) die("SORRY - YOUR ACTIVATION CODE WAS NOT FOUND");

// SET UP THE EMAIL ADDRESS HINT - billy@gmail.com HINTS bill? gmail com
echo "<br />num_rows = $res->num_rows" . PHP_EOL;
$row = $res->fetch_assoc();
echo "<br />email_address = " . $row["email_address"] . PHP_EOL;
atymic
  • 3,093
  • 1
  • 13
  • 26