Page 1 of 1

multiple sql queries within one php script?

Posted: Wed Mar 16, 2011 8:14 am
by Suneseren
pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Is it possible to execute more than one sql run in one php script/file?
Im trying to write a script that creates a reference list in the Harvard referencing style.
What i would like to do is letting my script use secondary sql queries in lookup tables. Script is below. It works fine apart from the piece with "sql2" in it. Is it possible to use multiple queries like this?

Best regards

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title></title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="../css/style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
<body><?php include("incl/connect.php"); ?>
<?php
		

		$sql = "SELECT * from reference order by authorstring";
		
		$result = mysql_query($sql);
		
		while( $row = mysql_fetch_assoc($result) ) {		
			$mediumcode = $row["mediumtype"];
		
			if ($mediumcode==1)	{
				$tempdate = utf8_encode($row["date"]);
				$year = substr($tempdate,0,4);
				$author = utf8_encode($row["authorstring"]);
				$title = utf8_encode($row["title"]);
				$edition = $row["edition"];
				$place = utf8_encode($row["place"]);
				$publisher = utf8_encode($row["publisher"]);
				
				echo $author."., ".$year.". <i>".$title."</i>. ";
				if (!($edition==1))	{
					echo $edition;
						if 	($edition==2)	{
							echo "<sup>nd</sup>";
						}
						if 	($edition==3)	{
							echo "<sup>rd</sup>";
						}
						if 	($edition>3)	{
							echo "<sup>th</sup>";
						}
					echo " ed.";
				}
				
				echo " ".$place.". ".$publisher.".<br><br>"    ;
				
			}
		
			if ($mediumcode==7)	{
				$tempdate = utf8_encode($row["date"]);
				$year = substr($tempdate,0,4);
				$author = utf8_encode($row["authorstring"]);
				$title = utf8_encode($row["title"]);
				//$journal = utf8_encode($row["journal"]);
				$jid = $row["journal"];
				$part = utf8_encode($row["part"]);
				$pages = utf8_encode($row["pages"]);
						
				echo $author." ".$year."., ".$title." <i>";
				
					$sql2 = "SELECT * FROM journal WHERE id=$jid";
					$result = mysql_query($sql2);
					while($row2 = mysql_fetch_assoc($result))	{
						$journal = utf8_encode($row2["journal"]);
						echo $journal;
					}
				
				echo "</i>, ".$part.", ";
				if (!(strpbrk($pages,'-')))	{
					echo "p.";
				}
				else	{
					echo "pp.";
				}
				echo $pages.".<br><br>";
			}
		}






?> 
<?php include("incl/mysql_close.php"); ?>
</body>
</html>

pickle | Please use [ syntax=php ], [ syntax=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: multiple sql queries within one php script?

Posted: Wed Mar 16, 2011 11:48 am
by Mini
Hey,

The second resource is overwriting the first one, rename $result to $result2 and it should work :).