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

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

LVL 7
MHenryAsked:
Who is Participating?
 
Michael701Connect With a Mentor Commented:
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
 
Michael701Commented:
oops, i forgot $x++ at the end of the loop
0
 
MHenryAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
agamalCommented:
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
 
MHenryAuthor Commented:
Yeah, I tried that too. Still getting a parse error at line 9
0
 
agamalCommented:
can you send us the complete code
0
 
Michael701Commented:
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
 
MHenryAuthor Commented:
The constant, that was the problem. I had an if statement around it. Just had to move this outside...

Thanks!

MH
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.