gtrufitt Posted March 11, 2008 Share Posted March 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/ Share on other sites More sharing options...
mpharo Posted March 11, 2008 Share Posted March 11, 2008 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.... Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489485 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489502 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 Can anyone help with this? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489658 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Yes. You're calling $m before it's being defined. Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489678 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489688 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Try this: $hallmembers = "SELECT `f_name`, `l_name` FROM `user` WHERE `id` = '$userid'"; If that fails, you don't have a column called 'l_name' Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489703 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489712 Share on other sites More sharing options...
sasa Posted March 11, 2008 Share Posted March 11, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489730 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 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"; Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489732 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489754 Share on other sites More sharing options...
discomatt Posted March 11, 2008 Share Posted March 11, 2008 Line 53 is probably missing a semicolon. Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489761 Share on other sites More sharing options...
gtrufitt Posted March 11, 2008 Author Share Posted March 11, 2008 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-489876 Share on other sites More sharing options...
gtrufitt Posted March 13, 2008 Author Share Posted March 13, 2008 Anyone?Please Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-490940 Share on other sites More sharing options...
mpharo Posted March 14, 2008 Share Posted March 14, 2008 your gonna hafta post the entire code and not just sections of it.... Quote Link to comment https://forums.phpfreaks.com/topic/95612-need-help-with-displaying-query-array/#findComment-492326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.