Page 1 of 1
how to use a session variable in a select statement
Posted: Tue Nov 22, 2011 11:12 am
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
Re: how to use a session variable in a select statement
Posted: Tue Nov 22, 2011 11:16 am
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.
Re: how to use a session variable in a select statement
Posted: Tue Nov 22, 2011 11:35 am
by Tokunbo
@Celauran,
thanks for your quick response, could you be more specific on what I need to do,.
thanks
Toks
Re: how to use a session variable in a select statement
Posted: Tue Nov 22, 2011 11:39 am
by Celauran
Code: Select all
while ($row = mysql_fetch_assoc($result))
{
// do something here
}
Re: how to use a session variable in a select statement
Posted: Tue Nov 22, 2011 3:04 pm
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.
Re: how to use a session variable in a select statement
Posted: Thu Nov 24, 2011 9:04 am
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
}
?>
Re: how to use a session variable in a select statement
Posted: Thu Nov 24, 2011 10:15 am
by Celauran
$row isn't the number of rows, it's the current row as an associative array.
indicates a comment. I don't know what you want to do with the results.
Re: how to use a session variable in a select statement
Posted: Thu Nov 24, 2011 11:45 am
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
Re: how to use a session variable in a select statement
Posted: Thu Nov 24, 2011 11:48 am
by Celauran
You had it (more or less) right earlier:
Code: Select all
$query = "SELECT * FROM my_table where UserSid = '{$userSid}'"