-1

If I have a MySQL query that looks like this:

SELECT Content FROM AP_Data WHERE Title='Featured' or Title='Carousel'

Is it possible to use PHP to then turn the variables into something like

echo $Row['Featured']['Content'];
echo $Row['Carousel']['Content'];

I hope that by doing this, I can save time without bombarding the server with multiple queries. This is only a snippet of the queries I require.

Blease
  • 1,380
  • 4
  • 38
  • 64
  • first you `select content` not all columns , replace `select content` by `select *` , and this is not possible – Charaf JRA Sep 16 '13 at 20:11

3 Answers3

2

First lets update your query to include the Title field.

SELECT Content, Title FROM AP_Data WHERE Title='Featured' or Title='Carousel'

When looping through results you can build the array to suit your needs

<?php
while($row = your_query_result){

  //If you have one Content per Title   
  $array[$row['Title']] = $row['Content'];

  //If you have multiple Contents per Title
  $array[$row['Title']][] = $row['Content'];

}
?>
jbcappell
  • 116
  • 1
  • 4
0

Featured and Carousel are not column names , they just values in columns.

you can do this instead.

    SELECT Content,Title FROM AP_Data WHERE Title='Featured' or Title='Carousel'

and the code

if  ($Row['Title'] == 'Featured') 
    {echo $Row['Content']; // content of featured
    } 
else{
    echo $Row['Content']; //content of carousel
    }
echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

No, it does not work that way. But if you add Title to your query, like so:

SELECT Title,Content FROM AP_Data WHERE Title='Featured' or Title='Carousel'

You can then build an array like that. You can also do an ORDER BY Title so that all of the Carousel results would be first.

aynber
  • 22,380
  • 8
  • 50
  • 63