Jump to content

SELECT * FROM ... trouble


ReeceSayer

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/
Share on other sites

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>

 

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830383
Share on other sites

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...

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830387
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830391
Share on other sites

		<?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?

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830394
Share on other sites

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());

Link to comment
https://forums.phpfreaks.com/topic/157499-select-from-trouble/#findComment-830396
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.