Jump to content

Recommended Posts

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.

   

$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.

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.

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)");

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)

 

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.