benjumanji Posted October 8, 2008 Share Posted October 8, 2008 Hi, MySQL Server version is : 5.0.67 php 5.2.6 Basicall I have a query on a single table that I *know* works and returns the result I want, however retrieving that in php is giving me headaches and I cannot fathom for the life of me why. I tried the following just to get everything out of the result <?php $query = "SELECT MIN(expiry) FROM entries WHERE expiry != 0"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { foreach ($row as $key => $value) { echo '$row["'.$key.'"] : '.$value.'<br/>'; } } ?> And all I get back is $row["0"] : $row["MIN(expiry)"] : This is so frustrating! Like I say, if I run this query in MySQL is returns a single row/column wit the desired number. If someone could tell me what I am doing wrong I would be much obliged. Quote Link to comment https://forums.phpfreaks.com/topic/127521-solved-retrieving-result-from-min-query/ Share on other sites More sharing options...
Chicken Little Posted October 8, 2008 Share Posted October 8, 2008 If all you need is the minimum number you can use this: $result = mysql_query('SELECT MIN(expiry) FROM entries'); if (!$result) { die('Invalid query: ' . mysql_error()); } echo "<br>"; echo "<br>"; while (list($expiry) = mysql_fetch_row($result)) { echo "$expiry"; } Quote Link to comment https://forums.phpfreaks.com/topic/127521-solved-retrieving-result-from-min-query/#findComment-659886 Share on other sites More sharing options...
benjumanji Posted October 8, 2008 Author Share Posted October 8, 2008 I want the minimum as long as it's not 0, because that has a special meaning. I'll test the code tonight. I stress that the issue here isn't with the query as such, because if I login in to MySQL and the command prompt and run it it gives me the result I want. The issue is that when I run the same query in php I get back a resource where the response to the query is null? Quote Link to comment https://forums.phpfreaks.com/topic/127521-solved-retrieving-result-from-min-query/#findComment-659956 Share on other sites More sharing options...
aschk Posted October 8, 2008 Share Posted October 8, 2008 Simplify and name your column 'min': <?php $query = "SELECT MIN(expiry) as 'min' FROM entries WHERE expiry != 0"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { echo $row['min']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127521-solved-retrieving-result-from-min-query/#findComment-660002 Share on other sites More sharing options...
benjumanji Posted October 8, 2008 Author Share Posted October 8, 2008 Thanks for all your input. The actual point of failure turned out to be the fact that I had somehow botched the query in the php file I'm such an idiot. Solved. Quote Link to comment https://forums.phpfreaks.com/topic/127521-solved-retrieving-result-from-min-query/#findComment-660095 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.