Jump to content

Recommended Posts

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);
}

Link to comment
https://forums.phpfreaks.com/topic/170249-array-from-database-problem/
Share on other sites

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);
}

 

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);
}

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?

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

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.

@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 );

 

 

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.