batearedfox Posted August 14, 2009 Share Posted August 14, 2009 Hi I'm having trouble with the code below, this works fine for an array that I've defined but if I grab the array from a database (will print_r() with no trouble) nothing is returned. I'm new to building my own php so this is probably something obvious but after 2 hours googling I just can't seem to spot what might be going wrong. Thanks for any help. Tom $query = mysql_query("SELECT * FROM final WHERE `0` = '0'") or die(mysql_error()); $row = mysql_fetch_assoc($query ); while ($new = current($row)) { echo key($new).'<br />'; next($row); } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 Are you sure your query is correct? `0` = '0' means the field 0 has the value 0. Normally you wouldn't name a field like that. Quote Link to comment Share on other sites More sharing options...
batearedfox Posted August 14, 2009 Author Share Posted August 14, 2009 Yes, fields are 0 through to 20 and the field 0 in the row I want is 0 Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 Check out the examples on mysql_fetch_assoc to see how you would iterate through the result set. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 14, 2009 Share Posted August 14, 2009 Also, the logic in your while loop will not work. Not sure what you are trying to accomplish. Added comments to your code to explain why you aren't getting any results. //Assigns the VALUE of the current element in the array while ($new = current($row)) { //key() will attempt to return the KEY of the current element of the array //However, $new is the extracted VALUE, so key() will return nothing in this instance echo key($new).'<br />'; next($row); } Quote Link to comment Share on other sites More sharing options...
batearedfox Posted August 14, 2009 Author Share Posted August 14, 2009 Hi, thanks for your responses I meant to put $row rather than $new in echo key() I'm trying to get the key of the values from a row if they aren't equal to a string eg 'sample', I'm basing this on the code at the bottom which came from the example on php.net for key, just dropped the if statement to see if that was causing any problems. //Assigns the VALUE of the current element in the array while ($new = current($row)) { //key() will attempt to return the KEY of the current element of the array //However, $new is the extracted VALUE, so key() will return nothing in this instance echo key($row).'<br />'; next($row); } Code I'm basing this on:- $array = array( 'fruit1' => 'apple', 'fruit2' => 'orange', 'fruit3' => 'grape', 'fruit4' => 'apple', 'fruit5' => 'apple'); // this cycle echoes all associative array // key where value equals "apple" while ($fruit_name = current($array)) { if ($fruit_name == 'apple') { echo key($array).'<br />'; } next($array); } Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 It still will not work. Check out what mysql_fetch_assoc returns (and I'll reiterate myself: see the examples on that page). Quote Link to comment Share on other sites More sharing options...
batearedfox Posted August 14, 2009 Author Share Posted August 14, 2009 Sorry Daniel0, didn't mean to ignore your advice. I still can't see a way to get the keys of values based on whether they match a string. I'll stare at those examples some more when I have time. Out of interest why would it print_r($row) as an array ok but not work with the while loop? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 Sorry, maybe I don't understand your question properly. What exactly are you trying to do? Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 assuming that your $row array is 1 dimensional, couldn't you just do $query = mysql_query("SELECT * FROM final WHERE `0` = '0'") or die(mysql_error()); while ($row = mysql_fetch_assoc($query )) { echo key($row).'<br />'; } Quote Link to comment Share on other sites More sharing options...
batearedfox Posted August 14, 2009 Author Share Posted August 14, 2009 Sorry, doesn't seem to work Mikesta707. I'm trying to go through all the cells on row 0 and see if any of them are either empty or match the string 'sample' (one or the other), then get the keys of the columns of those that aren't empty. To do this I was going to add IF != 'sample' to the while loop as in the fruit example but it won't read the array returned from mysql_fetch_assoc My intention is to use these to only display the columns where the fields aren't empty in row 0 as an html table. This is probably a really convoluted way to achieve this, i tried to just use a msql select but couldn't find a way to specify well enough. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted August 14, 2009 Share Posted August 14, 2009 hmm try a print_r on the $row array and see what you get Quote Link to comment Share on other sites More sharing options...
batearedfox Posted August 14, 2009 Author Share Posted August 14, 2009 @mikesta707 print_r returns nothing, the code you gave returns 0 which is the first entry of the array, missed that before, my page was getting a little full. If I print_r on $row in the code below I get an list of the whole array. $query = mysql_query("SELECT * FROM final WHERE `0` = '0'") or die(mysql_error()); $row = mysql_fetch_assoc($query ); Quote Link to comment 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.