Passing variables from php script to another

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Forum Newbie
Posts: 3
Joined: Thu Aug 03, 2006 6:52 am

Passing variables from php script to another

Post by cle13 »

Hi, I have a php script which searches for school name results in a database and displays them back as links to another php script which displays detailed information about the a particular school from the same table and database. Now the second php script is supposed to use a variable from the first php script. How can I send that variable. Please help.

Below are the two scripts in question.

search script.

Code: Select all

<title>School Search Results</title>
<h3>School Search Results</h3>
//create short variable names
       echo 'You have not entered search details please go back and try again';

$conn = mysql_connect('localhost','','pass');
if (!$conn)
       echo 'Error: could not connect to database. Please try again later.';
$query = "select * from SCHOOL where sch_name like '%".$search1."%' or type like '%".$search2."%' or year like '%".$search3."%'";
$result = mysql_query($query) or die (mysql_error()."<br>Couldn't execute query: $query");
$num_results = mysql_num_rows($result);

echo '<p>Search results found: '.$num_results.'</p>';

for ($i=0; $i < $num_results; $i++)
     $row = mysql_fetch_array($result);
     echo '<p>&nbsp;'.($i+1).'<a href=schselect.php>&nbsp;';
     echo htmlspecialchars(stripslashes($row['sch_name']));
     echo '</a><br>&nbsp;&nbsp;&nbsp;';
     echo stripslashes($row['type']);
     echo '<br>&nbsp;&nbsp;&nbsp;';
     echo stripslashes($row['year']);
     echo '</p>';

view details script

Code: Select all

mysql_connect("localhost", "","pass") or
    die ("Could not connect to database");
mysql_select_db("school") or
    die ("Could not select database");   
// define the SQL command
$query = "select * from school where sch_name = '$sch_name'";
// submit the query to the database
// make sure it worked!
if (!$res) {
echo mysql_error();
echo "<div align=\"center\">";
echo "<center>";
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"8\" width=\"98%\">";
echo "<tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $res )) {
   // Print out the contents of each row into a table
   echo "<tr>";
   echo "<td width=\"100%\" height=\"58\" colspan=\"2\"><strong><font size=\"7\">";
   echo $row['sch_name'];
   echo "</font></strong>";
   echo "</td>";
   echo "</tr>";
   echo "<tr>";
   echo " <td width=\"20%\"><font size=\"2\">YEAR OF ESTABLISHMENT</font>";
   echo "<font size=\"3\"><br>";
   echo $row['year'];
   echo "</font>";
   echo "<font size=\"2\">";
   echo "<br>";
   echo "<p><font size=\"2\">LOCATION</font><font size=\"4\"><br>";
   echo "</font><font size=\"2\">";
   echo $row['location'];
   echo "<br>";
   echo "<p><font size=\"2\">KIND</font><br>";
   echo "<font size=\"2\">";
   echo $row['kind'];
   echo "<br><br>";
   echo "<p><font size=\"3\">TYPE</font></p>";
    echo "<p>";
   echo $row['type'];
   echo "</p>";
   echo "<p>TEL:</p><p><font size=\"3\">";
   echo $row['phone'];
   echo "</font><br>";
   echo "<p>POSTAL ADDRESS:</p><p><font size=\"3\">";
   echo $row['address'];
   echo "</font><br>";
   echo "</td>";
   echo "<td width=\"80%\"><p><strong><font size=\"4\">Headteacher's Message</font></strong></p><p>";
   echo $row['ht_msg'];
   echo "</p></font></td></tr>";
echo "</table>";
echo "</center></div>"; 
echo "</table>"

Thanx in advance.

NB: Im new on the forum, and I'd preffer the neccesarry directions to than criticisims.
User avatar
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

If the one script does not call the other one directly, you can pass variables from one to the other via the session ($_SESSION vars).

You need to start a session in each script and pass the var by copying it e.g. $_SESSION['myvariable'] = $var.
The second script can then retrieve the variable by issuing $var = $_SESSION['myvariable'] .
Forum Newbie
Posts: 3
Joined: Thu Aug 03, 2006 6:52 am

Post by cle13 »

Thanx mate but I've already tried that and Im having trouble with it. Isn't there another way?
User avatar
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Sessions are the way you pass info in php... so if you need to have a variable across multiple scripts, you need to use sessions. just call session_start() at the beginning of every page (before any output) and you shouldn't have any problems.... what problems are you having?
Post Reply