do/while multiple arguments

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

do/while multiple arguments

Post by fariquzeli »

I have a huge db of data drawing information from a form that is submitted. There is so much data that i made the form submit to 4 different mysql tables that are all within the same db. Now I know I have my connections correct, but what I want to have happen is have the rows of my html table repeat, while there is still data in the 4 mysql tables. I know that the rows in the mysql tables will all be identical, so that isn't a problem, the problem i'm having is in my do {repeat info goes here} and while {what is going to make the information repeat}

sometimes I can have one table of data shown, but then it just repeats the same entry for the other 4 entries, sometimes I can have no info from all but one table, this is what I have written for my wile statement right now:

Code: Select all

while ($row_general = mysql_fetch_assoc($general) & $row_curriculum = mysql_fetch_assoc($curriculum) & $row_products = mysql_fetch_assoc($products) & $row_miscellaneous = mysql_fetch_assoc($miscellaneous));
the 4 tables are general curriculum products and miscellaneous. That code won't work it has a table with 5 rows, 4 are blank and one has the info from the first row of mysql, can anyone help?
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

you want && not &. The former is the equivalent of AND in a mysql statement, while the latter is a bitwise and.
What a bitwise and does is it takes binary arguments and compares each bit. It makes:

Code: Select all

10110111 & 01100101 = 00100101
If both are true, the resulting bit is true, otherwise it is false.
Therefore, it seems that & is not what you want.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

First, it's &&, not &. Secondly I take back my statement about using || in the other thread. What it would do is set the first array, find that it is true, then short-circuit out of the evaluation because it knows if one thing in an OR is true, the statement is true. That's why (I assume)

Code: Select all

mysql_query($query) or die(mysql_error());
works.

Try a table join to get all your information into one place:

Code: Select all

$result = mysql_query("SELECT table1.field table2.field FROM table1, table2 WHERE table1.id = table2.id");
Then go through the while() loop.

The id field here is the linking field, the one all of them have in common.

Good luck on the project.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

Code: Select all

while ($row_general = mysql_fetch_assoc($general) && $row_curriculum = mysql_fetch_assoc($curriculum) && $row_products = mysql_fetch_assoc($products) && $row_miscellaneous = mysql_fetch_assoc($miscellaneous));
that is the code I have now. This is what the page looks like
http://www.youngdental.com/surveys/admin/all.php

still have blank cells below the first row.[/url]
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

table still doesn't show anything, only shows first row.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

First off, you should probably be able to get all of those queries into one SQL statement with one (or a few) joins.IMHO, you should pursue this to make your code a lot cleaner.
Secondly, do a print_r on the values of all the rows immediately after the while loop. If any one of them returned false then your while loop would exit. In what state are the variables once you exit the loop?
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

I just gave up on it, made 4 different tables, each one with a do while statement rather than try to combine it all

when joining tables on querys:

if i join them all by one unique id, i can then use all of the table's info through queries?

i'm sort of a newbie and it lookd to me like you could get specific info from one field from each table by a join, but not an entire table's data from joining them by one unique id
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

I know you gave up on it, but let me try to explain why your loop was exiting after the first iteration. That last table you have (suggesstions/comments) only has one entry. So, after the first loop, PHP went to try and find the next row in all of your statements. For the loop to continue, they all had to be true (because they were connected with ANDs). Up until the last one, they were all true, but the last one returned false because there were no more rows to return. Therefore, the loop exited, its conditions not having been met.
As to the question that you're asking now, it's really too large for me to get into here, and it involves a lot of database design. Therefore, I recommend you get a book which teaches database design. I personally have found "Relational Database Design" by Jan L. Harrington to be an excellent book. If you read it, you'll never have another question about joins or how to structure tables - a Good Thing(tm).
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

Oh, I totally understand what you are saying now about why my rows wouldn't repeat.

I can't believe it was the one comments/suggestions column. I will look into that book, thanks a bunch you really helped me along alot.
Post Reply