# Thread: Two queries at once?

1. ## Two queries at once?

how would i do both of these queries at once?
PHP Code:
``` SELECT * from links WHERE cat_id = '\$cat' LIMIT \$limitvalue, \$limit SELECT sum(ratings) as total from links_ratings WHERE id = '\$rate_row[id]'  ```
also, how would i take the sum from that query, and divide it by the number of entrys to get the average of the ratings?

2. From the looks of things you're not going to be able to do them both at the same time, but I can solve your average problem, change:

SELECT sum(ratings) as total from links_ratings WHERE id = '\$rate_row[id]'

to

SELECT sum(ratings) as total, avg(ratings) as average from links_ratings WHERE id = '\$rate_row[id]'

and you should now have the average returned as well as the sum.

3. well, i need to loop both of those queries so i can print out the links, and their ratings. here's the whole code so you can see what i mean
PHP Code:
``` <?     \$limit = 20;     \$columns = 3;     \$sqlcount= "SELECT * from links WHERE cat_id = '\$cat'";      \$sql_countresult = mysql_db_query(\$db_name, \$sqlcount, \$db_connect) or die(mysql_error());      \$totalrows = mysql_num_rows(\$sql_countresult) or die(mysql_error());      if(empty(\$page))     {      \$page = 1;      }      \$limitvalue = \$page*\$limit-(\$limit);      \$get_cat2 = "SELECT * from links_catagories WHERE cat_id = '\$cat'"; // Get Current Catagory     \$do_cat2 = mysql_db_query(\$db_name, \$get_cat2, \$db_connect);     \$get_row = mysql_fetch_array(\$do_cat2);     \$id_query = "SELECT * from links WHERE cat_id = '\$cat' LIMIT \$limitvalue, \$limit"; // Get link id for rating     \$get_id = mysql_query(\$id_query);     \$rate_row = mysql_fetch_array(\$get_id);     \$get_cat = "SELECT * from links WHERE cat_id = '\$cat' LIMIT \$limitvalue, \$limit"; // Select all links where \$cat = catagory id     \$do_cat = mysql_db_query(\$db_name, \$get_cat, \$db_connect); ?>      <td width="50%" colspan="2"><font face="verdana" size="5"><i><b><?=\$get_row[catagory]; ?></b></i></font></td>      <td width="50%" colspan="2"></td>     </tr>     <tr> <?     \$get_rate = "SELECT * from links_ratings WHERE id = '\$rate_row[id]'"; // get rating for links     \$do_rate = mysql_query(\$get_rate);     \$show_rate = mysql_fetch_array(\$do_rate);     while (\$result = mysql_fetch_array(\$do_cat))     {     ?>       <td width="20%" bgcolor="#520608"><font size="2" face="verdana"><b><i>       <a href="/links/jump.php?id=<?=\$result[id]; ?>" target="_blank"><?=\$result[url_name]; ?></a></i></b></font></td>       <td width="20%" bgcolor="#520608">       <form action="/links/ratings.php" method="POST">       <input type="hidden" value="<? echo "\$CHARSET_HTTP_METHOD" . "\$HTTP_HOST" . "\$REQUEST_URI"; ?>" name="back_value">       <input type="hidden" name="id" value="<?=\$result[id]; ?>">       <input type="Image" name="submit" src="/links/rate_it.jpg" border="0"></td></form>       <td width="20%" bgcolor="#520608"><font size="2" face="Verdana">Rating: <?=\$show_rate[rating]; ?></font></td>       <td width="20%" bgcolor="#520608"><font size="2" face="Verdana"><?=\$result[hits]; ?> hits</font></td>     </tr>     <tr>       <td colspan="4" width="100%"><font size="1">&nbsp;&nbsp;<?=\$result[description]; ?></td>      </tr>      <tr>      <?      } ?> ```
At this point it just gets the rating of the first link and prints the same thing out for each of the links.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•