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.

Link to comment
Share on other sites

When using mysql_num_rows(), you're never actually fetching the result data.

 

if (mysql_num_rows($techMatch) > 0)  # found existing tech
{
$techMatch = mysql_fetch_assoc($techMatch); //added
$tNum = $techMatch['t_num'];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.