Jump to content

[SOLVED] variable not being set when using mysql_num_rows


rbragg

Recommended Posts

My whole objective here is to set $tNum. If a tech is found in the db, the $tNum should be set with t_num. If this is a new tech, there will be a new $tNum (given by the db via incrementation). My problem is that $tNum is not being set! Mainly, it's not being set when there is a match.

 

<?php 
# see if tech already exists
$queryTechMatch = " 
SELECT t_num
FROM tech
WHERE t_first = '".mysql_real_escape_string($_SESSION['tFirst'])."'
AND t_last = '".mysql_real_escape_string($_SESSION['tLast'])."'
";
$techMatch = mysql_query($queryTechMatch) or die( "Select tech query failed: " . mysql_error() );

if (mysql_num_rows($techMatch) > 0)  # found existing tech
{
  $tNum = $techMatch['t_num'];
}
else # new tech so insert new
{
  $insertTech = "
  INSERT into tech (t_first, t_last, t_phone)
  VALUES ('".mysql_real_escape_string($_SESSION['tFirst'])."', '".mysql_real_escape_string($_SESSION['tLast'])."', '".mysql_real_escape_string($_SESSION['tPhone'])."' 
  )";
  $newTech = mysql_query($insertTech) or die( "Insert tech query failed: " . mysql_error() );

  if ( $newTech )
  {
    # get the t_num of this new insert to store in call table
    $queryLastTech = "
    SELECT t_num
    FROM tech
    ORDER by t_num DESC
    LIMIT 1
    ";  
    $lastTechResult = mysql_query($queryLastTech) or die( "Select tech query failed: " . mysql_error() );
    $lastTech = mysql_fetch_assoc($lastTechResult);
    $tNum = $lastTech['t_num'];
  }
}
?>

 

However, if instead of using mysql_num_rows I use:

 

<?php
while( $row = mysql_fetch_array($techMatch) ) 
{
  $tNum = $row['t_num'];
}
?>

 

... it works. I still want to use my else statement, though.  :-\ Can someone help? Thanks in advance.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.