Help w/ code

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
salgonza
Forum Newbie
Posts: 3
Joined: Thu Aug 21, 2003 1:07 pm

Help w/ code

Post by salgonza »

<?

$db_name ="testDB";
$table_name ="my_contact";

//connect to server and select database
$connection = @mysql_connect("localhost","spike","9sj7En4") or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());

//build and issue query
$sql ="SELECT id, f_name, l_name FROM $table_name ORDER BY l_name";
$result = @mysql_query($sql,$connection) or die(mysql_error());

//check the number of results
$num = @mysql_num_rows($result);

if ($num < 1) {
//if there are no results,display message
$display_block = "<P><em>Sorry!No results.</em></p>";

} else {

//if results are found,loop through them
//and make a form selection block
while ($row =mysql_fetch_array($result)){
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .="<option value=''$id''>$l_name,$f_name</option>";

}

//create the entire form block
$display_block ="
<FORM METHOD=\"POST\" ACTION=\"show_modcontact.php\">
<P><strong>Contact:</strong>
<select name=\"id\">
$option_block
</select>

<INPUT TYPE=\"SUBMIT\" NAME=\"submit\" VALUE=\"Select this Contact\"></input></P>
</form>";

}
?>


That is my code. however when i run the page, i get an error message for line 29.

Apache 2.0.47 (win)
MySQL 4.0.14b
PHP Version 4.3.2

Could it possibly be a conflict in versions of software that i am using?
or if not, can you please help w/ my code.

Thanks in advance.
salgonza
Forum Newbie
Posts: 3
Joined: Thu Aug 21, 2003 1:07 pm

Post by salgonza »

Notice: Undefined variable: option_block in C:\Program Files\Apache Group\Apache2\htdocs\pick_modcontact.php on line 29

That is the error i get, sorry forgot to put it in the original post.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

line 29

Post by phpScott »

what is line 29 and what error do you get.

phpScott

beat me to it
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

declare

Post by phpScott »

before you start doing
$optionBlock.="";

you should just assign a value to $optionBlock like
$optionBlock="";

PHP has know idea what the orginal value of optionBlock is before you start trying to concatinate things to it and this caused the problem.

phpScott
salgonza
Forum Newbie
Posts: 3
Joined: Thu Aug 21, 2003 1:07 pm

Still not working! Argh

Post by salgonza »

but when i do that it doesn't show all the names in the database.

This is line 29

Code: Select all

<?php
$option_block .="<option value=''$id''>$l_name,$f_name</option>"; 

?>
It still works w/ the error, so i am just thinking about hiding the error.
but i want to fix it.
Post Reply