0

I have 2 tables one called users and one called tv shows. Im storing the name of the user in a variable called username by doing. The users table holds the user_id PK, username, password and the tv shws table stores the tv_id PK, user_id FK, TV Show Name

$username=$_SESSION['username'];

i want to be able to display all the tv shows for the specific user that has logged in and im guessing i would need to show all the results for the user id assigned to the user that has logged in because the user_id in the tv shows table is a foreign key of the primary key user id in the users table.

Code:

$user = "SELECT user_id FROM users where username='$username'";

if(!$rs = mysql_query("SELECT * FROM tv shows WHERE user_id='$user'")) {

When i run this code i get "cannot select table"

9
  • SELECT user_id FROM users where username='$username' JOIN tv shows ON user_id_id='$user' ? Commented Nov 30, 2012 at 12:25
  • This does not work. Cannot select table. Commented Nov 30, 2012 at 12:29
  • SELECT user_id FROM users JOIN tv shows ON user_id_id='$user' where username='$username' and now ? Commented Nov 30, 2012 at 12:32
  • Notice: Undefined variable: user Commented Nov 30, 2012 at 12:36
  • Well you have to have variable value to get this query to work. Check why $user is not defined and what is the value of it, and put user in curly braces {$user} Commented Nov 30, 2012 at 12:40

5 Answers 5

1

Try this

$query = 'SELECT * FROM tv_shows where user_id=(SELECT user_id FROM users where username="'.$username.'")';
Sign up to request clarification or add additional context in comments.

7 Comments

any error did you get?and can you please provide the sample data so we can figure it out what is wrong?And please check hat which username you try to log in is presented on database or not
cwstudios.co.uk/code.txt there is my code now with your query i cant seem to select the table.
Where is your code where you storing the value of username to $_SESSION
$username=$_SESSION['username']; that? the username variable works because it says logged in as John. when you are logged in.
@ChristopherWard Sorry just a mistake in my code i have updated my answer please paste the updated query and see if it works
|
1

okay ,try this:

<?php
$user = musql_query("SELECT * FROM users where username='$username'");
$result = mysql_fetch_array($user);
$userid = $result['user_id'];

$sql = "SELECT * FROM tv shows WHERE user_id=".$userid;
$get_tv = mysql_query($sql);
$make_array = mysql_fetch_assoc($get_tv);
 print_r($make_array);
?>

Happy coding!!

Comments

0

First of all, as a better design, you can have user_id in your $_SESSION.. so that you can avoid unnecessary query...

Here the problem could be due to Single quote ... So please escape your Single quote ..

Thanks

1 Comment

Now it says "no records found"
0
SELECT username,user_id 
FROM users as a 
JOIN tv shows as b ON b.user_id_id=a.user_id 
WHERE a.username='{$username}'

Comments

0

managed to figure it out myself with help from Tornado

if(!$rs = mysql_query("$query2")) {
$query2 = 'SELECT * FROM tv_shows where user_id=(SELECT user_id FROM users where username="'.$username.'")';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.