简体   繁体   中英

I get “undefined variable” error on my code after an apache (xampp) reinstall

This weekend I formatted and reinstalled my computer: today when I try to continue working on some php pages I've been programming last weeks, when I execute the php code on the localhost server I get an:

"Notice: Undefined variable: consulta in D:\\WEBS\\WP\\lbodas.php on line 42 1 de enero de 1970"

It's happening on all the php pages that perform an SQL query (wich worked perfectly before the reinstall).

I must be missing some configuration tweak (on php.ini, httpd.conf or some other apache configuration file), can somebody point me out what I'm missing?

Thanks in advance


Here is the code I'm using. To conect to the DDBB:

$bd_host='localhost';
$bd_login='MYLOGIN';  
$bd_clave='MYPASSWORD';
$bd_nombre='MYDATABASE';

connecttodb($bd_host,$bd_nombre,$bd_login,$bd_clave);
function connecttodb($bd_host,$bd_nombre,$dbuser,$bd_clave)
{
    global $conn;
    $conn=mysql_connect ("$bd_host","$dbuser","$bd_clave");
    mysql_query("SET NAMES 'utf8'");
    if(!$conn){die("CONNECTION ERROR");}
    //mysql_query("SET NAMES 'utf8'");

    mysql_select_db("$bd_nombre",$conn) or die ("CONNECTION ERROR".mysql_error());
}

And one of the sql queries that used to work but after the reinstall doesn't work:

        $sql = "SELECT * FROM cat_prov ORDER BY categoria ASC";
        $rs_result = mysql_query ($sql, $conn);

        while ($row = mysql_fetch_assoc($rs_result)) {
            $categoria=$row["categoria"];
            $notas=$row["notas"];
        }        

It's saying that the $row variable, wich holds the array with the results of the sql query, hasn't been defined... but two days ago, before the server OS reinstall I didn't have to define variables!!

Your new installation has error reporting set to E_ALL which is also displaying notices. Try

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

Source: PHP Manual

You're probably doing something like

$result = $consulta + 1;

without ever having assigned a value to $consulta before this. Anytime you use a variable in a "read" context, without having first assigned it, newer versions of PHP will throw that warning.

Doing

$consult = 0;
$result = $consulta + 1;

will solve the problem, becase you've defined a value for the variable BEFORE it was used in a read context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM