Giri J Posted May 8, 2009 Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/ Share on other sites More sharing options...
Ken2k7 Posted May 8, 2009 Share Posted May 8, 2009 Can you post your mysql_fetch_array() part of the code? Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829461 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 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 />; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829464 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 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 />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829465 Share on other sites More sharing options...
GingerRobot Posted May 8, 2009 Share Posted May 8, 2009 Whoops - neil's right there. No need for an alias. Excuse the brain fart. Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829467 Share on other sites More sharing options...
Giri J Posted May 8, 2009 Author Share Posted May 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829470 Share on other sites More sharing options...
Giri J Posted May 8, 2009 Author Share Posted May 8, 2009 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 />; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829477 Share on other sites More sharing options...
JonnoTheDev Posted May 8, 2009 Share Posted May 8, 2009 while($row=mysql_fetch_assoc($exeQ1) ){ Quote Link to comment https://forums.phpfreaks.com/topic/157362-solved-how-to-retrieve-mysql-distinct-query-value-in-php/#findComment-829479 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.