/    Sign up×
Community /Pin to ProfileBookmark

Querying MySQL DB twice in same file

Okay, I’m sure there is an easy way to do this but, since I’m so new to MySQL, I don’t know it off the bat. I’m pulling two item numbers to do comparisons with from the script prior to this page. I need to pull the info for the first item, write it out, then go back through, find the second item and pull its info and write it out. Things I’ve tried previously haven’t worked. The only way I have been able to figure out how to do it is to actually call the DB info twice, which just doesn’t seem the cleanest way to do it, considering I’m actually pulling 26 fields from the DB. Here is the only way I’ve found to get it to work, but what’s better?

[code=php]
<?php

$product1 = $_POST[‘product1’];
$product2 = $_POST[‘product2’];

require_once (“DB_info_file”);

$result = mysql_query(“SELECT all_of_my_db_ info ORDER BY item_number”);

$result2 = mysql_query(“SELECT all_of_my_db_info_AGAIN ORDER BY item_number”);
print “<table width=650><tr><td>n”;
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
if ($row{‘item_number’}==$product1)
{
print “<table width=325 border=1>n”;
print “<tr><td>”.$row{‘all_of_my_db_info1}.”</td></tr>n”;
print “<td>”.$row{‘all_of_my_db_info2}.”</td></tr>n”; \through 26 of em
}
}
print “</td><td>n”;
while ($row2 = mysql_fetch_array($result2, MYSQL_BOTH))
{
if ($row2{‘item_number’}==$product2)
{
print “<table width=325 border=1>n”;
print “<tr><td>”.$row2{‘all_of_my_db_info_AGAIN1}.”</td></tr>n”;
print “<td>”.$row2{‘all_of_my_db_info_AGAIN2}.”</td></tr>n”; \through 26 of em
}
}
print “</td></tr></table>n”;
?>
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 15.2005 — How about:
[code=php]
$query = "SELECT * FROM table_name WHERE item_number = '$product1' OR item_number = '$product2'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
# output the contents of $row as desired
}
[/code]
Copy linkTweet thisAlerts:
@ephmynusauthorApr 15.2005 — See.... you're so smart. Thanks a lot for the help. Just getting into MySQL I'm already seeing one of the big differences between it and say JS or PHP. That WHERE command is going to be outrageously useful... as long as I can remember to us it. ?
Copy linkTweet thisAlerts:
@NogDogApr 16.2005 — You'll find you use it all the time, it will be 2nd nature soon. ?
×

Success!

Help @ephmynus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,
)...