Jump to content

Recommended Posts

Hi, my code is:

[code<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../main.css" />
</head>
<body>
<?php
mysql_connect("localhost", "admin", "admin") or die("Cannot connect to DB!");

mysql_select_db("padgate") or die("Cannot select DB!");

$hall = "SELECT id FROM userhall WHERE hallid = '6'";

$h = mysql_query($hall) or die(mysql_error()."<Br /><br /.".$hall); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.

$rowa = mysql_fetch_array($h);
$userid = $rowa['id'];

$hallmembers = "SELECT f_name, l_name FROM user WHERE id = '$userid'";

$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.

$countm = mysql_num_rows($m);

?>

<div id="center"> 
  <div id="logo"> <img src="../images/padcommlogo.png" alt="PadComm" /></div>
  <?php include("../php/login.php"); ?>
  <div id="date"> 
    <script language="JavaScript" type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write("Today's date is: ")
document.write(day + "/" + month + "/" + year)
//-->
</script>
  </div>
  <?php include("../php/navigation.php"); ?>
  <div id="content"> 
  <h1>Croft Hall</h1></p>
   <?php

if($countm == 0)
{
echo 'Apparently no one lives in Croft hall!' ;
}
else
{
while($row = mysql_fetch_array($m))
echo 'The following people live in Croft Hall! </p>';
echo $row['f_name'];
echo ' ';
echo $row['l_name'];
}
?>
  </div>
  <?php include("../php/footer.php"); ?>
</div>
</body>
</html>

 

There are three members linked to the hall ID 6.

 

The table 'userhall' has the columns 'id' - the users id and 'hallid' the hall id.

The table user has the colums 'id' - the users id and 'f_name' and 'l_name'.

 

The line "The following people live in Croft Hall!" displays but it does not display any of the members linked to the hall ID.

 

Thanks,

 

Gareth

Link to comment
https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/
Share on other sites

Hi, my code is:

[code<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../main.css" />
</head>
<body>
<?php
mysql_connect("localhost", "admin", "admin") or die("Cannot connect to DB!");

mysql_select_db("padgate") or die("Cannot select DB!");

$hall = "SELECT * FROM userhall WHERE hallid = '6'";

$h = mysql_query($hall) or die(mysql_error()."<Br /><br /.".$hall); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.

$rowa = mysql_fetch_array($h);
$userid = $rowa['id'];


?>

<div id="center"> 
  <div id="logo"> <img src="../images/padcommlogo.png" alt="PadComm" /></div>
  <?php include("../php/login.php"); ?>
  <div id="date"> 
    <script language="JavaScript" type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write("Today's date is: ")
document.write(day + "/" + month + "/" + year)
//-->
</script>
  </div>
  <?php include("../php/navigation.php"); ?>
  <div id="content"> 
  <h1>Croft Hall</h1></p>
   <?php

if(!$userid)
{
echo 'Apparently no one lives in Croft hall!' ;
}
else
{
while($row = mysql_fetch_array($m)) {


$hallmembers = "SELECT * FROM user WHERE id = '$userid'";

$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.

echo 'The following people live in Croft Hall! </p>';
echo $row['f_name'];
echo ' ';
echo $row['l_name'];

}

}
?>
  </div>
  <?php include("../php/footer.php"); ?>
</div>
</body>
</html>

 

There are three members linked to the hall ID 6.

 

The table 'userhall' has the columns 'id' - the users id and 'hallid' the hall id.

The table user has the colums 'id' - the users id and 'f_name' and 'l_name'.

 

The line "The following people live in Croft Hall!" displays but it does not display any of the members linked to the hall ID.

 

Thanks,

 

Gareth

 

The way you have it setup it will only loop through 1 result and not them all...try this...it is not tested but you should be able to get it to work from here....

Thanks for your help.

 

I now get this error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Inetpub\wwwroot\padgate2\hall\TMP3keoixklx4.php on line 48

 

The line is:

 

while($row = mysql_fetch_array($m)) {

 

is it because that line is before the query?

 

while($row = mysql_fetch_array($m)) {


$hallmembers = "SELECT f_name, l_name FROM user WHERE id = '$userid'";

$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers);

 

Cheers

Cheers, I managed to work that out but I cant work out how to display all the names, it only displays the first name so far..

 

$hallmembers = "SELECT * FROM user WHERE id = '$userid'";

$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers);

 

while($row = mysql_fetch_array($m)) 
{
echo $row['f_name'];
echo ' ';
echo $row['l_name'];

 

Thanks

try

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../main.css" />
</head>
<body>
<?php
mysql_connect("localhost", "admin", "admin") or die("Cannot connect to DB!");
mysql_select_db("padgate") or die("Cannot select DB!");
$hall = "SELECT * FROM userhall WHERE hallid = '6'";
$h = mysql_query($hall) or die(mysql_error()."<Br /><br /.".$hall); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.
//$rowa = mysql_fetch_array($h); //move this line3s down
//$userid = $rowa['id'];
?>

<div id="center"> 
  <div id="logo"> <img src="../images/padcommlogo.png" alt="PadComm" /></div>
  <?php include("../php/login.php"); ?>
  <div id="date"> 
    <script language="JavaScript" type="text/javascript">
    <!--
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    document.write("Today's date is: ")
    document.write(day + "/" + month + "/" + year)
    //-->
</script>
  </div>
<?php include("../php/navigation.php"); ?>
  <div id="content"> 
  <h1>Croft Hall</h1></p>
<?php
if(mysql_num_rows($h) < 1) {
echo 'Apparently no one lives in Croft hall!' ;
} else {
echo 'The following people live in Croft Hall! </p>';
while($rowa = mysql_fetch_array($h)) {
	//$rowa = mysql_fetch_array($h); //move in while condition
	//$userid = $rowa['id'];
	$hallmembers = "SELECT * FROM user WHERE id = '$userid'";
	$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.
	$row = mysql_fetch_array($m);
	//echo 'The following people live in Croft Hall! </p>'; this line move before while loop
	echo $row['f_name'];
	echo ' ';
	echo $row['l_name'],"<br />\n";
}
}
?>
  </div>
<?php include("../php/footer.php"); ?>
</div>
</body>
</html>

I had already tried that before, it still comes up with one result. I definately have the column 'l_name', it displays the 'f_name' and 'l_name' of only the first row but does not seem to loop to echo the other rows...

 

Oh, well, look at your query... you are only selecting where the id = a specific id... which i'm guessing is unique. Do this:

 

$hallmembers = "SELECT `f_name`, `l_name` FROM `user` WHERE 1";

I tried the code that sasa posted, the code is now:

 

<?php
mysql_connect("localhost", "admin", "admin") or die("Cannot connect to DB!");

mysql_select_db("padgate") or die("Cannot select DB!");

$hall_name = $_GET['hall'];

$hallid = "SELECT hallid FROM hall WHERE name = '$hall_name'";

$hi = mysql_query($hallid) or die(mysql_error()."<Br /><br /.".$hallid); // don't use your SQL statement in your error. 

$rowh = mysql_fetch_array($hi);
$hallid2 = $rowh['hallid'];

$hall = "SELECT id FROM userhall WHERE hallid = '$hallid2'";

$h = mysql_query($hall) or die(mysql_error()."<Br /><br /.".$hall); // don't use your SQL statement in your error. 


?> 

 

<?php

if (mysql_num_rows($h) == 0) {
echo 'Apparently no one lives in ' $hall_name ' hall!' ;
} else {
echo 'The following people live in ' $hall_name ' Hall! </p>';
while($rowa = mysql_fetch_array($h)) {
	$userid = $rowa['id'];
	$hallmembers = "SELECT * FROM user WHERE id = '$userid'";
	$m = mysql_query($hallmembers) or die(mysql_error()."<Br /><br /.".$hallmembers); // don't use your SQL statement in your error. It will let malicious users know your table structure and open it up to sql injection.
	$row = mysql_fetch_array($m);
	{
echo '<a href ="userprofile.php?user=';
echo $row['id'];
echo '">';
echo $row['f_name'];
echo ' ';
echo $row['l_name'],"</a><br />\n";

}

}
?>

 

But I now get the error:

 

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\Inetpub\wwwroot\padgate2\hallprofile.php on line 54

 

which is:

 

if (mysql_num_rows($h) == 0) {

 

Thanks.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.