ReeceSayer Posted May 9, 2009 Share Posted May 9, 2009 Trying to get information on an order for the username stored in the session... <?php require_once "header.php"; $username = $_SESSION['username']; $result = mysql_query("SELECT orderID, username, QtyHam, QtyCheese, QtyBLT FROM sandwich WHERE username='$username'"); echo $result; ?> <!-- end of php start of xhtml --> <!-- xhtml files require a DOCTYPE, then an html tag and then the head --> <!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" xml:lang="en" lang="en" > <!-- head defines Page Title, Character set and Stylesheet link --> <head> <title>Course Data</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link href="login.css" rel="stylesheet" type="text/css" /> </head> <!-- After the head, comes the body of your web page --> <body> <div class="container"> <!-- A Heading for the page --> <h1>Order List</h1> <!-- A table to display the data --> <table class="neat" border="2"> <tr> <th>orderID</th> <th>Qty Ham</th> <th>Qty Cheese</th> <th>Qty BLT</th> </tr> <!-- enclose some PHP code to collect the data from the table in the database --> <?php // LOOP through all the records in the database table while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['orderID'] . "</td>"; echo "<td>" . $row['QtyHam'] . "</td>"; echo "<td>" . $row['QtyCheese'] . "</td>"; echo "<td>" . $row['QtyBLT'] . "</td>"; echo "</tr>"; } // Free up the memory used for $result mysql_free_result($result); // Close the database connection mysql_close(); ?> <!-- Close the XHTML table --> </table> <p> </p> </div> <!-- End of container div --> <!-- And don't forget to close the body and html --> </body> </html> <?php require_once "footer.php"; ?> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/ Share on other sites More sharing options...
wildteen88 Posted May 9, 2009 Share Posted May 9, 2009 Before using $_SESSION vars make sure you call session_start. Is it in header.php? Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830382 Share on other sites More sharing options...
ReeceSayer Posted May 9, 2009 Author Share Posted May 9, 2009 yes header.php contains <?php // Start a session session_start(); ?> <!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" xml:lang="en" lang="en"> <head> <title>Door Step Sandwiches</title> <meta http-equiv="content-type" content="application/xhtml; charset=utf-8" /> <link href="login.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="container"> <div id="header"> <h1><CENTER>DSS Logo / Door Step Sandwiches Title</CENTER></h1> </div> Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830383 Share on other sites More sharing options...
wildteen88 Posted May 9, 2009 Share Posted May 9, 2009 Your code should work fine as it is then. Are you getting any errors? Whats happening now/supposed to do Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830384 Share on other sites More sharing options...
ReeceSayer Posted May 9, 2009 Author Share Posted May 9, 2009 right i think i needed to include a connection to my database so i added connection.php at the top... the result is i get an echo saying "Resource id #5" which i don't understand, followed by a blank table... and there are definately orders for my user... Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830387 Share on other sites More sharing options...
wildteen88 Posted May 9, 2009 Share Posted May 9, 2009 right i think i needed to include a connection to my database so i added connection.php at the top Yea you'll need to connect to mysql in order for your queries to work (I didnt check that earlier) ... the result is i get an echo saying "Resource id #5" which i don't understand, followed by a blank table... and there are definately orders for my user... That because mysql_query() returns nothing else but a result resource. To grab the results from your query you'd use on of the mysql_fetch_(row, array, assoc or object) functions. Before using either of the above functions you should make sure your query returned any results. You can do this using mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830391 Share on other sites More sharing options...
ReeceSayer Posted May 9, 2009 Author Share Posted May 9, 2009 <?php // LOOP through all the records in the database table while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['orderID'] . "</td>"; echo "<td>" . $row['QtyHam'] . "</td>"; echo "<td>" . $row['QtyCheese'] . "</td>"; echo "<td>" . $row['QtyBLT'] . "</td>"; echo "</tr>"; } // Free up the memory used for $result mysql_free_result($result); // Close the database connection mysql_close(); ?> isn't that the code which should show the results in my table? Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830394 Share on other sites More sharing options...
wildteen88 Posted May 9, 2009 Share Posted May 9, 2009 Yes. However if nothing is being displayed it means nothing has been returned from your query. In which case you'll need to debug it. Change $username = $_SESSION['username']; $result = mysql_query("SELECT orderID, username, QtyHam, QtyCheese, QtyBLT FROM sandwich WHERE username='$username'"); to $username = $_SESSION['username']; $sql = "SELECT orderID, username, QtyHam, QtyCheese, QtyBLT FROM sandwich WHERE username='$username'"; $result = mysql_query($sql) or die('MySQL Error!<br />Query: '.htmlentities($sql).'<br />'.mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830396 Share on other sites More sharing options...
ReeceSayer Posted May 9, 2009 Author Share Posted May 9, 2009 Yep thats sorted it Thanks alot for your help i owe ya one Quote Link to comment https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830400 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.