473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
Jul 17 '05 #1
3 5203
Martin wrote:
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.


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

Jul 17 '05 #2
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
";

}

Jul 17 '05 #3
On Sun, 13 Feb 2005 22:24:25 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:
Martin wrote:
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.


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

Thanks.

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

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Finn | last post by:
OpenBSD 3.2 Apache 1.3.26 PHP 4.3.4 PHP-Nuke 6.9 getting these in the apache error log: Sun Nov 16 20:20:16 2003] PHP Notice: Undefined variable: HTTP_USER_AGENT in...
6
by: Will | last post by:
I downloaded a webcounter from http://www.math.sunysb.edu/~shafikov/computing/webcounter. It seems to be working but I have the following being printed to the page: Notice: Undefined variable:...
7
by: Coder Droid | last post by:
I decided to run some code with errors set to E_ALL, just to see what I would run across. It caught a few things, but 90% or better of the messages were of the 'undefined' kind: PHP Notice: ...
0
by: Dave | last post by:
Hi everyone, (I already posted this to the VS.NET IDE news group without any responses, so I'm attempting one more time in this group) The issue I'm having is occuring in the IDE of VS.NET...
10
by: Sharon | last post by:
Hi! Does anyone know why the onclick in the following popup menu gives the error:"Val is undefined"? Does it have something to do with the fact that it is called within the variable tablePop?...
4
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not...
49
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.