php mysql question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

php mysql question

Post by smoky989 »

OK I have a small form that inserts ten entries into a database table with 7 fileds in it. Each time the form is submitted the 10 rows are deleted and replaced with the new values. Now I have a page where I want to retrieve the information on the ten rows and asign a variable to each row and field.

Usually when I select data I use the 'where' keyword to specify which row I want to select. Than I would do

$row = mysql_fetch_array($result);

$var = stripslashes($row["field_name"]);

Now since I'm retrieveing the entire table how would I asign variables for each row/field combonation.

I'd imagine the array stores all the values in a multidimensional array so could I do something like this?

$var = stripslashes($row[1, 1]); than do [1, 2] and so on for the 7 fileds than do the next row like [2, 1] and so on.

I'm off to work and I will try that when I get home but oif anyone can confirm if I can do it that way or if there is a better way to do it let me know. Thanks in advance.

-Dave
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Re: php mysql question

Post by AVATAr »

smoky989 wrote:Now since I'm retrieveing the entire table how would I asign variables for each row/field combonation.
As you said here "foreach"!!!

with that function you fo for each row and for each field....
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

Hmmm

Post by smoky989 »

Well I read up on the foreach function but I'm not sure if its going to solve my problem. When this data is entered into the database it is also printed on the screen where I want them.

On the page where I'm calling the information from the database I'd like to use the same code but to do that I need to be able to asign each row/field combo to its own variable like

$r1f1 = row one field one of the databse
$r1f2 = row one field one of the databse.........up to r1f7 for all seven fields

than

$r2r1 = row one field one of the databse......up to r2f7 for all seven fields

and do that for all 10 rows in the database. I'd like to do this for two reasons.

1. as I mentioned above I'd like to resue the code I already wrote.
2. the variables will be written on the page in different places. And depending on circumstances some may not be used at all. I want to asign them all to their own variables and than use.

<? echo $var ?> wherever I want that variable. Any ideas. I'll play around with the foreach function more tomorrow but it doesn't appear to be what I'm looking for.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could do something like:

Code: Select all

$num_rows = mysql_num_rows($result);
for ($i=1; $i <= $num_rows; $i++) {
    $row = mysql_fetch_assoc($result);
    foreach ($row as $key => $var) {
        $info[$i][$key] = stripslashes($var);
    }
}
// to see the resulting array of all the data
echo '<pre>';
print_r($info);
echo '</pre>';
Then you get all of the results in one array and can reference needed results as a when you need to. IMHO, keeping things like that in an array makes life a lot easier that saving each thing to an individual variable.

Mac
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

Thanks

Post by smoky989 »

Thanks alot, that worked great
Post Reply