0

Am new to PHP. I tried to fetch the elements from a table but it is getting stored as a single element in an array. I want each element in the column to be a separate element in the array.. Someone please help me with this..

$sql="select name from item_details";
$result=mysql_query($sql,$con);
if(!$sql)
echo "Query Failed".mysql_error();
while($arr=mysql_fetch_row($result))
{
echo $arr[0];
echo "<br>";
}
1
  • Dont use mysql_* functions any more.Use mysqli_* or pdo Commented Dec 19, 2013 at 7:56

2 Answers 2

0

You probably want to get an associative array of column names. Like so:

$sql="select name from item_details";
$result=mysql_query($sql,$con);
if(!$sql)
echo "Query Failed".mysql_error();
$rows = mysql_fetch_array($result);
foreach ($rows as $row) {
    echo $row['name'];
    echo "<br />";
}

For future reference, you can always dump the array to see how it looks and get a look at its values. For example:

$rows = mysql_fetch_array($sql);
var_dump($rows);// this will output the array and all its values nice and pretty.


Also, stay away from mysql_* functions.

As mentioned in the comments, you might want to ditch mysql_* functions as they are deprecated and will be removed in future PHP versions. You might want to look at PDO or an ORM like RedBean.

Sign up to request clarification or add additional context in comments.

Comments

0
$sql="select name from item_details";
$result=mysql_query($sql,$con);
if(!$sql)
echo "Query Failed".mysql_error();

while($arr=mysql_fetch_array($result))
{
   echo $arr['id']; //I assume a id column exists in item_details table
   echo "<br>";
}

2 Comments

What you have prescribed is wat happening to me... I dnt want to happen lik tat.. Wen i fetch a column from table its getting stored as a[0]... But i want each record in tat column to be stored as a[0],a[1],etc...
just stored them into another array. can I have an example of the result array

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.