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.

Link to comment
Share on other sites

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 />;
}
?>

Link to comment
Share on other sites

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 />";
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />;
}
?>

Link to comment
Share on other sites

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.