Connecting Tech Pros Worldwide Help | Site Map

Undefined variable error

Martin
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm getting
PHP Notice: Undefined variable: db_list in C:\mypage.php on line xx

when I use the following code:

<?php
$conn = @mysql_connect( "localhost", "myname", "mypassword" )
or die( "Sorry - could not connect to MySQL" );

$rs= @mysql_list_dbs( $conn )
or die( "Sorry - could not list databases" );

for( $row=0; $row < mysql_num_rows($rs); $row++ )
{
$db_list .= mysql_tablename( $rs, $row ) . "<br>";

}
?>


This is copied straight out of a book I'm using to try to learn
about PHP and MySQL. Can someone tell me what's causing the
Undefined variable error? I didn't know that variables had to be
defined.

Using: PHP 5.0.1 MySQL 4.1.9
Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Undefined variable error


Martin wrote:[color=blue]
> This is copied straight out of a book I'm using to try to learn
> about PHP and MySQL. Can someone tell me what's causing the
> Undefined variable error? I didn't know that variables had to be
> defined.
>[/color]

That's because you are concatenating the output from the function call with
$db_list without it's initialized.

Just initialize $db_list before you are using it and it'll work fine:

$db_list = '';
for ($row=0; $row < mysql_num_rows($rs); $row++) {
$db_list .= mysql_tablename( $rs, $row ) . "<br>";
}

BTW, PHP will create variables on the fly, but throws notices when the error
reporting level is high enough. There are ways of suppressing these errors
without adjusting the error reporting level, but it's good programming
practice to define the variables you're going to use.


JW



David Karn
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Undefined variable error


You can also put the initialization right into the for loop, like this:


for ($row=0, $db_list = ''; $row < mysql_num_rows($rs); $row++) {
$db_list .= mysql_tablename( $rs, $row ) . "<br[color=blue]
>";[/color]
}

Martin
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Undefined variable error


On Sun, 13 Feb 2005 22:24:25 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
[color=blue]
>Martin wrote:[color=green]
>> This is copied straight out of a book I'm using to try to learn
>> about PHP and MySQL. Can someone tell me what's causing the
>> Undefined variable error? I didn't know that variables had to be
>> defined.
>>[/color]
>
>That's because you are concatenating the output from the function call with
>$db_list without it's initialized.
>
>Just initialize $db_list before you are using it and it'll work fine:
>
>$db_list = '';
>for ($row=0; $row < mysql_num_rows($rs); $row++) {
> $db_list .= mysql_tablename( $rs, $row ) . "<br>";
>}
>
>BTW, PHP will create variables on the fly, but throws notices when the error
>reporting level is high enough. There are ways of suppressing these errors
>without adjusting the error reporting level, but it's good programming
>practice to define the variables you're going to use.
>
>
>JW
>[/color]
Thanks.

One would think that the book's author should have known to do that.

Closed Thread