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

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

Re: help with sessions please

Post by Neilos »

Did it work when on its own?

On its own it should be able to pull the id.

That error means that the server found a string variable (string being the data type of the form "This is a string") when it was expecting to find something else. Most likely I think that it will be expecting a variable containing a different datatype or an array. It could be a missing ; ) or }

And line 16 is blank?!!? Are you posting the code for the php file in the error, ie the content of profilesummary.php?

And yes, try to use php code tags lol, I hinted at it last time :wink:
Neilos wrote:If you get any problems then post the full code, in code tags lol, and copy paste any errors you get.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

Hi there , thanks for the reply. No it did not work on it's own. the error was the same. As far as I can see the error is from these rows:

[if ($numRows) {

if ($numRows == 1) {]

I tried many things but no luck.

userid is an auto increment integer. I wonder whats up.
cheers.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

Those lines of code work. I have tested the code on my server and it runs properly.

What version of php are you running?

Try this and let me know what you get, just run it on its own;

Code: Select all

<?php

session_start();

// Do your includes here
require_once('Connections/recommendingpeople.php');

// Check if the session variable is set
if (isset($_SESSION['MM_Username'])) {

	// Assign the session variable to a regular one, i prefer this lol
	$username = $_SESSION['MM_Username'];
	
	// echo the username to check if we are getting the correct username
	echo $username;
	
	// This is the query using the string stored in the variable $username
	$query= "SELECT userid FROM rsusername WHERE username='$username';";
	$result = mysql_query($query);
	
	// this counts the rows, false if none
	$numRows = mysql_num_rows($result);
	
	// run if statements to check we have stuff retrieved in the query, we echo the id if it is found or a string if 
	// either it isn't or many users are found
	if ($numRows) {
	
	         if ($numRows == 1) {
	
	         // create an array containing the result of the mysql query result
	         $array = mysql_fetch_array($result, MYSQL_ASSOC);
	
	                 // retrieve the userid from the array of the mysql query result
	                 $userid = $array['userid'];
	
	                 // assign the userid to the session variable
	                 $_SESSION['userid'] = $userid;
	
	                 // echo the session variable 'userid' for debugging purposes, you'll want to delete this eventually
	                 echo $_SESSION['userid'];
	
	         } elseif ($numRows > 1) {
	         // echo for debugging purposes, you'll want to delete this eventually
	         echo "Lots of rows were returned";
	         } else {
	         // echo for debugging purposes, you'll want to delete this eventually
	         echo "No rows were returned";
	         }
	
	} else {
	
	         // echo for debugging purposes, you'll want to delete this eventually
	         echo "No rows were returned";
	
	}

} else {

	// The session variable MM_Username was not set
	echo "Session MM_Username was not set.";

}

?>
Also I would like to reiterate a question...
Neilos wrote:And line 16 is blank?!!? Are you posting the code for the php file in the error, ie the content of profilesummary.php?
Any way try that code first.and tell me the errors. Then try this code and let me know any errors (there is one thing for you to change, the username, write one that appears in the table rsusername;

Code: Select all

<?php

session_start();

// Do your includes here
require_once('Connections/recommendingpeople.php');

// Assign the username !!!!!CHANGE THIS TO ONE THAT IS IN THE TABLE!!!!!
$username = "Username";

// echo the username to check if we are getting the correct username
echo $username;

// This is the query using the string stored in the variable $username
$query= "SELECT userid FROM rsusername WHERE username='$username';";
$result = mysql_query($query);

// this counts the rows, false if none
$numRows = mysql_num_rows($result);

// run if statements to check we have stuff retrieved in the query, we echo the id if it is found or a string if 
// either it isn't or many users are found
if ($numRows) {

         if ($numRows == 1) {

         // create an array containing the result of the mysql query result
         $array = mysql_fetch_array($result, MYSQL_ASSOC);

                 // retrieve the userid from the array of the mysql query result
                 $userid = $array['userid'];

                 // assign the userid to the session variable
                 $_SESSION['userid'] = $userid;

                 // echo the session variable 'userid' for debugging purposes, you'll want to delete this eventually
                 echo $_SESSION['userid'];

         } elseif ($numRows > 1) {
         // echo for debugging purposes, you'll want to delete this eventually
         echo "Lots of rows were returned";
         } else {
         // echo for debugging purposes, you'll want to delete this eventually
         echo "No rows were returned";
         }

} else {

         // echo for debugging purposes, you'll want to delete this eventually
         echo "No rows were returned";

}

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

Re: help with sessions please

Post by timoteo »

hi there,
I tried both these scripts - they both came up with errors. The first script on line 9 -" if (isset($_SESSION['MM_Username'])) { "
and the second with a username on line 18, which is empty - just with "//this counts the rows, false if none"
now that's really strange no?

Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/profilesummary.php on line 18
the first error was the same except line 9.
This happened exactly the same when I ran in my page and by itself.

And no I have not been copying my script under the error. Line numbers are in the right places.
I am using php version 3.2.4 on remote server
mysql version 5.0.91
dreamweaver mx2004 for mac

I appreciate your continued help. I am sure it has got to be something ridiculously silly lol. These impossible ones always are. lol
anyway thanks a lot
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

sorry got the version wrong -
that was phpmyadmin version3.2.4
It would appear to be php 5.2.5
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

sorry try again
I'm using php 5.2.11
that's for real...
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

Lol, yeah this one is a terror.

Ok so lets cut out all the unnecessary code, I guess you are familiar with how it should work by now. Basically we just want to get a users id from the table. Try this code on your server, don't forget to change the username, try it first with something that is in the db table;

Code: Select all

<?php

include('Connections/recommendingpeople.php');

// Assign the username !!!!!CHANGE THIS TO ONE THAT IS IN THE TABLE!!!!! 
// If it is not in the table you should get to where I put ####!!!#### that
$username = "Username";

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

if ($numRows) {

         if ($numRows == 1) {

         $array = mysql_fetch_array($result, MYSQL_ASSOC);

                 $userid = $array['userid'];

                 echo $userid;

         } elseif ($numRows > 1) {
         echo "Lots of rows were returned";
         } else {
         echo "No rows were returned and we are in the if statement, actually we should never reach here.";
         }

} else {

         // ####!!!####
         echo "No rows were returned";

}

?>
I have tried an equivalent, and it worked 100%. For reference the code I used is;

Code: Select all

<?php

include('connecttodatabase.php');

// Assign the username !!!!!CHANGE THIS TO ONE THAT IS IN THE TABLE!!!!! 
// If it is not in the table you should get to where I put ####!!!#### that
$username = "Neilos";

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

if ($numRows) {

         if ($numRows == 1) {

         $array = mysql_fetch_array($result, MYSQL_ASSOC);

                 $userid = $array['userid'];

                 echo $userid;

         } elseif ($numRows > 1) {
         echo "Lots of rows were returned";
         } else {
         echo "No rows were returned and we are in the if statement, actually we should never reach here.";
         }

} else {

         // ####!!!####
         echo "No rows were returned";

}

?>
I tried it with no username that matched, one username that matched and two usernames that matched, all parts of the code were functioning correctly with no errors or warnings.

If this doesn't work then I suggest checking all the table names, table field names, usernames etc... checking that they are all what you would expect, especially check for whitespace, we could actually of stripped this from any strings just to make sure but we didn't, maybe we will later lol.

I just had a thought also. When trying to access session variables, you have been checking that they are set first haven't you? I've just been assuming that they were set. However the code I want you to try now has no session so that'll not be a problem.
Neilos wrote: Also I would like to reiterate a question...
Neilos wrote:And line 16 is blank?!!? Are you posting the code for the php file in the error, ie the content of profilesummary.php?
Sorry I didn't explain this very well. What I mean is are you posting the code that is contained in the .php file that the error is pointing to? ie I wanted to make sure that the code I was seeing was the code from the file profilesummary.php. The reason is that it is weird that the error points to blank lines.

What are you writing the code in? notepad++? an IDE? And where is your development environment, is it an apache server on your pc or a server on the net?
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

Hi there,
quite a correspondence we have now...
anyway I tried and I am just getting to the same stumbling block.
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/profilesummary.php on line 13

This would be line 13:

Code: Select all

if ($numRows)  {
I have tried it on its own, and on the page where I have pasted it at the top of the page and also after the recordsets. Same result, same line error.
I am writing in dreamweaver and using a remote server. Here is a copy of the sql table structure:
checked for blank spaces, all seems ok as far as I can see.
Server: localhost Database: biomagn1_recom Table: username "InnoDB free: 22528 kB"

Field Type Collation Attributes Null Default Extra Action
userid int(11) No None auto_increment
username varchar(50) latin1_swedish_ci No None
password varchar(50) latin1_swedish_ci No None
firstname varchar(50) latin1_swedish_ci No None
lastname varchar(50) latin1_swedish_ci No None
email varchar(100) latin1_swedish_ci No None
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

I just don't get it:
this should work right?:

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

$username = "french";

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

if ($numRows) {

          if ($numRows == 1) {

          $array = mysql_fetch_array($result, MYSQL_ASSOC);

                  $userid = $array['userid'];

                  echo $userid;

          } elseif ($numRows > 1) {
          echo "Lots of rows were returned";
          } else {
          echo "No rows were returned and we are in the if statement, actually we should never reach here.";
          }

} else {

          // ####!!!####
          echo "No rows were returned";

}

?>
but it gives me this:
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/Untitled-2.php on line 17
(line 17 is

Code: Select all

if ($numRows == 1) {
)

when it should echo the userid of "french" which is "16"!!
Does that mean it is finding no rows?
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: help with sessions please

Post by kalpesh.mahida »

Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/Untitled-2.php on line 17
may be you forgot to add a semi-colon at end of php statement.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

where would it be missing one? I've tried various ';'
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

kalpesh.mahida wrote:
Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/Untitled-2.php on line 17
may be you forgot to add a semi-colon at end of php statement.
No, it's my code that is being copy pasted and I have run it and tested it and it works ok.

ok lets get back to basics. Copy and paste the code that I write into a file called test.php that is in public_html. I'll write a series of different scripts, each time the script runs ok, delete it and replace it with the next. Repeat this until you get an error and then tell me which one gives the error (I'll number them) and also post the error too.

REMEMBER to change the username to something appropriate.

Code: Select all

<?php

// This is test number 1

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

$username = $_SESSION['MM_Username'];

echo $username;

?>

Code: Select all

<?php

// This is test number 2

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

?>

Code: Select all

<?php

// This is test number 3

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

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

?>

Code: Select all

<?php

// This is test number 4

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

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

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

         echo $row['userid'];

}

?>

Code: Select all

<?php

// This is test number 5

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

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

?>

Code: Select all

<?php

// This is test number 6

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

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

if ($numRows) {

         echo "Some rows!";

} else {

         echo "No rows were returned.";

}

?>

Code: Select all

<?php

// This is test number 7

session_start();

// Change "Username" to a username in the database, remember to keep the quotes
$_SESSION['MM_Username'] = "Username";

include('Connections/recommendingpeople.php');

$username = $_SESSION['MM_Username'];

echo $username;

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

if ($numRows) {

         if ($numRows == 1) {

         $array = mysql_fetch_array($result, MYSQL_ASSOC);

          $userid = $array['userid'];

          echo $userid;

         } elseif ($numRows > 1) {
         echo "Lots of rows were returned";
         }

} else {

         echo "No rows were returned.";

}

?>
Ok so this adds small parts up until where we got to, lets see where we get to before we encounter errors.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

Re: help with sessions please

Post by timoteo »

Thanks a lot for the tests - here's what I got
-test 1, 2 and 3 all good - no errors
-test 4 error:
"Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 19"

(line 19 : while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

-test 5 error: (but did echo username)

"french
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 16"

line 16 : $query = "SELECT userid FROM username WHERE username='$username';";

Here I wasn't sure about whether to put a recordset or not (above ran as is) then I created a recordset and got this error:
"french
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"

-test 6 error:
"Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 20"
line 20 : "if ($numRows) {"

-test 7 error:
"Parse error: syntax error, unexpected T_STRING in /home/biomagn1/public_html/recommendingpeople.com/test.php on line 20"
line 20 : "if ($numRows) {"
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: help with sessions please

Post by Neilos »

ok so I ran test 4 and it worked ok. It seems that you are having trouble with if statements and while loops for some reason. I don't know enough about any settings to suggest you to try changing some, you may have some setting somewhere that affects this but I don't know.

I've done a quick google but can't see any answers to this right away.

So lets try a simple if statement to check that out, it works 100% on my server it should work for you;

Code: Select all

<?php

$value1 = "Hello ";
$value2 = "World!";

if ($value1 != $value2) {

echo $value1 . $value2;

}

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

Re: help with sessions please

Post by timoteo »

Hello World!

hello neilos. that worked fine!!!
Post Reply