2

I'm trying out my hand at php at the moment - I'm very new to it!

I was wondering how you would go about selecting all items from a mySQL table (Using a SELECT * FROM .... query) to put all data into an array but then not displaying the data in a table form. Instead, using the extracted data in different areas of a web page.

For example:

I would like the name, DOB and favorite fruit to appear in one area where there is already say 'SAINSBURYS' section hardcoded into the page. Then further down the next row that is applicable to 'ASDA' to appear below that.

I searched both here and google and cant seem to find an answer to my strange questions! Would this involve running the query multiple times filtering out the sainsburies data and the asda data where ever I wanted to place the relevant

   echo $row['name']." ";
   echo $row['DOB']." "; etc etc

next to where it should go?

I have got php to include data into an array (I think?!)

$query = "SELECT * FROM people"; 

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
   echo $row['name']." ";
   echo $row['DOB']." ";
   echo $row['Fruit']." ";
}

?>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ez007
  • 135
  • 2
  • 12

2 Answers2

0

Just place this (or whatever your trying to display):

echo $row['name']." ";

Anywhere you want the info to appear. You can place it within HTML if you want, just open new php tags.

<h1>This is a the name <?php echo $row['name']." ";?></h1>
Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
  • Thanks Paul, so if I typed it would print out all data for jane (DOB and fav fruit?) – ez007 Apr 17 '12 at 17:51
  • `echo $row['name']." ";` is going to just print the name of the person. It looks like your missing a couple of steps before this. You'll need to select the values with either a loop or via the databse query – Paul Dessert Apr 17 '12 at 17:57
  • Ill amend my question above to include what I had so far....I can print out all of the data - just not the part of the data (relating to a certain name in the DB) that I need. – ez007 Apr 17 '12 at 17:59
  • I think to explain a little more what I meant...I want to limit the first 4 rows to show, then with each individual row I want to decide where it appears on my webpage - not just in a standard print out table – ez007 Apr 17 '12 at 18:11
  • You'll need to define the `user` before you can pull the data you're looking for. I don't have the time at the moment to explain any further (sorry), but if you look at this thread, it will point you in the right direction. http://stackoverflow.com/questions/3047506/get-value-from-mysql-database-with-php – Paul Dessert Apr 17 '12 at 18:43
  • Thanks for your help Paul! I've solved it - it was right in front of me! – ez007 Apr 17 '12 at 20:10
0

If you want to access your data later outside the while-loop, you have to store it elsewhere.

You could for example create a class + array and store the data in there.

class User {
    public $name, $DOB, $Fruit;
}

$users = new array();

$query = "SELECT * FROM people"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)) {
    $user = new User;
    $user->name = $row["name"];
    $user->DOB = $row["DOB"];
    $user->Fruit = $row["Fruit"];

    $users[$row["name"]] = $user;
}

Now you can access the user-data this way:

$users["USERNAME"]->name

$users["USERNAME"]->DOB

$users["USERNAME"]->Fruit