Populate array from a database

Hello,

I’m trying for hours now to populate an array from a table in my database.

The problem I have is that after populating the array and trying to see each element in the array, I only see the last element.

Went through several tutorials, tried several ways, nothing works.

What I need to do is create a table cell on my page for each record in the table.

The table loooks like this:

Id
HamsaTitle
ImgUrl
ThmbUrl

For now I just try to echo things to see what my array holds.

This is the last thing I tried:

$query = “select * from hamsa”;
$result = mysql_query($query) or die(‘Sorry, no hamsa judaica was found’);

While ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$hamsa = array(‘Id’=>$row[‘Id’], ‘Title’=>$row[‘HamsaTitle’]);
}

foreach( $hamsa as $v )
{
echo $v."<br />
";
}

I get only the last record in the table.

Can someone please help?

Thanks,
Sigal

Hi,

Your problem is here:


$hamsa = array('Id'=>$row['Id'], 'Title'=>$row['HamsaTitle']);

Each iteration $hamsa value is being overwritten. That’s why you are getting only the last record.


<?php

$result = mysql_query('SELECT * FROM hamsa') or die(mysql_error());

While ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	$hamsa[] = array(
		'Id' => $row['Id'],
		'Title' => $row['HamsaTitle']
	);
}

foreach( $hamsa as $v )
{
	echo $v['id']."<br />\
";
}

?>

:slight_smile:

Or you are wanting like this:


$result = mysql_query("SELECT * FROM hamsa") or die('Sorry, no hamsa judaica was found');

While($row = mysql_fetch_array($result, MYSQL_ASSOC))
	$hamsa[$row['Id']] = $row['HamsaTitle'];
foreach($hamsa as $id=>$title){
	echo $title . "<br />\
";
}

Thanks, but this doesn’t help me.

I need to show all the fileds in a record, your solution shows only one field.

So if I have a table with 3 fields in it, how will I display each record one after the other?

Let’s say my table has 3 fields called:
a
b
c

and 3 records

record 1:
a=1 b=11 c=111

record 2:
a=2 b=22 c=222

recocrd 3:
a=3 b=33 c=333

I want my page to show a table with 3 rows and 3 columns:

a b c
1 11 111
2 22 222
3 33 333

How will I do that?

Thanks,
Sigal

Hi,

You should read some books dude. (www.hudzilla.org/php/)


<?php

$result = mysql_query('SELECT a, b, c FROM hamsa') or die(mysql_error());

echo '<table>
		<tr>
			<td>a</td>
			<td>b</td>
			<td>c</td>
		</tr>
';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	echo('<tr>
			<td>'.$row['a'].'</td>
			<td>'.$row['b'].'</td>
			<td>'.$row['c'].'</td>
		</tr>
	');
}

echo '</table>';

?>

I wonder why you want to store all the information in the array before printing the. If you dont need to store them in the array then you can simply do what ludesign.bg says above. Otherwise do it like this then:


$result = mysql_query("SELECT * FROM hamsa") or die('Sorry, no hamsa judaica was found');

While($row = mysql_fetch_array($result, MYSQL_ASSOC))
    $hamsa[$row['Id']] = array('title'=>$row['HamsaTitle'],'url'=>$row['ImgUrl'],'thumburl'=>$row['ThmbUrl']);
foreach($hamsa as $id=>$info){
    echo $info['title'] . "-" . $info['url'] . "<br />\
";
}

Thank you very much rajug.

I have my reasons why I want to put it in an array, my page is designed in a specific way that does not allow me to do what ludesign is offering. And also, what he is offering won’t work since he just hardcoded the a, b, c in the first part.

Nevermind, you really helped.

Thanks again,

Sigal

Hi ludesign,

First thanks for the help, but no need to be cynical, if you don’t want to help, just don’t, but when you help and give a cynical remark with it, it ruins the whole point of helping.
I did an online php course, and I read lots of tutorials as I said in my first post, but couldn’t figure out how to do what I need. I thought that’s why we have forums, so we can ask questions when we get stuck. Thanks for the link.

Second, I’m not a dude, I’m a female :slight_smile:

Thanks,
Sigal