Jump to content

MySQL PHP Question


gazalec

Recommended Posts

Hi i was wondering i am using a while loop to extract selected data from a MySQL database with PHP but i was wondering if there was anyway to assign these values to their own separate variable names as they came off? i want it so that each value that is extracted is assigned to its own variable because i want to use them separately later

 

Thanks for Any Help

Link to comment
Share on other sites

In an array you can address the elements individually..i.e. $array_name['element_name'].  In the mysql object you can only address them in a linear fashion...i.e. through a while loop.

 

while ($row = mysql_fetch_assoc($result)) {
  $data[$row['id'] = $row;
}

 

You can then address the items by their id...

 

echo $data[1]['username'];

 

Or if you use the username as the key...

 

while ($row = mysql_fetch_assoc($result)) {
  $data[$row['username'] = $row;
}

echo $data['gazalec']['post_count'];

Link to comment
Share on other sites

I dont think this is working for what i need, ok i'll explain the whole situation to see if it clears it up, on my database i have a table with cust_no, no_of_products, order_date, what i want is that you type in a cust_no it picks up all the orders they had i.e. order_date, products ordered on that date, then i want them to be separated into individual variables i.e., order_date1 = '2007-04-04' no_of_products1 = '700', order_date2 = '2007-03-29' no_of_products2 = '500' etc. for all their orders, is this even possible ???

 

Thanks so much for the help so far

 

Link to comment
Share on other sites

The only situation I can think where it would be preferable to to do it like that, rather than use an array, is if you are charging per line of code

 

<?php
foreach ($order_dates as $date) echo "$date<br>";
?>

Fee : £1.00

 

Contrast with

<?php
echo "$order_date_1<br>";
echo "$order_date_2<br>";
echo "$order_date_3<br>";
echo "$order_date_4<br>";
echo "$order_date_5<br>";
echo "$order_date_6<br>";
echo "$order_date_7<br>";
echo "$order_date_8<br>";
echo "$order_date_9<br>";
echo "$order_date_10<br>";
echo "$order_date_11<br>";
echo "$order_date_12<br>";
echo "$order_date_13<br>";
echo "$order_date_14<br>";
echo "$order_date_15<br>";
echo "$order_date_16<br>";
echo "$order_date_17<br>";
echo "$order_date_18<br>";
echo "$order_date_19<br>";
echo "$order_date_20<br>";
?>

Fee : £20.00

Link to comment
Share on other sites

well the thing is i need each order date and each no_of_products separately because i'm making a bar graph in php, so that a cell is giving id's and if it is in the  'order_date' column and is the value of the 'no_of_products' it makes its background black and if it isn't its white to do a kind of line graph thing, i'm experimenting. so i hope that clears things up

 

 

Sorry bout the double post

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.