Page 1 of 1

NEWBIE: Loading an array from a mysql_fetch_row output???

Posted: Wed Nov 13, 2002 8:09 am
by rathlon
What I read, and what I work with is the idea that the following only returns one "row" of info at a time, from the $result variable.

<?
$query = "select * from panties";
$result = mysql_query($query);
$number = mysql_num_rows($result);
for($i = 0; $i < $number; $i++)
{
$row = mysql_fetch_row($result);
for($k = 0; $k < count($row); $k++)
{
echo $row[$k];
}
}
?>

how would I (syntax value requested) load each row that is pulled from each $i loop into an array? What I want to do is use an "id" passing scheme inside my script so that I can loop through each rowset resident in the same script. I think I know how to do it (or at least a direction to press) but I can't seem to load the contents of the above into one array for continual use (pointer control is the goal). Am I heading in the wrong direction?

Posted: Wed Nov 13, 2002 9:14 am
by f1nutter
So you want to extract everything from a table and store it in an array?

Code: Select all

&lt;? 
$query = "select * from panties"; 
$result = mysql_query($query);

while ($row = mysql_fetch_row($result))
{
  $big_array&#1111;] = $row;
} 
?&gt;
This will create a 2D array.

Code: Select all

Array
(
 &#1111;0] =&gt; Array (
            &#1111;0] =&gt; col_0
            &#1111;1] =&gt; col_1
            )

 &#1111;1] =&gt; Array (
            &#1111;0] =&gt; col_0

...etc...
Could you not reduce the number of results by restricting the number of columns, and adding a WHERE clause, if appropriate? I guess you need something to do with "id" in WHERE as you mention that later on.

Another Newbie...tagging another question onto this answer

Posted: Wed Nov 13, 2002 6:45 pm
by Mel Voght
Can this array be returned and then used in html? To display the appropriate data on a webpage?

I'm a newbie and having a devil of a time getting my includes to work, I saw a thread on that that I'll try, but once I get the functions to work and return data, I'll need to figure out how that integrates w/ the html.

Currently, I've got a sample of code working that displays my images based on the filename that I pull from a mysql database, but I need to get this stuff into a table with the headers, footers and sidebars in....first step, get that include statement to work.

When I paste my php into the html, nothing happens...so I've obviously done something VERY wrong :oops: .

Mel

Posted: Thu Nov 14, 2002 12:04 pm
by rathlon
Mel,

I'm still new to this myself, but if you post your code, I will try to give you a hand. Sure somone else here can as well. Php integrates nicely with html and with mysql. Getting content displayed is easy, formatting it is the more challenging part -- which is still easy enough if you understand it. My problem is the book I'm currently reading, only covers concepts in general and doesn't cover what I'm trying to do at all. Give us a reply and I'm sure we can help.

Posted: Fri Nov 15, 2002 5:06 pm
by Mel Voght
DOH! Is there an emoticon for this???? :lol:

I KNEW I must have been making a really silly oversight. The file extension has to be .php! suddenly, the html that I surrounded the php with works!


Now to the little details of formating the output.