Connecting to 2 Mysql Tables on a single php page

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

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can consolidate the first 2 queries you have into 1, and the three after that into 1. Since the query is the same for each set, you can bring the variable setting into one block like so:

Code: Select all

$sql = mysql_query($query) or die(mysql_error());
$categories = array();
$whatever = array();
while($row = mysql_fetch_assoc($sql))
{
  $categories[] = $row['category'];
  $whatever[] = $row['something else'];
}
ynoc
Forum Newbie
Posts: 3
Joined: Sat Oct 23, 2004 5:56 am

Post by ynoc »

try to avoid cross product queries
e.g.

Code: Select all

select t1.thing1, t2.thing2 from table1 as t1, table2 as t2 where .....
try to use joins instead, these are usually more efficient
devilgrendall
Forum Newbie
Posts: 17
Joined: Fri Sep 10, 2004 10:29 am
Location: Warwickshire
Contact:

Post by devilgrendall »

Thanks for your advice and help.

I solved it in the end by doing the multiple query.

Code: Select all

<?php
			include ("**");
			$link = mysql_connect($host, $user, $password) or die ("Could not connect.");
			$result = mysql_db_query($dbname,$sql,$link);
			$sql = mysql_query("SELECT * FROM table ORDER BY keyindex desc limit 5") OR die(mysql_error());
					while($row = mysql_fetch_array($sql, MYSQL_ASSOC))
					{
   				  $categories [] = $row['category'];
				  $articlenames [] = $row['articletitle'];
						}
			$sql = mysql_query("SELECT * FROM table2 ORDER BY keyindex desc limit 4") OR die(mysql_error());
					while($row = mysql_fetch_array($sql, MYSQL_ASSOC))
					{
   				  $bname [] = $row['buildingname'];
				  $count [] = $row['county'];
						}
						
			$rows = mysql_num_rows($sql);
			if($rows) {
			$message = "$rows records found";
			}
			else{
			echo "No records found";
			}			
			
			mysql_close($link);
?>
HTML BIT

Code: Select all

<?php echo $bname[$indexcount]; ?>
Etc etc

Thanks again


Jay
Post Reply