?
Solved

how to get data from mySQL into variables - php5, mySQL

Posted on 2009-02-21
8
Medium Priority
?
235 Views
Last Modified: 2013-12-12
I have this select statement:
$query = "SELECT tbl_orderskus.orderSKU_SKU, tbl_orderskus.orderSKU_Quantity, tbl_customers.cst_ID FROM tbl_orderskus UNION, tbl_customers
WHERE tbl_customers.cst_ID = 'customerID']}";

I'm trying to take the results of that query and assign to a variable for each record in the database. So if they have 3 different SKUs, there would be an associated number, the quantity they ordered of each.

But I need to assign a variable to each so I can access it later. So something like:

Customer ordered three of sku1 --> $sku1=skuNumber1  and $quantity1=3
Customer ordered four of sku2 -->  $sku2=skuNumber2  and $quantity2=4

I tried this, but I keep getting Array ( ) 1 Array ( ) 1 no matter what they ordered.

$datax = array();
$datay = array();
if (isset($_SESSION['customerID']) && 0 != intval($_SESSION['customerID'])) {
$query = "SELECT tbl_orderskus.orderSKU_SKU, tbl_orderskus.orderSKU_Quantity, tbl_customers.cst_ID FROM tbl_orderskus UNION, tbl_customers
WHERE tbl_customers.cst_ID = 'customerID']}";
if ($result = mysql_query($query)) {
      while($data = mysql_fetch_array($result, MYSQL_NUM)) {
            $datax[] = $data[0];
            $datay[] = $data[1];
      }
}
}

Best,
MH

0
Comment
Question by:MHenry
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Accepted Solution

by:
Michael701 earned 2000 total points
ID: 23703382
You'll need to use variable variable names;

http://us2.php.net/manual/en/language.variables.variable.php
$x=1;
 while($data = mysql_fetch_array($result, MYSQL_NUM)) {
   $var_name='item'.$x;
   $$var_name = $data[0];
   $var_name='quantity'.$x;
   $$var_name = $data[1];
 
 
// to see
 
echo "Item1: ".$item1;

Open in new window

0
 
LVL 19

Expert Comment

by:Michael701
ID: 23703384
oops, i forgot $x++ at the end of the loop
0
 
LVL 7

Author Comment

by:MHenry
ID: 23703480
Michael701,

That isn't working. Looks like it's missing a } somewhere. I tried adding another at the end but that didn't work...
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 8

Expert Comment

by:agamal
ID: 23703819
i think he means

$x=1;
 while($data = mysql_fetch_array($result, MYSQL_NUM)) {
   $var_name='item'.$x;
   $$var_name = $data[0];
   $var_name='quantity'.$x;
   $$var_name = $data[1];
$x++;
} 
 
// to see
 
echo "Item1: ".$item1;

Open in new window

0
 
LVL 7

Author Comment

by:MHenry
ID: 23703901
Yeah, I tried that too. Still getting a parse error at line 9
0
 
LVL 8

Expert Comment

by:agamal
ID: 23703920
can you send us the complete code
0
 
LVL 19

Expert Comment

by:Michael701
ID: 23704944
can't have a constant in the mysql command.

If that's not it, what's on lines 8 and 9?
$x=1;
 while($data = mysql_fetch_array($result)) {
   $var_name='item'.$x;
   $$var_name = $data[0];
   $var_name='quantity'.$x;
   $$var_name = $data[1];
   $x++
}
 

Open in new window

0
 
LVL 7

Author Comment

by:MHenry
ID: 23705616
The constant, that was the problem. I had an if statement around it. Just had to move this outside...

Thanks!

MH
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question