rbragg Posted July 18, 2007 Share Posted July 18, 2007 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 https://forums.phpfreaks.com/topic/60555-solved-variable-not-being-set-when-using-mysql_num_rows/ Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 what about if ( mysql_fetch_array($techMatch) === true) { $tNum = $techMatch['t_num']; } else # new tech so insert new Link to comment https://forums.phpfreaks.com/topic/60555-solved-variable-not-being-set-when-using-mysql_num_rows/#findComment-301235 Share on other sites More sharing options...
lur Posted July 18, 2007 Share Posted July 18, 2007 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 https://forums.phpfreaks.com/topic/60555-solved-variable-not-being-set-when-using-mysql_num_rows/#findComment-301237 Share on other sites More sharing options...
king arthur Posted July 18, 2007 Share Posted July 18, 2007 You can also just do $tNum = mysql_result($techMatch, 0); Link to comment https://forums.phpfreaks.com/topic/60555-solved-variable-not-being-set-when-using-mysql_num_rows/#findComment-301246 Share on other sites More sharing options...
rbragg Posted July 18, 2007 Author Share Posted July 18, 2007 Thank you both very much for your quick replies. I used lur's method and this works VERY well and I understand where I went wrong. Again, thanks much. Link to comment https://forums.phpfreaks.com/topic/60555-solved-variable-not-being-set-when-using-mysql_num_rows/#findComment-301249 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.