Connecting Tech Pros Worldwide Help | Site Map

Undefined variable error

  #1  
Old July 17th, 2005, 12:30 PM
Martin
Guest
 
Posts: n/a
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
  #2  
Old July 17th, 2005, 12:30 PM
Janwillem Borleffs
Guest
 
Posts: n/a

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



  #3  
Old July 17th, 2005, 12:30 PM
David Karn
Guest
 
Posts: n/a

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]
}

  #4  
Old July 17th, 2005, 12:30 PM
Martin
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined variable error Sharon answers 10 July 23rd, 2005 12:46 PM
Keep getting undefined variable error Chuck answers 10 July 20th, 2005 03:27 PM
Undefined variable error with new install of PHP-Nuke Dan Finn answers 3 July 17th, 2005 02:10 AM