how to use a session variable in a select statement

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

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

how to use a session variable in a select statement

Post by Tokunbo »

hello sirs,

Kindly, I need some help. At the top of my code, I have:

Code: Select all

<?php

//session start
session_start();
$userSid = session_id();

?>
I have a table that stores some data inputs from users, one of the fields inputted from my form is the session_id of the user. If I query my db with this, I see all the data:

Code: Select all

<?php
$query="SELECT * FROM my_table";
$result=mysql_query($query);
$num=mysql_numrows($result);

?>
I would want to query the DB so that it would show only the data entered by the current user, with reference to the session_id for each of his/her currently inputted data, coz for every line of data input, the session_id is stored.

Code: Select all

<?php
$query="SELECT * FROM my_table where UserSid= $userSid";
$result=mysql_query($query);
$num=mysql_numrows($result);
?>
UserSid is the column-name of where $userSid variables are stored in the table.

Is my above code correct? coz none of the input data displays.

pls, what is wrong with my above code.

thanks
Toks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to use a session variable in a select statement

Post by Celauran »

There's no code to actually iterate over the results, which is why it's not displaying anything. Also, you'll want quotes around $userSid since it's a string. Finally, the function is mysql_num_rows.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: how to use a session variable in a select statement

Post by Tokunbo »

@Celauran,

thanks for your quick response, could you be more specific on what I need to do,.

thanks
Toks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to use a session variable in a select statement

Post by Celauran »

Code: Select all

while ($row = mysql_fetch_assoc($result))
{
  // do something here
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: how to use a session variable in a select statement

Post by pickle »

You also need to quote $userID. Without the quotes, MySQL thinks it's a column name & will crap out. You should also run it through mysql_real_escape_string() so it becomes safe to put into a query.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: how to use a session variable in a select statement

Post by Tokunbo »

@Celauran,

where/how am I supposed to use your code?

do you mean $row is the returned number of rows?
while ($row = mysql_fetch_assoc($result))
{
// do something here
}
i also tried this:
<?php
$query="SELECT * FROM my_table";
$result=mysql_query($query);
$num=mysql_numrows($result);
while ($row = mysql_fetch_assoc($result))
{
// do something here
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to use a session variable in a select statement

Post by Celauran »

$row isn't the number of rows, it's the current row as an associative array.

Code: Select all

// do something here
indicates a comment. I don't know what you want to do with the results.
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: how to use a session variable in a select statement

Post by Tokunbo »

@Celauran
I kind of figured out what your previous code does after I echoed it out. So for example, here is my code together with your previous fetch assoc:

Code: Select all

<?php

$query="SELECT * FROM mytable";

$result=mysql_query($query);



$num=mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
    echo "<tr>
		<td>".$row['OrderID']."</td>
		<td>".$row['UserSessionID']."</td>
		<td>".$row['Product']."</td>
	</tr>";
}




?>


here is the result:
vt0ve9ddg8j4aftmll62tmthr0 Apples
vt0ve9ddg8j4aftmll62tmthr0 Garden Eggs
vt0ve9ddg8j4aftmll62tmthr0 Cabbage
vt0ve9ddg8j4aftmll62tmthr0 Pear (Corn)
vt0ve9ddg8j4aftmll62tmthr0 Tangerine
bgg9si4bt2leoamgs1jd5gghc2 Cherry
bgg9si4bt2leoamgs1jd5gghc2 Coconut
A user selects a product and then hits the submit button. Column-1 is the generated sessionID, column-2 is the selected product.

I used 2-browsers for my simulation as if there were 2-simultaneous users, and each has a different sessionID. What I want to do(hopefully) is to display to the user only the products he added during his session. If he doesnt select any product/hit the submit button, he should get nothing displayed to him.

For ex: from the above, user-1 should get a display (5 products from apples to tangerine) and then user-2 should only see two products: cherry and coconut.

So if the above is the data in my table, how do I query it so that it would display to the user only the products entered during his session.

regards
Tokunbo
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: how to use a session variable in a select statement

Post by Celauran »

You had it (more or less) right earlier:

Code: Select all

$query = "SELECT * FROM my_table where UserSid = '{$userSid}'"
Post Reply