help with sessions please

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

timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

still with the original problem ...
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

Well I suggest that we keep trying tests until we pinpoint the problem. Try another if statement, this time using sessions as this might also be the problem;

Code: Select all

<?php

session_start();

$_SESSION['int'] = 1;
$_SESSION['string'] = "String";

$int = $_SESSION['int'];
$string = $_SESSION['string'];

if ($int == 1) {
echo "Saw the int";
} else {
echo "No int :(";
}

if ($string == "String") {
echo "Saw the string";
} else {
echo "No string :(";
}

?>
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help with sessions please

Post by Benjamin »

Note the missing =

Code: Select all

if ($string = "String") {
Should be:

Code: Select all

if ($string == "String") {
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

this one gave me

Parse error: syntax error, unexpected '/' in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 14
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: help with sessions please

Post by Benjamin »

Looks like the double quotes aren't escaped either.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

Benjamin wrote:Note the missing =

Code: Select all

if ($string = "String") {
Should be:

Code: Select all

if ($string == "String") {
I literally posted it and pressed edit as soon as it was posted noticing this one, you guys give me no time lol.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

So what do you reckon benjamin. How can I fix this
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

Code: Select all

echo "No int <img src="./images/smilies/icon_sad.gif" alt=":(" title="Sad" />";
}

if ($string == "String") {
echo "Saw the string";
} else {
echo "No string <img src="./images/smilies/icon_sad.gif" alt=":(" title="Sad" />";
This one wasn't my fault, i had a :( in the echo and phpBB posted all that junk. it was supposed to read echo "No string found :(";

I didn't review my posted code properly. ARRRGH ok try this;

Code: Select all

<?php

session_start();

$_SESSION['int'] = 1;
$_SESSION['string'] = "String";

$int = $_SESSION['int'];
$string = $_SESSION['string'];

if ($int == 1) {
echo "Saw the int.";
} else {
echo "No int found.";
}

if ($string == "String") {
echo "Saw the string.";
} else {
echo "No string found.";
}

?>
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

looks good :D

Saw the int.Saw the string.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

ok good. Try this (untested);

Code: Select all

<?php

session_start();

// I have put this equal to 'french' I assume that this is a valid username in rsusername, if not please change!
$_SESSION['string'] = "french";

$username = $_SESSION['string'];

$query = "SELECT userid FROM rsusername WHERE username='$username';";
$result = mysql_query($query);

if (!$result) {
echo "Got nothing";
} else {
echo "Got something";
}

?>
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

this is what came up - not sure if good or bad but no errors anyway!!
Got nothing

had to include at top:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

I made an adaption and included a recordset and "Got something"
However not without problems -c ame up with

Got something
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 26

line 26 is empty

complete code:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
<?php
mysql_select_db($database_recommendingpeople, $recommendingpeople);
$query_username = "SELECT * FROM username";
$username = mysql_query($query_username, $recommendingpeople) or die(mysql_error());
$row_username = mysql_fetch_assoc($username);
$totalRows_username = mysql_num_rows($username);


session_start();

// I have put this equal to 'french' I assume that this is a valid username in rsusername, if not please change!
$_SESSION['string'] = "french";

$username = $_SESSION['string'];

$query = "SELECT userid FROM username WHERE username='$username';";
$result = mysql_query($query);

if (!$result) {
echo "Got nothing";
} else {
echo "Got something";
}


mysql_free_result($username);
?>
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

timoteo wrote:had to include at top:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
Yes sorry I forgot about connections, good spot.
timoteo wrote: mysql_select_db($database_recommendingpeople, $recommendingpeople);
Is this not done in 'Connections/recommendingpeople.php'? lol.

Code: Select all

<?php

require_once('Connections/recommendingpeople.php');

mysql_select_db($database_recommendingpeople, $recommendingpeople);

session_start();

// I have put this equal to 'french' I assume that this is a valid username in rsusername, if not please change!
$_SESSION['string'] = "french";

$username = $_SESSION['string'];

$query = "SELECT userid FROM rsusername WHERE username='$username';";
$username = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($username);

if (!$result) {
echo "Got nothing";
} else {
echo $row['userid'];
}

?>
The reason you got the error was because you changed the variable $username after setting it to the query
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

"Got something"

OK This worked better!!:

Code: Select all

<?php require_once('Connections/recommendingpeople.php'); ?>
<?php
mysql_select_db($database_recommendingpeople, $recommendingpeople);
$query_username = "SELECT * FROM username";
$username = mysql_query($query_username, $recommendingpeople) or die(mysql_error());
$row_username = mysql_fetch_assoc($username);
$totalRows_username = mysql_num_rows($username);


session_start();

// I have put this equal to 'french' I assume that this is a valid username in rsusername, if not please change!
$_SESSION['string'] = "french";
mysql_select_db($database_recommendingpeople, $recommendingpeople);
$query_username = "SELECT * FROM username";
$username = mysql_query($query_username, $recommendingpeople) or die(mysql_error());
$row_username = mysql_fetch_assoc($username);
$totalRows_username = mysql_num_rows($username);


$username = $_SESSION['string'];

$query = "SELECT userid FROM username WHERE username='$username';";
$result = mysql_query($query);

if (!$result) {
echo "Got nothing";
} else {
echo "Got something";
}


?>
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

I think that your connection to the database was the problem. Try this now;

Code: Select all

<?php
require_once('Connections/recommendingpeople.php');

// I have put this equal to 'french' I assume that this is a valid username in rsusername, if not please change!
$username = "french";

mysql_select_db($database_recommendingpeople, $recommendingpeople);

$query = "SELECT * FROM username WHERE username='$username';";
$result = mysql_query($query, $recommendingpeople) or die(mysql_error());
$row = mysql_fetch_assoc($result);

if (!$result) {
echo "Got nothing";
} else {
echo $row['userid'];
}


?>
Post Reply