"Undefined variable" error

Asked

Viewed 2,981 times

2

I’m trying to run this code but it’s not working.

$sql    = "SELECT * FROM cliente WHERE id = $id ";
$limite = mysql_query("$sql");

while ($sql = mysql_fetch_array($limite)){

    $nome       = $sql["nome"];
    $telefone   = $sql["telefone"];

    $id         = $_GET['id'];

    echo "<div class='clientecelula'>" ;
    echo "$nome"; 
    echo "<span class='telcelula'> $telefone </span>";
    echo "<span class='semcelula'>  </span>";
    echo "</div>" ; 

}

I’m getting the bug:

Notice: Undefined variable: id in /Applications/XAMPP/xamppfiles/htdocs/Ross/semana.php on line 7

Warning: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in /Applications/XAMPP/xamppfiles/htdocs/Ross/semana.php on line 12

What’s the problem here?

  • I find this kind of question out of scope because it’s more about asking for help/support to solve something personal. I don’t think it’s up to the community to be doing the third party service..

  • 1

    @Danielomine requests for help with bugs in the code are within the scope.

  • 1

    I think the question is within the scope, I think you confuse things, it’s not because someone is lost with something basic that she is outside the scope

  • 1

    $_GET['id'] empty and caused an error in sql that returned a false. error-related question

  • @rray the problem is not that, the problem is the location of the $id.

  • I disagree. For me, this is out of scope. I don’t think this kind of issue brings community value. This is like doing the service of others, for free.

  • Thank you for your help! I would like to apologize for asking such a basic question and that much was said that was out of scope. I’m still a student and I’m only php for 2 months.

Show 2 more comments

2 answers

2

Your problem is in the variable $id that is assigned after it is already being used in SQL.

If you change places, you’ll figure it out that problem:

$id     = $_GET['id'];
$sql    = "SELECT * FROM cliente WHERE id = $id ";
$limite = mysql_query($sql);

while ($sql = mysql_fetch_array($limite))
{
    $nome       = $sql["nome"];
    $telefone   = $sql["telefone"];

    echo "<div class='clientecelula'>" ;
    echo "$nome"; 
    echo "<span class='telcelula'> $telefone </span>";
    echo "<span class='semcelula'>  </span>";
    echo "</div>" ; 

}

IMPORTANT: I advise the use of functions of type mysqli_ because the functions of the type mysql_ will be discontinued as you can see in this question.

1

Notice: Undefined variable

says the variable does not exist and in the code displayed in the question $id is defined after the query has been executed and you need it before that, as it will be used as an argument in SQL be sure to check if its value is valid, force a cast for whole.

Prefer mysql_query($sql) or die(mysqsl_error()) in place of mysql_query($sql); the first way will display the database error message if the query fails that can be from a syntax error to some other more serious problem.

>>#a última linha do while deveria estar aqui<<

$sql    = "SELECT * FROM cliente WHERE id = $id ";
$limite = mysql_query("$sql");

while ($sql = mysql_fetch_array($limite)){
    $nome       = $sql["nome"];
    $telefone   = $sql["telefone"];
    $id         = $_GET['id'];    <----- essa linha deveria estar antes da atribuição de $sql
}

The corrected code looks like this:

$id = (isset($_GET['id'])) ? $_GET['id'] : 0;

$sql    = "SELECT * FROM cliente WHERE id = $id ";
$limite = mysql_query($sql) or die(mysql_error());

while ($sql = mysql_fetch_array($limite)){
    $nome       = $sql["nome"];
    $telefone   = $sql["telefone"];
    $id         = $sql['id'];
}

If the code is from a new project it is highly recommended to use mysqli or PDO. Recommended reading:

Why should we not use mysql type functions_*?

Mysqli vs PDO - which is the most recommended to use?

  • Tchi already had the answer ready, I will answer anyway

  • By the way the while doesn’t close there...

  • @Jorgeb. vc says to remove this line $id = $sql['id']; or place the Sections ?

  • Set up...

  • And that line isn’t in the AP code either

Browser other questions tagged

You are not signed in. Login or sign up in order to post.