0

I have multiple record(s) in PHPMYADMIN and now i am trying to fetch those record(s) using PHP Code, but always i am getting Array ( ) 1 whenever i run my php script using Localhost, however i have 5 rows in table.

Please see below code:

<?php
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("mydatabase");

$strMemberID = $_POST["sMemberID"];
$strSQL =  "SELECT * FROM order_details WHERE 
MemberID = '".mysql_real_escape_string($strMemberID)."' ORDER BY OrderID DESC ";

$objQuery = mysql_query($strSQL);


while($obResult = mysql_fetch_assoc($objQuery))
{
$arr = array();  
$arr["OrderID"] = $obResult["OrderID"];
$arr["ItemDetails"] = $obResult["ItemDetails"];
}
mysql_close($objConnect);   
echo print_r($arr); 
?>
Chulbul Pandey
  • 506
  • 1
  • 8
  • 20
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Jun 13 '13 at 04:52
  • 2
    your code is ___vulnerable to sql injection___ you need to escape all request and are you sure that sMemberId is set ? – NullPoiиteя Jun 13 '13 at 04:53
  • 2
    Also don't echo `print_r`. – Yogesh Suthar Jun 13 '13 at 04:54
  • 1
    You're overwriting your `$arr` in each loop. try `var_dum($obResult);` to check whether you getting result as desired or not. – Rikesh Jun 13 '13 at 04:55
  • It seems that you are following a tutorial written in **1998**. The current year is **2013**. Please don't use `mysql_` functions. – 000 Jun 13 '13 at 05:02

2 Answers2

0

change your code in while loop.

declare $arr outside the loop. declaring array inside loop will clear it before initializing. thats why you are getting a single row in each run.

$arr = array(); 
while($obResult = mysql_fetch_assoc($objQuery))
{
$arr["OrderID"] = $obResult["OrderID"];
$arr["ItemDetails"] = $obResult["ItemDetails"];
}

Also, to view array elements use echo json_encode($arr) or var_dump($arr) or print_r($arr);

it will definitely work for you

0

It's not directly an answer to your question but while you're at it try to use PDO and prepared statements

$strMemberID = $_POST["sMemberID"];
$strSQL = 'SELECT * FROM order_details WHERE MemberID = ? ORDER BY OrderID DESC';
try {
    $db = new PDO('mysql:host=localhost;dbname=dbname;charset=UTF8', 'user', 'password');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $query = $db->prepare($strSQL);
    $query->execute(array($strMemberID));
    $result = $query->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo 'Exeption: ' .$e->getMessage();
    $result = false;
}
$query = null;
$db = null;
var_dump($result);

You may like it.

peterm
  • 91,357
  • 15
  • 148
  • 157
  • you need to set PDO::ATTR_EMULATE_PREPARES , false for reason see http://stackoverflow.com/questions/134099/are-pdo-prepared-statements-sufficient-to-prevent-sql-injection/12202218#12202218 – NullPoiиteя Jun 13 '13 at 05:18
  • @NullPoiиteя Skipped it for brevity. But anyway thanks, good point. Updated the answer to include it. – peterm Jun 13 '13 at 06:23