surajkandukuri Posted October 14, 2009 Share Posted October 14, 2009 Hi, Here is the php code that I have, Query is running properly in phpmyadmin and is resulting 5 which is correct but when i try to get tht in php its not printing the value $sql_query= "Select Count(*) from (Select Cat_Num from cat_completiondate where `EmployeeNumber`=$employeenumber and `CategoryJobPosition` =$jobid) as temp"; $query_result2=mysql_query($sql_query); $result_row2=mysql_fetch_array($query_result2); $value=$result_row2['temp']; echo($value) Output:- It doesnot print anything. Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/ Share on other sites More sharing options...
GKWelding Posted October 14, 2009 Share Posted October 14, 2009 $sql_query= "Select Count(*) from (Select Cat_Num from cat_completiondate where `EmployeeNumber`=$employeenumber and `CategoryJobPosition` =$jobid) as temp"; $query_result2=mysql_query($sql_query); while($result_row=mysql_fetch_array($query_result2){ $value = $result_row['temp']; echo($value); } also, try using code tags. Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/#findComment-937035 Share on other sites More sharing options...
GKWelding Posted October 14, 2009 Share Posted October 14, 2009 just as a pointer, I knew the answer to this straight off, but if you didn't then try googling it first, I just googled mysql_fetch_array to test this and the first page back was http://php.net/manual/en/function.mysql-fetch-array.php which shows you exactly what I've just shown you above. Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/#findComment-937036 Share on other sites More sharing options...
dawsba Posted October 14, 2009 Share Posted October 14, 2009 function q($query,$assoc=1) { $r = @mysql_query($query); if( mysql_errno() ) { $error = 'MYSQL ERROR #'.mysql_errno().' : ' . mysql_error(). '< / small>< br>< VAR>$query< /VAR>'; echo($error); return FALSE; } if( strtolower(substr($query,0,6)) != 'select' ) return array(mysql_affected_rows(),mysql_insert_id()); $count = @mysql_num_rows($r); if( !$count ) return 0; if( $count == 1 ) { if( $assoc ) $f = mysql_fetch_assoc($r); else $f = mysql_fetch_row($r); mysql_free_result($r); if( count($f) == 1 ) { list($key) = array_keys($f); return $f[$key]; } else { $all = array(); $all[] = $f; return $all; } } else { $all = array(); for( $i = 0; $i < $count; $i++ ) { if( $assoc ) $f = mysql_fetch_assoc($r); else $f = mysql_fetch_row($r); $all[] = $f; } mysql_free_result($r); return $all; } } add the function and call as such $temp = q("Select Count(*) from (Select Cat_Num from cat_completiondate where `EmployeeNumber`=$employeenumber and `CategoryJobPosition` =$jobid)"); Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/#findComment-937037 Share on other sites More sharing options...
DavidAM Posted October 14, 2009 Share Posted October 14, 2009 mysql_fetch_array returns an associative array or numeric array or both. However, your query does not return a column named 'temp' so your assignment: $value=$result_row2['temp']; is not assigning anything to $value. 1. Turn on error reporting at the beginning of your script error_reporting(E_ALL);. Then you will see the error message about referring to a non-existant key. 2. Either specifically assign the column a name or refer to the numeric index $sql_query= "Select Count(*) AS temp from (Select Cat_Num from cat_completiondate where `EmployeeNumber`=$employeenumber and `CategoryJobPosition` =$jobid) as temp"; $query_result2=mysql_query($sql_query); $result_row2=mysql_fetch_array($query_result2); $value=$result_row2['temp']; $value=$result_row2[0]; // Should return the same value echo($value) Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/#findComment-937041 Share on other sites More sharing options...
surajkandukuri Posted October 14, 2009 Author Share Posted October 14, 2009 Thank you DavidAM, My problem is solved and I could see the error I made. Quote Link to comment https://forums.phpfreaks.com/topic/177710-unable-to-retreve-the-values-from-mysql-query/#findComment-937044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.