Jump to content

Sessions and MySQL queries


el_nino

Recommended Posts

Hey...

 

what I am trying to do is use the information stored in a session to display all the information of the user who has just logged in

 

my script currently looks like...

	<?php
	session_start(); // Start the session.

// If no session value is present, redirect the user:
// Also validate the HTTP_USER_AGENT!
if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']) )) {
require_once ('login_functions.inc.php');
$url = absolute_url();
header("Location: $url");
exit();	
}
?>

<html>
<head>
	<title> My Profile</title>
</head>

<?php
	include ('includes\header.html');
?>	

<?php
	//require_once ('mysql_connect.php');

	//connect to your database
	mysql_connect("****","****","****"); //(host, username, password)

	//specify database
	mysql_select_db("****") or die("Unable to select database"); //select which database we're using

?>

<body>
<div align="center">
</br>
<?php
	// Print a customized message:
	echo "<p>You are logged in as <b>{$_SESSION['Name']}!</b></p>";
?>
<?php
//$query = "select * from members where Name = ['Name']"; 

//$query = "select * from members where Name = '$_SESSION['Name']'";

$query = "select * from members where Name = '" . $_SESSION['Name'] . "'";

//$numresults=mysql_query($query);
//$numrows=mysql_num_rows($numresults);

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Could not execute query");
  
  	echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Username</th> <th>DOB</th> <th>Team</th> <th>Email</th></tr>";
   // keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['Name'];
echo "</td><td>"; 
echo $row['Username'];
echo "</td><td>"; 
echo $row['DOB'];
echo "</td><td>"; 
echo $row['Team'];
echo "</td><td>"; 
echo $row['Email'];
echo "</td></tr>"; 
} 

echo "</table>";

?>

</br>
</div>
</body>

<?php
	include ('\includes\footer.html');
?>

</html>

 

the error i am getting is "Could not execute query"

 

el nino

Link to comment
https://forums.phpfreaks.com/topic/186708-sessions-and-mysql-queries/
Share on other sites

right, so after doing that i now get the following error:

 

Could not execute query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

and also get:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\view_profile.php:3) in C:\xampp\htdocs\view_profile.php on line 4

try this version:

   <?php
      session_start(); // Start the session.

   // If no session value is present, redirect the user:
   // Also validate the HTTP_USER_AGENT!
   if (!isset($_SESSION['agent']) OR ($_SESSION['agent'] != md5($_SERVER['HTTP_USER_AGENT']) )) {
   require_once ('login_functions.inc.php');
   $url = absolute_url();
   header("Location: $url");
   exit();   
   }
   ?>

<html>
   <head>
      <title> My Profile</title>
   </head>
   
   <?php
      include ('includes\header.html');
   ?>   
   
   <?php
      //require_once ('mysql_connect.php');
      
      //connect to your database
      mysql_connect("****","****","****"); //(host, username, password)

      //specify database
      mysql_select_db("****") or die("Unable to select database"); //select which database we're using
      
   ?>
   
<body>
   <div align="center">
   </br>
   <?php
      // Print a customized message:
      echo "<p>You are logged in as <b>{$_SESSION['Name']}!</b></p>";
   ?>
   <?php
   //$query = "select * from members where Name = ['Name']"; 
   
   //$query = "select * from members where Name = '$_SESSION['Name']'";

   $query = "SELECT * FROM `members` WHERE `Name` ='{$_SESSION['Name']}'";
   
//$numresults=mysql_query($query);
//$numrows=mysql_num_rows($numresults);

// get results
  $query .= " LIMIT $s,$limit;";
  echo $query;
  $result = mysql_query($query) or die("Could not execute query\n".mysql_error());
  
     echo "<table border='1'>";
   echo "<tr> <th>Name</th> <th>Username</th> <th>DOB</th> <th>Team</th> <th>Email</th></tr>";
   // keeps getting the next row until there are no more to get
   while($row = mysql_fetch_array( $result )) {
   // Print out the contents of each row into a table
   echo "<tr><td>"; 
   echo $row['Name'];
   echo "</td><td>"; 
   echo $row['Username'];
   echo "</td><td>"; 
   echo $row['DOB'];
   echo "</td><td>"; 
   echo $row['Team'];
   echo "</td><td>"; 
   echo $row['Email'];
   echo "</td></tr>"; 
} 

echo "</table>";

   ?>
   
   </br>
   </div>
</body>

   <?php
      include ('\includes\footer.html');
   ?>

</html>

it will output your query, so you can check it for errors.

i now get:

 

SELECT * FROM `members` WHERE `Name` ='Guest' LIMIT ,;Could not execute query You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

this looks like its correct in that the user that has just logged in is Guest and it is this users details that should be displayed

 

i cant seem to see what is wrong at line 1

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.