Jump to content

php/mysql help ?


jd2007

Recommended Posts

let's say a table named 'cars' has two columms, one is 'id', second is 'make' in mysql. i want to take all content in 'make' and store in a numerical array called 'mycars'. the content of 'make' is ferrari, aston martin, buick and jaguar. if i echo mycars[0], it will display ferrari...how do i do that ?

Link to comment
Share on other sites

I have a stupid question for you. Are you familiar with running queries and retrieving data from a DB?

 

This is a pretty simple task

 

 

<?php

$query="SELECT * FROM cars";
$result=mysql_query($query);

while($row=mysql_fetch_object($result))
{
   $mycars[]=$row->make;
}

echo $mycars[0];

?>

 

So long as Ferrari is the first item pulled from the database, this code will do exactly that. You could use a while loop to display the results, or a for loop or a foreach loop. I suggest you get pretty familiar with using loops and getting data in and out of an array.

 

Hope this helps.

 

Link to comment
Share on other sites

Yes MySQL pulls data out of the data in order they are stored in the table, eg if ferrari was entered first and jaguar was entered second the mysql will pull Ferrari first followed by jaguar. You can change the order by using the ORDER BY clause within the query if you wish.

Link to comment
Share on other sites

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.