Looking up column values for an array of tables 
June 16th, 2009, 07:27 PM
| | Newbie | | Join Date: May 2009
Posts: 21
| | |
Hello,
In PHP, I have an array called $table_list[]. It contains a list of MySQL table names. Each one of these tables has a column called "site" and one called "votes_up."
For each table in $table_list[], I would like to look up the value of "votes_up" when "site"=$entry.
How could I do this using PHP and MySQL?
Thanks in advance,
John
| 
June 16th, 2009, 07:41 PM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,701
Provided Answers: 4 | | | re: Looking up column values for an array of tables
Hi.
Check out the foreach loop in the manual.
The idea is: loop though your array and execute a query for each of your tables while collecting the results into a second array (or just printing them, whichever works for you.)
Try it out. If you run into any problems, show us what you've done and we'll help.
Last edited by Atli; June 16th, 2009 at 07:42 PM.
Reason: Spelling
| 
June 16th, 2009, 11:15 PM
| | Newbie | | Join Date: May 2009
Posts: 21
| | | re: Looking up column values for an array of tables
The code below is close. What it does is return the following: Quote:
Table Name 1
Table Name 2
Table Name 3
Table Name 4
"$entry": "votes_up for $entry from Table Name 4"
| What I want is: Quote:
Table Name 1: "votes_up for $entry from Table Name 1"
Table Name 2: "votes_up for $entry from Table Name 2"
Table Name 3: "votes_up for $entry from Table Name 3"
Table Name 4: "votes_up for $entry from Table Name 4"
| How could I change the code to make it return what I want?
Thanks,
John - $result = mysql_query("SHOW TABLES FROM feather")
-
or die(mysql_error());
-
-
while(list($table)= mysql_fetch_row($result))
-
{
-
$sqlA = "SELECT `site`,votes_up FROM `$table` WHERE `site` LIKE '$entry'";
-
$resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
-
if(mysql_num_rows($resA) > 0)
-
{
-
$table_list[] = $table;
-
while($rowA = mysql_fetch_assoc($resA))
-
{
-
$votes_up[$rowA["site"]] = $rowA["votes_up"];
-
}
-
}
-
}
-
-
foreach( $table_list as $key => $value){
-
echo "$value <br />";
-
}
-
-
foreach($votes_up as $site => $vote_up)
-
{
-
echo "$site: $vote_up";
-
}
| 
June 17th, 2009, 01:48 AM
| | Newbie | | Join Date: May 2009
Posts: 21
| | | re: Looking up column values for an array of tables
Nevermind.
This gives me what I want: - $result = mysql_query("SHOW TABLES FROM feather")
-
or die(mysql_error());
-
-
while(list($table)= mysql_fetch_row($result))
-
{
-
$sqlA = "SELECT COUNT(*) FROM `$table` WHERE `site` LIKE '$entry'";
-
$resA = mysql_query($sqlA) or die("$sqlA:".mysql_error());
-
list($isThere) = mysql_fetch_row($resA);
-
if ($isThere)
-
{
-
$table_list[] = $table;
-
}
-
}
-
-
-
foreach ($table_list as $table) {
-
$sql = "SELECT votes_up FROM `$table` WHERE `site` LIKE '$entry'";
-
$sql1 = mysql_query($sql) or die("$sql:".mysql_error());
-
while ($row = mysql_fetch_assoc($sql1)) {
-
echo $table . ': "' . $row['votes_up'] . " for $entry from $table\"<br />";
-
}
-
}
| 
June 17th, 2009, 08:08 AM
|  | Moderator | | Join Date: Nov 2006 Location: Iceland
Posts: 3,701
Provided Answers: 4 | | | re: Looking up column values for an array of tables
I'm glad you found a solution :)
Post again in you run into any other problems you need help with!
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|