-2

sql statement that shows the employees for each age from 30 to 50 , DOB is only in db / my sql query:

$sql = "SELECT age AS Age, count( * ) AS no. of employees FROM ( SELECT year( curdate( ) ) - year( dob ) AS age FROM emp ) AS x WHERE age >=30 AND age <=50)"
$result = mysqli_query($con,"$sql");

is working fine in sql. but when i am using this query in php it is not working as, while runnig this command :

while ($row = mysql_fetch_array($result)) { // This is new code
if($counter <=10){

    echo "<tr>";
    echo "<td>" . $row['firstname'] . "</td>";
    echo "<td>" . $row['lastname'] . "</td>";
    echo "</tr>";
    $counter++;

i am getting an error mysql_fetch_array() expects parameter 1 to be resource, boolean help me ..

Yotam Omer
  • 14,836
  • 11
  • 58
  • 62
  • `echo mysql_error();` – zerkms Jul 04 '13 at 23:24
  • yes when i am checking that anythinf is in result .. this will give me an error – user2551859 Jul 04 '13 at 23:25
  • if ($result !== false) { // echo "result has some value "; } else { // an error has occured echo "error "; die – user2551859 Jul 04 '13 at 23:26
  • 1
    `this will give me an error` that's nice but if u don't tell us the error it will be harder to help u. You should also use `\`` for `no. of employees` as I doubt spaces are allowed there would become `\`no. of employees\`` and on your query you have an extra `)` at the end, `mysql_fetch_array` is not a function from mysqli is from mysql_*. – Prix Jul 04 '13 at 23:27
  • ohh sorry sir, means i have checked that mysql error.. for result: if ($result !== false) { // echo "error "; } else { // an error has occured echo "no error "; die; // note : echoing the error message and dying // is OK while developping, but not in production ! } – user2551859 Jul 04 '13 at 23:32
  • @user2551859 then you will die without knowing it. – Prix Jul 04 '13 at 23:33
  • sir help me . i have to solve this query – user2551859 Jul 04 '13 at 23:35
  • @user2551859: we cannot help unless you provide a error message. – zerkms Jul 04 '13 at 23:36
  • plz give me your chat id , so that i can clear you better , you will help me better then.. sir i m in need.. my gtalk id is amplifier100100@gmail.com – user2551859 Jul 04 '13 at 23:36
  • plzzz sir help me sir. – user2551859 Jul 04 '13 at 23:42
  • beside everything, you are trying to print non-existent columns `firstname` and `lastname`, since you've selected only `age` and `count` – Royal Bg Jul 05 '13 at 00:29
  • plz stop calling everybody "sir", plz? ... Study this site rules ([About] & [help]) and happy Q&A :) – brasofilo Jul 05 '13 at 01:32

2 Answers2

0

Why are you using mysqli_ and mysql_ functions at the same time? Replace *mysqli_query* with *mysql_query*.

Paweł Zegardło
  • 278
  • 3
  • 10
0

You've got a small typo, on your $result line. You've got 'mysqli' as opposed to 'mysql', which I'm assuming is what you wanted as you're using mysql_fetch_array(). This might mean that result isn't properly assigned to in a way that mysql_fetch_array() expects.

This could be where you've typed your code in as opposed to copy+paste, just a shot at what it might be there.

Craig Brett
  • 2,241
  • 1
  • 22
  • 26