Jump to content

[SOLVED] How to retrieve mysql distinct query value in php?


Giri J

Recommended Posts

Hi,

 

My code is as follows:

 

$qry1="select distinct (coder) from daily_reports where week_no=$wkno";

$exeQ1=mysql_query($qry1) or die ("Q1 :: Error ::: ".mysql_error());

$num1=mysql_num_rows($exeQ1);

 

 

I need some help / syntax on how to retrieve the results of mysql query (select distinct (coder) from daily_reports where week_no=$wkno)

 

The query is working fine on mysql it retrieves 4 rows. I want to know how to get the results in php.

 

 

I tried mysql_fetch_array().

I get the following error:

Notice: Undefined offset: 1 in D:\wamp\www\Reports\qau\processGU.php on line 27

 

 

I tried searching but didn't exactly get the relevant solution.

Please post if there are any existing threads.

 

 

Thank you very much !

 

 

Cheers

G.

Arrays in all (sensible) programming languages index from 0. You've only selected one column from the table, so it's at index 0 not 1. You'd probably find it easier to give that field an alias an use that however:

 

$qry1="select distinct (coder) AS coder from daily_reports where week_no=$wkno";
$exeQ1=mysql_query($qry1) or die ("Q1 :: Error ::: ".mysql_error());
while($row=mysql_fetch_assoc($exeQ1) ){
    echo $row['coder'].'<br />;
}
?>

Sorry for changing your variables slightly, force of habbit. Here you go:

<?php
$result  = mysql_query("SELECT DISTINCT coder FROM daily_reports WHERE week_no='".mysql_real_escape_string($wkno)."'") or die(mysql_error());
$numRows = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)) {
print $row['coder']."<br />";
}
?>

Hi guys,

 

That's like a rocket response.

 

Here is my test code.

 

<?php

 

//Connections

mysql_connect("localhost", "root", "giri") or die(mysql_error());

mysql_select_db("hexl_data") or die(mysql_error());

 

//Get Week No.

//$wkno=$_POST['wkno'];

 

 

//Mysql Querys

$qry1="select distinct (coder) from daily_reports where week_no=2";

$exeQ1=mysql_query($qry1) or die ("Q1 :: Error ::: ".mysql_error());

$num1=mysql_num_rows($exeQ1);

 

//Result set

if($num1<1)

{

echo "Unable to retreive data. Please try again.";

}

else

{

while($rows = mysql_fetch_array($exeQ1, MYSQL_ASSOC))

{

echo "Coders:".$rows['Coder'];

}

}

 

?>

 

 

and here are my errors:

 

 

 

Notice: Undefined index: Coder in D:\wamp\www\Reports\qau\test.php on line 25

Coders:

Notice: Undefined index: Coder in D:\wamp\www\Reports\qau\test.php on line 25

Coders:

Notice: Undefined index: Coder in D:\wamp\www\Reports\qau\test.php on line 25

Coders:

Notice: Undefined index: Coder in D:\wamp\www\Reports\qau\test.php on line 25

Coders:

 

 

Let me know if anything is confusing.

 

Cheers

G.

The following code perfectly works.

u ROCK man \m/.

 

tons and tons of thanks to ya and thank you every one for your super fast responses. much much appreciated :)

 

 

 

Arrays in all (sensible) programming languages index from 0. You've only selected one column from the table, so it's at index 0 not 1. You'd probably find it easier to give that field an alias an use that however:

 

$qry1="select distinct (coder) AS coder from daily_reports where week_no=$wkno";
$exeQ1=mysql_query($qry1) or die ("Q1 :: Error ::: ".mysql_error());
while($row=mysql_fetch_assoc($exeQ1) ){
    echo $row['coder'].'<br />;
}
?>

Archived

This topic is now archived and is closed to further replies.

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