Connecting Tech Pros Worldwide Help | Site Map

Undefined variable error

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:30 AM
Martin
Guest
 
Posts: n/a
Default Undefined variable error

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, 11:30 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default 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, 11:30 AM
David Karn
Guest
 
Posts: n/a
Default 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, 11:30 AM
Martin
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.