1

I have a table called sales_product with columns

sales_product_id, sales_id, type, class, age

I have a query in php saying

$sql = "SELECT * from sales_product WHERE sales_id = " . $_GET['sales_id];

Suppose this query returns two rows.

$result = mysql_query($sql);

How do I loop through this result and save in an array so that later on I can retrieve data like

echo $sales_product[$i]['sales_product_id'];
echo $sales_product[$i]['sales_product_id'];
........
2
  • 1
    No No No to mysql_* extension Commented Aug 31, 2012 at 6:59
  • 1
    @fluty Go on ! Suggest other solution. Commented Aug 31, 2012 at 7:00

7 Answers 7

5

Do mysql_fetch_assoc in while loop:

$result = mysql_query($sql);
$rows[] = array();

while ( $row = mysql_fetch_assoc($result) ) {
    echo $row['sales_product_id'];
    $rows[] = $row;
}

var_dump($rows);
Sign up to request clarification or add additional context in comments.

4 Comments

but how do I save each row in an array?
sorry could you pls tell me how do I loop through this $rows[] array in order to display each rows
Use foreach then: foreach ( $rows as $row ) { var_dump($row); }
This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
1

Use mysql_fetch_assoc

while ($row = mysql_fetch_assoc($result)) {
    echo $row["sales_product_id"];
}

2 Comments

but how do I save each row in an array?
@KOU $row is an array with keys. If you want to use integers i.e $row[0] then you could use mysql_fetch_array() instead of fetch_assoc
0

Try this,

$result = mysql_query($sql);
while ( $row = mysql_fetch_assoc($result) ) {
    echo $row['columnName'];
}

Use mysql_fetch_array() if u need with index

Comments

0

check this one.

$sql="SELECT sales_product_id, sales_id, type, class, age From sales_product";
$sales= $db->query($sql);
while($sale = $db->fetchByAssoc($sales))
{
    echo $sales['sales_product_id'];
    echo $sales['type'];
 // here instead echo uyou will add data in your array

}

Comments

0

Take a look at

mysql_fetch_array();

Note

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_fetch_array()

PDOStatement::fetch()

Comments

0

Something like this should work, this will give you the format you requested (untested):

while($row = mysql_fetch_assoc($result)) {
    $sales_product[] = $row;
}

Comments

0

Since mysql_ has been declared deprecated so i am going to answer your question with
mysqli_* extension

//Prepare an SQL statement for execution
$stmt = $mysqli->prepare("SELECT * from sales_product WHERE sales_id = ?");

//bind parameter to the prepared statement      
$stmt->bind_param("i", $_GET['sales_id]);

//execute it 
$stmt->execute();

//store the result  
$result = $stmt->get_result();

//loop through the  result set
while ($myrow = $result->fetch_assoc()) 
{

      $salesIds[]  = $myrow["sales_product_id"];

}

For more information please refer to php documentation.
Thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.