4 Replies - 6633 Views - Last Post: 26 July 2008 - 09:11 AM Rate Topic: -----

#1 zura_84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 08

How to get data from multiple tables

Posted 26 July 2008 - 04:58 AM

dear sir,

i just want to ask how to get my data from another database let say i want to take s_title from medic_subject_offered to put together with another database so that when i debug i can make the s_title appear... because when i search by s_code..all the data will be insert but not the s_title... can you answered me as soon as possible.. thanks

<?php require_once('Connections/mas.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
  session_start();
}

// ** Logout the current user. **
$logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
  $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
  //to fully log out a visitor we need to clear the session varialbles
  $_SESSION['MM_Username'] = NULL;
  $_SESSION['MM_UserGroup'] = NULL;
  $_SESSION['PrevUrl'] = NULL;
  unset($_SESSION['MM_Username']);
  unset($_SESSION['MM_UserGroup']);
  unset($_SESSION['PrevUrl']);
	
  $logoutGoTo = "login.php";
  if ($logoutGoTo) {
	header("Location: $logoutGoTo");
	exit;
  }
}
?>
<?php
if (!isset($_SESSION)) {
  session_start();
}
$MM_authorizedUsers = "";
$MM_donotCheckaccess = "true";

// *** Restrict Access To Page: Grant or deny access to this page
function isAuthorized($strUsers, $strGroups, $UserName, $UserGroup) { 
  // For security, start by assuming the visitor is NOT authorized. 
  $isValid = False; 

  // When a visitor has logged into this site, the Session variable MM_Username set equal to their username. 
  // Therefore, we know that a user is NOT logged in if that Session variable is blank. 
  if (!empty($UserName)) { 
	// Besides being logged in, you may restrict access to only certain users based on an ID established when they login. 
	// Parse the strings into arrays. 
	$arrUsers = Explode(",", $strUsers); 
	$arrGroups = Explode(",", $strGroups); 
	if (in_array($UserName, $arrUsers)) { 
	  $isValid = true; 
	} 
	// Or, you may restrict access to only certain users based on their username. 
	if (in_array($UserGroup, $arrGroups)) { 
	  $isValid = true; 
	} 
	if (($strUsers == "") && true) { 
	  $isValid = true; 
	} 
  } 
  return $isValid; 
}

$MM_restrictGoTo = "login.php";
if (!((isset($_SESSION['MM_Username'])) && (isAuthorized("",$MM_authorizedUsers, $_SESSION['MM_Username'], $_SESSION['MM_UserGroup'])))) {   
  $MM_qsChar = "?";
  $MM_referrer = $_SERVER['PHP_SELF'];
  if (strpos($MM_restrictGoTo, "?")) $MM_qsChar = "&";
  if (isset($QUERY_STRING) && strlen($QUERY_STRING) > 0) 
  $MM_referrer .= "?" . $QUERY_STRING;
  $MM_restrictGoTo = $MM_restrictGoTo. $MM_qsChar . "accesscheck=" . urlencode($MM_referrer);
  header("Location: ". $MM_restrictGoTo); 
  exit;
}
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
	case "text":
	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	  break;	
	case "long":
	case "int":
	  $theValue = ($theValue != "") ? intval($theValue) : "NULL";
	  break;
	case "double":
	  $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
	  break;
	case "date":
	  $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
	  break;
	case "defined":
	  $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
	  break;
  }
  return $theValue;
}
}

mysql_select_db($database_mas, $mas);
$query_reg = "SELECT * FROM medic_block_reg";
$reg = mysql_query($query_reg, $mas) or die(mysql_error());
$row_reg = mysql_fetch_assoc($reg);
$totalRows_reg = mysql_num_rows($reg);

mysql_select_db($database_mas, $mas);
$query_name = "SELECT * FROM medic_student";
$name = mysql_query($query_name, $mas) or die(mysql_error());
$row_name = mysql_fetch_assoc($name);
$totalRows_name = mysql_num_rows($name);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>View Record | Medic Assessment System</title>
<style type="text/css">
<!--
.style1 {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
a:link {
	text-decoration: none;
}
a:visited {
	text-decoration: none;
}
a:hover {
	text-decoration: none;
}
a:active {
	text-decoration: none;
}
.style2 {color: #FFFFFF}
.style3 {color: #333333; }
.style4 {color: #000000}
.style6 {font-size: 14; }
-->
</style>
<script type="text/Javascript">
<!--
function MM_popupMsg(msg) { //v1.0
  alert(msg);
}
//-->
</script>
</head>

<body>
<form action="" method="post" name="form1" class="style1" id="form1">
  <table width="763" height="24" border="0" align="center">
	<tr>
	  <td bgcolor="#FFFFFF"><p align="center">&nbsp;</p>
		  <p align="center" class="style3"><strong>List of Registered Student</strong></p>
		  <p align="center" class="style3">S_Code: 
			<input type="text" name="s_code" id="s_code"  value="<?php echo $row_reg['s_code']; ?>"/>
		  </p>
		  <p align="center" class="style3">S_Title:
			<input type="text" name="title" id="title" value="<?php echo $row_a['title']; ?>" />
		  </p>
	<table width="621" border="1" align="center" bordercolor="#99CC00">
			<tr>
			  <td width="84" height="38" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Matric No. </div></td>
			  <td width="94" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Name</div></td>
			  <td width="60" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Sessi</div></td>
			  <td width="52" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Year</div></td>
			  <td width="58" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Block</div></td>
			  <td width="61" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Group</div></td>
			  <td width="63" bgcolor="#FFFFCC" class="style3"><div align="center" class="style6">Status</div></td>
			  <td width="97" bgcolor="#FFFFCC" class="style3">&nbsp;</td>
			</tr>
			<?php do { ?>
			  <tr>
				<td height="27" bgcolor="#CCFFCC" class="style3"><div align="center"> <?php echo $row_reg['matric_no']; ?> </div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['name']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['sessi']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['year']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['block']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['group']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center"><?php echo $row_reg['status']; ?></div></td>
				<td bgcolor="#CCFFCC" class="style3"><div align="center">
				  <input type="button" name="button" id="button" value="DELETE" />
				</div></td>
			  </tr>
			  <?php } while ($row_reg = mysql_fetch_assoc($reg)); ?>
		  </table>
		<p align="center"><span class="style2"><span class="style4"> <a href="reg_query.php">Back</a> | <a href="main.php">Module Menu</a> | </span></span><span class="style2"><a href="<?php echo $logoutAction ?>" onclick="MM_popupMsg('You\'re successfully logout from this system...')">Logout</a></span></p>
		<p>&nbsp;</p></td>
	</tr>
  </table>
  <p align="center">&nbsp;</p>
  <p align="center">&nbsp;</p>
  <p>&nbsp;</p>
</form>
<EMBED NAME='CS1210513108413' SRC='Windows Notify.wav' LOOP=false 
AUTOSTART=false MASTERSOUND HIDDEN=true WIDTH=0 HEIGHT=0></EMBED>
</body>
</html>
<?php
mysql_free_result($reg);

mysql_free_result($name);
?>



Is This A Good Question/Topic? 0
  • +

Replies To: How to get data from multiple tables

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: How to get data from multiple tables

Posted 26 July 2008 - 08:46 AM

If the data is in different databases you will have to do one query then close the database connection and open a new one up with the other database information then do another query to get the rest of the information that is missing.

If they are in different tables in the same database you can do some other, simpler stuff to get it working.
Was This Post Helpful? 0
  • +
  • -

#3 zura_84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-July 08

Re: How to get data from multiple tables

Posted 26 July 2008 - 08:56 AM

it is in same database but different table...
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1201
  • View blog
  • Posts: 7,309
  • Joined: 07-September 06

Re: How to get data from multiple tables

Posted 26 July 2008 - 09:09 AM

Okay, we can work with this (with more ease at least).

Here is an example of how to query 2+ tables at once (1 SQL statement)
SELECT table1.*, table 2.* FROM table1, table2 WHERE table1.conditional = somevalue


After that I believe you do everything the same other than when you are suing the values from the queries you need to do it like so:

$row['table1.TITLE'];
where TITLE is the field you want from table1.

Hope that helps.

<edit>
I also did a quick google search and found some more stuff on this, if you want to take a look:
http://www.google.co...+tables+at+once
</edit>

This post has been edited by BetaWar: 26 July 2008 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How to get data from multiple tables

Posted 26 July 2008 - 09:11 AM

The more efficient way would be to use a JOIN on the tables you're looking to query :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1