0

I have the following problem.

This error persisting in accompanying me

Fatal error: Uncaught Error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\certificado\functions.php:49 Stack trace: #0 C:\xampp\htdocs\certificado\index.php(11): get_info_from_email('amanda_pandoka@...') #1 {main} thrown in C:\xampp\htdocs\certificado\functions.php on line 49

In the code below I can not understand the error. Can someone help me?

    function connect() {
    $socket = new PDO('mysql:host=' . @$host . ';dbname=' . @$nomedobancodedados,
        @$usuario, @$senha);
    $socket->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $socket;
}

function get_info_from_email($email) {

    if (!$email)
        return false;

    global $db;

    $sql = "
        SELECT
            id,
            name,
            email,
            type,
            data,
            file
        FROM
            attendee
        WHERE 1=1
            AND email = '{$email}'
    ";
    if ($info = $db->query($sql))
        return false;

    if ($info = $info->fetchAll())
        return false;

    return $info;

    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

2
if ($info = $db->query($sql))
    return false;

This says: if the result of $db->query($sql) can be stored in $info and is not something like false, null or an empty string or array, stop now and return false. So basically, if your query executes successfully and properly returns a PDOStatement with results in it, your function stops here.

if ($info = $info->fetchAll())
    return false;

This is where you're getting the error. The fact that this code is reached at all means that the query failed to execute (otherwise, it would have returned false earlier). So basically, you're calling fetchAll() on false. Try to see what the error is here (do a print_r($db->errorInfo()); before this if-statement)

By the way, this if-statement will also cause your function to return false if the fetchAll() call was successful, which is probably not what you want. Additionally, by using $db->query() directly with the email address provided in the function call, you're leaving your code open to possible SQL injection attacks. As a rule, never trust any variable if you don't have 100% control over what's in it. You should use a prepared statement instead.

As another rule, always use curly braces on code blocks (if/elseif/else, for/foreach/while/do, try/catch/finally), because then you don't need to think about them anymore if you someday decide that the code block should do two things instead of one, and it's easier to debug code if you can visually see exactly what your code is trying to do.

This code (not tested) should work the way you want:

function get_info_from_email($email) {
    if (!$email) {
        return false;
    }

    global $db;

    $stmt = $db->prepare("
        SELECT
            id,
            name,
            email,
            type,
            data,
            file
        FROM
            attendee
        WHERE 1=1
            AND email = :email
    ");

    // return false if the query cannot be executed
    if (!$stmt->execute(array(':email' => $email))) {
        return false;
    }

    // return false if there was an **error** retrieving the query results
    if (($info = $stmt->fetchAll()) === false) {
        return false;
    }

    return $info;
}
rickdenhaan
  • 10,857
  • 28
  • 37
  • I made a print_r($db->errorInfo()); and it with the code that you put returned the error Array ([0] => 00000 [1] => [2] =>) – Guilherme Moreira Apr 27 '17 at 19:20
  • That means there was no error. What does it return with your original code? – rickdenhaan Apr 27 '17 at 19:21
  • Returns that the email could not be found in the database! – Guilherme Moreira Apr 27 '17 at 19:23
  • OK, so does the new function return the correct results then? – rickdenhaan Apr 27 '17 at 19:27
  • Yes according to      Print_r ($ db-> errorInfo ()); It returns a result, it just does not conflict the database to generate a certificate with the extension GD – Guilherme Moreira Apr 27 '17 at 20:01
  • I'm sorry, I have no idea what you just said or what this function has to do with certificates or the GD extension. If you call `get_info_from_email()` with an email address that exists in your database, does it return the data from the `attendee` table? And does it return `false` if the email address does not exist? – rickdenhaan Apr 27 '17 at 22:08
  • It returns a message that was previously specified for emails that do not exist in the database. Even though the email exists, it returns that it does not exist. – Guilherme Moreira Apr 28 '17 at 12:51
  • Sounds like there is no column "email" in the "attendee" table. Can you copy/paste the exact error message? – rickdenhaan Apr 28 '17 at 13:35
  • Array ( [0] => 00000 [1] => [2] => ) Notice: Undefined variable: info in C:\xampp\htdocs\certificado\functions.php on line 76 – Guilherme Moreira Apr 28 '17 at 13:40
  • Line 76 on if (!$sql->execute(array($email => $info))) { return false; } – Guilherme Moreira Apr 28 '17 at 13:41
  • That's not how that array should look. It should be `array(':email' => $email)` -- if you use the function I posted with the modified query and a prepared statement. – rickdenhaan Apr 28 '17 at 13:46
  • Even with your code, continues the error on line 76 – Guilherme Moreira Apr 28 '17 at 13:51
  • You must have copy/pasted my code wrong. I just tried the exact code in my answer in a [test file](https://gist.github.com/rickdenhaan/6a32a360acc129f20dec023e5c996308), and it works without a problem. Can you update your question to include the code **exactly** as you have it now? – rickdenhaan Apr 28 '17 at 14:18
-1

Continue like this


 function get_info_from_email($email) {
if (!$email) {
    return false;
}

global $db;

$sql = $db->prepare("
    SELECT
        id,
        name,
        email,
        type,
        data,
        file
    FROM
        attendee
    WHERE 1=1
        AND email = :email
");
print_r($db->errorInfo());
// return false if the query cannot be executed
if (!$sql->execute(array(':email' => $info))) {
    return false;
}

// return false if there was an **error** retrieving the query results
if (($info = $sql->fetchAll()) === false) {
    return false;
}

return $info;

}