0

Basic php question here. The following SQL code returns an array containing a key called 'date'. All I want to do is parse the 'date' value based on the key name. Any help?

$result = mysql_query("SELECT * FROM `table` WHERE columnName ='value'") or die(mysql_error());
 $data = array();
while ( $row = mysql_fetch_assoc($result) )
{
  $data[] = $row;
}
echo $data->{'date'}
3

4 Answers 4

1

Ok here you go

With Foreach

foreach($data as $key => $value)
{
   if($key == 'date')
   {
      // do you parsing stuff
   }
}

Without foreach

$parsing_date = $data['date'];
Sign up to request clarification or add additional context in comments.

Comments

1

You're using object syntax on an array which you can't do.

 echo $data['date'],

Comments

0
foreach($data as $key => $value)
{
    if ($key == 'date')
    {
        echo "Date Value: '".$value."'";
        // Do your stuffs...
        echo "Date Final Value: '".$value."'";
    } 
}

Comments

0

mysql_fetch_assoc returns an ASSOCiative array. Which means it returns an array with your table name fields as keys. For example if your table has 3 columns 'date', 'name', and 'color'

Then you would access those fields in the following way.

$result = mysql_query("SELECT * FROM `table` WHERE columnName ='value'") or die(mysql_error());
 $data = array();

while ( $row = mysql_fetch_assoc($result) )
{
  echo $row['date']; //Prints the rows date field
  echo $row['name']; //Prints the row name field and so on.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.