Current location: Hot Scripts Forums » Programming Languages » PHP » Notice: Undefined variable: row


Notice: Undefined variable: row

Reply
  #1 (permalink)  
Old 05-11-05, 07:45 PM
jozin jozin is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Notice: Undefined variable: row

Hi guys after a few adjustments with my query form
I got this problem:

Notice: Undefined variable: row... in line 178

What I want to do is enter the student number then click the Find button, then display the record in the from.

Here is my code:

Code:
if(isset($_POST['find'])) { // find the athlete

	// Connect the user in the database.
	require_once ('../mysql_connect.php'); // Connect to the db.

	// Create a function for escaping the data.
	function escape_data ($data) {
		global $dbc; // Need the connection.
		if (ini_get('magic_quotes_gpc')) {
			$data = stripslashes($data);
		}
		return mysql_real_escape_string($data, $dbc);
	} // End of function.

	$message = NULL; // Create an empty new variable.

    // check for student number
	if (empty($_POST['student_number'])) { // if student number is not entered
        $sn = FALSE;
       	// Check for a last name.
    	if (empty($_POST['last_name'])) {  // check the surname of the athlete.
	     	$ln = FALSE;
    		$message .= '<p>You forgot to enter the student number or the the Surname of the athlete to search!</p>';
	    } else {
	    	$ln = escape_data($_POST['last_name']);
	    }
	} else {
		$sn = escape_data($_POST['student_number']);
	}

    if ($sn) { // If student number is entered.
		$query = "SELECT studno, lname, fname, mname, nname, course, presentadd, telno, cellno, email FROM basic_info WHERE studno='$sn'";
    } else { // last name is entered
        $query = "SELECT studno, lname, fname, mname, nname, course, presentadd, telno, cellno, email FROM basic_info WHERE lname='$ln'";
    }


	$result = @mysql_query($query); // Run the query.

    if ( mysql_errno() > 0 ) {
       echo mysql_error();
       exit;
    }

    if ( mysql_num_rows( $result ) == 0 ) {
      echo "no results";
    }
    else {
    // display the data in the form
    $student_number =$row['StudNo'];  // this is line 178
    $last_name = $row['lname'];
    }

  } // End of Find


// Print the error message if there is one.
if (isset($message)) {
	echo '<font color="red">', $message, '</font>';
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>

<p><b>Student Number:</b> <input type="text" name="student_number" size="15" maxlength="8" value="<?php if (isset($_POST['first_name'])) echo $_POST['student_number']; ?>" /></p>
<p><b>Last Name:</b> <input type="text" name="last_name" size="20" maxlength="20" value="<?php if (isset($_POST['last_name'])) echo $_POST['last_name']; ?>" /></p>
<p><b>First Name:</b> <input type="text" name="first_name" size="40" maxlength="40" value="<?php if (isset($_POST['first_name'])) echo $_POST['first_name']; ?>" /></p>
<p><b>Middle Name:</b> <input type="text" name="middle_name" size="20" maxlength="20" value="<?php if (isset($_POST['middle_name'])) echo $_POST['middle_name']; ?>" /></p>
<p><b>Nick Name:</b> <input type="text" name="nick_name" size="15" maxlength="15" value="<?php if (isset($_POST['nick_name'])) echo $_POST['nick_name']; ?>" /></p>
<p><b>Course:</b> <input type="text" name="course" size="15" maxlength="15" value="<?php if (isset($_POST['course'])) echo $_POST['course']; ?>" /></p>
<p><b>Present Address:</b> <input type="text" name="present_address" size="40" maxlength="40" value="<?php if (isset($_POST['present_address'])) echo $_POST['present_address']; ?>" /> </p>
<p><b>Email Address:</b> <input type="text" name="email" size="40" maxlength="40" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /> </p>
<p><b>Phone Number:</b> <input type="text" name="phone_number" size="10" maxlength="50" value="<?php if (isset($_POST['phone_number'])) echo $_POST['phone_number']; ?>" /></p>
<p><b>Cell Number:</b> <input type="text" name="cell_number" size="20" maxlength="50" value="<?php if (isset($_POST['cell_number'])) echo $_POST['cell_number']; ?>" /></p>
</fieldset>

<div align="center">
<input type="submit" name="find" value="Find Athlete" />
</div>

</form><!-- End of Form -->
Why does it say undefined variable: row?

Please help me on this. Thanks a lot in advance.
Reply With Quote
  #2 (permalink)  
Old 05-12-05, 12:22 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by jozin
PHP Code:

    $result = @mysql_query($query); // Run the query.


    
if ( mysql_errno() > ) {
       echo 
mysql_error();
       exit;
    }

    if ( 
mysql_num_rows$result ) == ) {
      echo 
"no results";
    }
    else {
    
// display the data in the form
    
$student_number =$row['StudNo'];  // this is line 178
    
$last_name $row['lname'];
    } 
Why does it say undefined variable: row?
Simply because you haven't defined the $row variable yet

Seriously, after you have executed your $query, you have a resource *pointer* to the returned results in $results - it doesn't contain any database rows or anything useful yet. What you want to do is to get all the database rows - pointed by this resource pointer - with functions mysql_fetch_row() or mysql_fetch_assoc() - you can do it with a loop that could look something like this:

PHP Code:

while ($row mysql_fetch_assoc($result)) {

    
// handle your database $row here
    
print_r($row);  // echoes the whole $row to screen

See, the $row gets defined in the beginning of the loop, and when there is no more rows to handle, it gets the value false and the loop exits. I hope that helps.

You can probably get some results by changing your code a bit:

PHP Code:

else {

  while (
$row mysql_fetch_assoc($result)) {
         
// display the data in the form
         
$student_number =$row['StudNo'];  // this is line 178
         
$last_name $row['lname'];
         echo 
"$last_name$student_number<br>"
  
}


Last edited by darkfreak; 05-12-05 at 12:25 AM.
Reply With Quote
  #3 (permalink)  
Old 05-12-05, 01:22 AM
jozin jozin is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks darkfreak for the help.

I really appreciate it.
Reply With Quote
  #4 (permalink)  
Old 05-12-05, 02:44 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Errors in above

In the code above the following part was added by this forum system:

BEGIN__VBULLETIN__CODE__SNIPPET

Forget that string.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NOTICE: Undefined index jozin PHP 6 04-29-05 04:19 AM
Include problem in C bkbenson C/C++ 4 02-08-05 04:24 AM
Notice: Undefined variable: netbakers PHP 2 01-17-05 08:03 AM
what is the best method to find last word in variable ? GS300 PHP 6 09-15-04 09:13 PM
hidden variable problem scorpioy PHP 1 05-23-04 05:19 AM


All times are GMT -5. The time now is 06:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.