help with understanding sessions and practical/correct usage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

also i tried a bit of wingin it with calling some of the users other information to the page but no luck....this is
what i tried

Code: Select all

doDB();
while ($newArray = mysql_fetch_array($result))
{
	$street = $newArray['street'];
	$city= $newArray['city'];
	$state= $newArray['state'];
	$phone= $newArray['phone'];
}
$display_block .="
echo <<< eot $street,$city,$state<br>$phone eot;
</div>
is there another way i can do this?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Where does $result come from?
Why while() in this case?
but how do i put that into a link like something we have here on the boards?
login.php?logout=true
login.php checks wether there is a GET parameter logout(=true) and if there is performs the logout.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

volka wrote:Where does $result come from?
Why while() in this case?
:? i was just taking a guess as to how you would actually go about doing that...i
want able to find another way of drawing the information from the database
based on the logname :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

  1. Where does $result come from? You're using $result in mysql_fetch_array().
    Where do you assign a mysql result to it? http://de.php.net/language.variables.scope might have something to do with it.
  2. Why while() in this case?
    What's the purpose of while(), how does it work?
    How many records are there for each _SESSION[logname]?
    What would happen to $street,$city,$state,$phone?
    => Is there any sense in using a while loop in this case?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

ok....remember when i was trying to get the persons log in name to print to the screen after they logged in to show that they were logged in...what i did was i went to the original login script and saw this

Code: Select all

if($row)
         {
           $_SESSION['auth']="yes";
           $_SESSION['logname'] = mysqli_real_escape_string($cxn,$_POST['fusername']);
           header("Location: $next_program?user='.$user_name");
         }
i couldent figure out why php wasnt displaying the user log in name when i used the variable 'fusername' or the
variable '$user_name' after the user loged in so i tried 'logname' and it worked fine....so i wanted to attempt to
print the address city state and phone from that same user to the page...in another program i did something like this

Code: Select all

<?php
// open the connection
$conn = mysql_connect("localhost", "root", "yea....isuck");
// database in use
mysql_select_db("testDB",$conn);
// create the SQL statement
$sql = "SELECT * FROM testTable";
//execute the SQL statement
$result= mysql_query($sql,$conn) or die(mysql_error());
// go through each row and display the result
while($newArray = mysql_fetch_array($result)){
// give a name to the fields
$id = $newArray['id'];
$testField = $newArray['testField'];
// echo the results on screen
echo "the ID is $id and the text is $testField<br>";
}
?>
and i just got a pretty neat idea from the suggestion you gave me, my book, and this example....im gonna try it out and post back
Last edited by Obadiah on Fri Oct 06, 2006 9:22 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Those questions were for you; you should think about them. Try to answer them, each of them.
I suspect you don't know what these constructs really do, you didn't learn the basics. And that hinders your scripting.
Last edited by volka on Fri Oct 06, 2006 9:09 am, edited 1 time in total.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

lol...it didnt like my idea too much i got this as a message
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\Log_In\agent\index_new.php on line 80
and this is what i used as code

Code: Select all

doDB();//this is a small function used in the beginning of the script that opens up a connection to the database
$sql = "Select street, city, state, phone FROM customer WHERE user_name == {$_SESSION['logname']}";
$result = mysql_query($sql,$conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result))
{
	$street = $newArray['street'];
	$city= $newArray['city'];
	$state= $newArray['state'];
	$phone= $newArray['phone'];
}
$display_block .="
echo <<< eot $street,$city,$state<br>$phone eot;
i thought it a good idea to paste this in here also so u can see whats going on with the connection as well

Code: Select all

<?php
session_start();
function doDB()
{
	$conn = mysql_connect("localhost","root","yea...isuck") or die(mysql_error());
	mysql_select_db("customerdirectory",$conn) or die(mysql_error());
}
//ini_set('error_reporting', E_ALL); 
//ini_set('display_errors', 1);
shouldn't this work?
Last edited by Obadiah on Fri Oct 06, 2006 9:21 am, edited 1 time in total.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

volka wrote:Those questions were for you; you should think about them. Try to answer them, each of them.
I suspect you don't know what these constructs really do, you didn't learn the basics. And that hinders your scripting.
i agree....im learning them now though....reading as much as i can and asking as many questions as possible....unfortunately i didnt have the time i wanted to have when i started this project to learn the basics...i got a book that told me how to do a project i needed to get done for a deadline i have to meet....unfortunately there also....it didnt go in to how or why you use this or why you wouldent use that....and thats why im here...
firstly to try and get this project done and most importantly trying to understand all of the ingredients involved...i understand some of the concepts like a while loop will continually run as far as the statement evaluates to true or whatever the if clause and so on...just when it comes to database design my brain goes kaput...im learning though :wink:
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Obadiah wrote:lol...it didnt like my idea too much i got this as a message
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\xampp\htdocs\Log_In\agent\index_new.php on line 80
and this is what i used as code

Code: Select all

doDB();//this is a small function used in the beginning of the script that opens up a connection to the database
$sql = "Select street, city, state, phone FROM customer WHERE user_name == {$_SESSION['logname']}";
$result = mysql_query($sql,$conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result))
{
	$street = $newArray['street'];
	$city= $newArray['city'];
	$state= $newArray['state'];
	$phone= $newArray['phone'];
}
$display_block .="
echo <<< eot $street,$city,$state<br>$phone eot;
i thought it a good idea to paste this in here also so u can see whats going on with the connection as well

Code: Select all

<?php
session_start();
function doDB()
{
	$conn = mysql_connect("localhost","root","yea...isuck") or die(mysql_error());
	mysql_select_db("customerdirectory",$conn) or die(mysql_error());
}
//ini_set('error_reporting', E_ALL); 
//ini_set('display_errors', 1);
shouldn't this work?
can anyone tell or explain to me the reason for this error?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, http://de.php.net/language.variables.scope can.

Code: Select all

function doDB()
{
        // $conn is a local variable
        $conn = mysql_connect("localhost","root","yea...isuck") or die(mysql_error());
        mysql_select_db("customerdirectory",$conn) or die(mysql_error());
        // end of local scope, end of $conn
}
try

Code: Select all

function doDB()
{
        $conn = mysql_connect("localhost","root","yea...isuck") or die(mysql_error());
        mysql_select_db("customerdirectory",$conn) or die(mysql_error());
        return $conn;
} 

$conn = doDB();
$sql = "Select street, city, state, phone FROM customer WHERE user_name == {$_SESSION['logname']}"; 
...
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

aye....i didnt return the value...sorry bout that...what if i declared the connection as global would it have worked the same way....kinda like

Code: Select all

function doDB() 
{
        global $conn;
        $conn = mysql_connect("localhost","root","yea...isuck") or die(mysql_error()); 
        mysql_select_db("customerdirectory",$conn) or die(mysql_error()); 
        return $conn; 
}
i tried your suggestion and...i got this

Code: Select all

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 '== Obadiah' at line 1
i dont quite understand why....i was looking at what you had gave me as a suggestion as far as my query went and didnt see any difference(lol....i at least got that right :wink: )...what could be the problem now? it says at line one but there isnt anything there besides the opening php tag and the session

[edited]

its really not making sense now looking at the statement and the error its supposed to compare the name of the person logged in to user_name which is what logname is stored as in the database and pick up the info...i dont get it! :?
Last edited by Obadiah on Fri Oct 06, 2006 2:21 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

MySQL doesn't understand '==' syntax. Use one equal sign.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

Everah wrote:MySQL doesn't understand '==' syntax. Use one equal sign.
i thought you were supposed to use 2 when doing a comparison....but i changed it now it says this
Unknown column 'Obadiah' in 'where clause'
:?
all its supposed to be doing is comparing the user_name to the person logged in...am i doing this wrong?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Post your query. What MySQL is telling you right now is that you are searching on a field named 'Obadiah' and there is not field named that. I would guess it is how your query is built, so post it.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post by Obadiah »

this is what i have

Code: Select all

$conn = doDB();
$sql = "Select street, city, state, phone FROM customer WHERE user_name = {$_SESSION['logname']}";
$result = mysql_query($sql,$conn) or die(mysql_error());
while ($newArray = mysql_fetch_array($result))
{
	$street = $newArray['street'];
	$city= $newArray['city'];
	$state= $newArray['state'];
	$phone= $newArray['phone'];
}
the field in the database is that stores the 'logname' is user_name so i made a comparison(or tried to :lol: ) and its not liking it very much
Post Reply