Jump to content

trying to retrieve data from table and put into a session variable, but can't


Recommended Posts

Hi, I'm trying to use this code below, I echo it out on intro.php page, and it shows blank.

 

$_SESSION['charname']=mysql_query("SELECT charname FROM users WHERE username='$username'");
//set character's name in a session. 

 

here is what im trying to echo

 

<?php echo $_SESSION['charname'];?>

to try to retrieve a character's name and put it into a session variable I can use throughout the game. Here is the page it is from, it doesn't work, any help greatly appreciated. Thanks. Derek

 

<?php

include("connect1.php");
session_start();

//mysql_error();

$_POST['email']='';
$_POST['password']='';

// ini_set('display_errors', 1);
//  error_reporting(E_ALL);   // GREAT SNIPPET FOR DEBUGGING OUTPUTS ERRORS TO SCREEN */

// this is the session declaration , one per page.
/// data is set up in the mysql lite table , rows are 
// record_id, int, 11, not null checked, default, null
//username, varchar, 20, default null
// password, varchar, 20 default, null
//Name, varchar, 20 default null

//$username='';//we just turned error reporting off to get rid of undeclared message 
//dont need this
////////////////////////////////////////
////////////////////////////////////////
// a while loop is used to loop through and display output, like a table info, etc.dynamic rows.
$username = $_POST['username'];
$password = $_POST['password'];




//echo "<pre>".print_r($_POST, 1)."</pre>";

//$hack = $_GET['hack']; /// we got the hack variable from other page with GET




// if logoff is set, destroy the session, or unset it.
if(isset($_GET['logoff'])){

    
unset($_SESSION['userid']);

//session_destroy();

$message = "You have been logged off"; // notice here that he used the same variable as before but just changed value


     

		}


/*if(isset($hack)){    

   $message = "Naughty Naughty!  "; // COOL

    }*/


// escape username and password for use in SQL//person said on board "looks fine" like this
//to prevent sql injections
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);



// if fields username and password have contents, then...
if($username && $password){



 //$password = md5($password); password not encrypted in database so exclude this

$query = mysql_query("SELECT * FROM users WHERE username = '$username' AND (password = '$password' )");

if(!mysql_num_rows($query))
{
$message = "You do not exist on the system";
}
else
{
$result = mysql_fetch_array($query); //creates array called result,//notice we dont need a while loop here.
                                          //if its found a user it will create a populated array, if find nothing, it creates a blank array.
                                        //the mysql_fetch_array automatically gives us our keys for us.
// if username is set, go on...username is a key for $result, and a field in the table.

	$message = "You have been logged in";
	// session is an array, a php defined word, becomes like a variable.which can be accessed on any page.
	// 'userid'here is like a variable. we are going to assign to $_SESSION whatever the $result array contains which is 'username' in this case.

	$_SESSION['userid'] = $result['Email'];

	$_SESSION['charname']=mysql_query("SELECT charname FROM users WHERE username='$username'");
//set character's name in a session. 

	 echo "<script type='text/javascript'>window.location='intro.php'</script>";
}



}





/////////BAN USER CODE/////////////


/*//this is the file that connect to sql
$s=$_SERVER["REMOTE_ADDR"];
//draws IP address of visitor
$ipbancheck="SELECT * from banip where IP='$s'";
$ipbancheck2=mysql_query($ipbancheck);
if (!$ipbancheck2) {
die("query error in $ipbancheck<br.".mysql_error());
}
if (mysql_num_rows($ipbancheck2) > 0)
{
header("Location:http://www.derekvanderven.com/hacker.html");
}
else
{
//put content you want unbanned users to see here
}*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Realm of the Sun Dragon</title>
<link rel="stylesheet" type="text/css" href="css/mainpage.css">
<style type="text/css">
<!--
body {
background-color: #000000;
}
.style7 {
font-family: "Times New Roman", Times, serif;
font-weight: bold;
}
.style8 {
font-size: larger;
color: gold;
}
-->
</style></head>

<body>

<div id="container">
<!-- ALL CENTERING DIV -->

<div align="center"> 
<div id="header"> <img src="images/images/header.jpg" alt="Realm of the Sun Dragon" /></div><div id="links"> <table align="center" cellpadding=3>
   <tr>
  <td style=" font-family: arial; font-size: 12px; ">
    <div align="center"><a href="whatis.html">What is ROSD?</a>
        <b>::</b>
      
        <a href="register_new.php">Create an Account</a>
        <b>::</b>
        <a target=_blank href="#">Forums</a>
        <b>::</b><a href="sendmail.php">Contact Us</a>
      
      </font></div></td>
</tr></table></div> 
<!--end header-->
<div id="leftnav">
<table  width="450" align="center" cellpadding=0  cellspacing=0><tr><td style="color: white;" align=center ><div align="center" class="style7">  
   <p><br />
     </p>
   <p class="style8">Sign In:</p>
</div></td>
</tr><tr><td style="padding: 5px;">
<div align="center">
         <table>
          <tr><td width="450"><table width="291" align=center>
        <form name="form1" action="checklogin.php" method="post">
          
          <tr>
            <td width="106" align=right class=small>User name:</td><td width="173" align=left><input class=text type=text
           name=myusername size=16 ></td></tr>
          <tr><td align=right class=small><div align="right">Password:</div></td><td align=left> 
            <input class=text type=password name=mypassword size=16>
          </td></tr>
          <tr><td colspan=2>                         
            <div align="center">
              <input class=button type=submit value="Log In" name=submit id=Submit>
              <br />
              <br />
             <p> If you've forgotten your password,  please <a href="passrecovery8.php">click here</a>. </p>              
          <p>Logging in constitutes agreement to abide by the <a href="policies.html">Policies of ROSD</a>.</p></td>
           </tr>
          </form>
          </table>
        
         </td></tr>
          </table>
  </div></td></tr><tr><td height=4> </td></tr></table>

</td>
<td valign=top><div align="center"><p>Support the Realm</p> </td>
<p align="center"><b> <p align="center">A friendly message from the Creator, Derek.</p> </b>
<p> As the number of ROSD players increases, so does the cost of hosting.  I very much want to keep the game free, so please consider supporting the development and maintenance of the Realms by donating (once you're logged in...) or picking up some groovy merchandise from our <a target="_blank" href="#">Store</a>.  With a donation of 10 dollars or more, you'll receive an in-game gift!  Check the donate page (accessible from your top menu after you've logged in) for details. </font></p>
<p align="center"><font face=Arial>Copyright © 2010 by <a href="http://derekvanderven.com"> Derek Van Derven</a></font></p>
</div>
    <div id="rightpic"> <img src="images/images/rightpic.jpg" alt="Realm of the Sun Dragon" />
    </div><!--end rightpic-->
    </div><!-- end center --> 
    </div><!--end container-->
</body>
</html>

A result resource cannot be serialized and hence cannot be stored within the $_SESSION array. It's likely you want to store the actual data anyway....

 

if ($result = mysql_query("SELECT charname FROM users WHERE username='$username'")) {
  if (mysql_num_rows($result)) {
    $_SESSION ['charname'] = mysql_result($result, 0);
  }
}

can I do this? what I did on this page, to choose a character name  and insert it into the database based on what their current username is in the session? Please any more help greatly appreciated. as in my editor it doesn't look like it's going to work based on the code colors and the red x in eclipse. lol. Thanks. Derek

 

<?php
include("connect1.php");

include('bouncer_final.php');

//include("bouncer.php"); // kicks the person off if session is not set, its the bouncer, big and fat man.


$_SESSION['player']['currentHP'] = 100;
$_SESSION['player']['damage']=10;
$_SESSION['player']['damageLow'] = 1;
$_SESSION['player']['damageHigh'] = 15;

/* End */

if(!empty($_POST['charname']) && !empty($_POST['charConfirm']) && !empty($_POST['submit']))
{

$charname = mysql_real_escape_string($_POST['charname']);
$charConfirm = mysql_real_escape_string($_POST['charConfirm']);


if($charname != $charConfirm)
{
	echo "<br>The two names did not match<br>";

}
else
{
// Check if the email already exists in database

	$query = "SELECT * FROM users WHERE charname = '$charname' ";
	$results = mysql_num_rows(mysql_query($query));

		if ($results > 0)
		{
			echo "sorry that name  already exists";

		}
		else
		{


			mysql_query("INSERT INTO users (charname)
			VALUES ('$charname') WHERE username='$_SESSION['username']")or die(mysql_error());  



			echo "success! you have been given the character name of ".$charname;
		}


}//else



}//if



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction</title>
<style type="text/css">
body {
background-color:#000000;
color: #ffffff;
}
a:link,a:hover,a:active,a:visited {
color: gold;
}
.style1 {font-family: "Times New Roman", Times, serif}
.style2 {color: #F4C25B}
.style3 {font-family: "Times New Roman", Times, serif; color: #F4C25B; }
.style4 {
color: gold;
font-weight: bold;
}
</style>
</head>

<body>


<div align="right">
   <table align="left" width="206" border="0">
     <tr>
       <td width="200"><span class="style4"><a href="index.php">BACK HOME</a></span></td>
     </tr>
   </table>
    
<strong><a href="choose_class.php">Go back to class choose</a></strong></div>

<a href="logout.php"><strong>LOG OFF</strong></a><br />
  <br />
  
<p align="center"><img src="dragon_images/glass_dragon_front_large.jpg" width="266" height="376" /></p>
<table align="center" width="700" border="0">
<form action="username.php" method="post> 
Please choose a character name <input type="text" name="charname"> 
<br /> 
Please enter the character name again<input type="text" name="charConfirm">
<br />
<input type="submit" name="submit" value="Submit name"></form>
</table>
<h2 align="center" class="style3"> </h2>
</body>
</html>

I tried this as well it doesn't work.

 

$username=$_SESSION['username'];
			mysql_query("INSERT INTO users (charname)
			VALUES ('$charname') WHERE 'username' = '$username'")or die(mysql_error());  

 

<?php
include("connect1.php");

include('bouncer_final.php');

//include("bouncer.php"); // kicks the person off if session is not set, its the bouncer, big and fat man.


$_SESSION['player']['currentHP'] = 100;
$_SESSION['player']['damage']=10;
$_SESSION['player']['damageLow'] = 1;
$_SESSION['player']['damageHigh'] = 15;

/* End */

if(!empty($_POST['charname']) && !empty($_POST['charConfirm']) && !empty($_POST['submit']))
{

$charname = mysql_real_escape_string($_POST['charname']);
$charConfirm = mysql_real_escape_string($_POST['charConfirm']);


if($charname != $charConfirm)
{
	echo "<br>The two names did not match<br>";

}
else
{
// Check if the email already exists in database

	$query = "SELECT * FROM users WHERE charname = '$charname' ";
	$results = mysql_num_rows(mysql_query($query));

		if ($results > 0)
		{
			echo "sorry that name  already exists";

		}
		else
		{

			$username=$_SESSION['username'];
			mysql_query("INSERT INTO users (charname)
			VALUES ('$charname') WHERE 'username' = '$username'")or die(mysql_error());  



			echo "success! you have been given the character name of ".$charname;
		}


}//else



}//if



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Introduction</title>
<style type="text/css">
body {
background-color:#000000;
color: #ffffff;
}
a:link,a:hover,a:active,a:visited {
color: gold;
}
.style1 {font-family: "Times New Roman", Times, serif}
.style2 {color: #F4C25B}
.style3 {font-family: "Times New Roman", Times, serif; color: #F4C25B; }
.style4 {
color: gold;
font-weight: bold;
}
</style>
</head>

<body>


<div align="right">
   <table align="left" width="206" border="0">
     <tr>
       <td width="200"><span class="style4"><a href="index.php">BACK HOME</a></span></td>
     </tr>
   </table>
    
<strong><a href="choose_class.php">Go back to class choose</a></strong></div>

<a href="logout.php"><strong>LOG OFF</strong></a><br />
  <br />
  
<p align="center"><img src="dragon_images/glass_dragon_front_large.jpg" width="266" height="376" /></p>
<table align="center" width="700" border="0">
<form action="username.php" method="post> 
Please choose a character name <input type="text" name="charname"> 
<br /> 
Please enter the character name again<input type="text" name="charConfirm">
<br />
<input type="submit" name="submit" value="Submit name"></form>
</table>
<h2 align="center" class="style3"> </h2>
</body>
</html>

I'm getting close. I'm using this code.

$username=$_SESSION['username'];

mysql_query("INSERT INTO users (charname)

VALUES ('$charname') WHERE username = '$username'")or die(mysql_error()); 

 

[/code]

 

but getting the following error

 

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 'WHERE username = ''' at line 2

 

any help greatly appreciated.

The error shows that $username is empty, indicating that $_SESSION['username'] is also empty, or unset. Is there a session_start(); at the head of this script?

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.