koki
koki

Reputation: 35

Sql query on PHP problems?

Its hard to explain my problem and what I want to do, but if you read my code you will understand.

When I log in, I have $username as cookie and I call it on this page, and it work. Like this:

<?php 
if (isset($_COOKIE['username'])) 
{ 
    $username = $_COOKIE['username']; 
} 
?> 

Now I want to get the id of this $username, form the table users. So this is my query:

<?php 
$sql = "SELECT id FROM users WHERE username = '$username'"; 
$result2 = mysqli_query($mysqli, $sql); 
?>

And then I have this next query, where I'm trying to get results where $username id = schedules.id_doc which is a foreign key of the table users.

<?php 
$doc = "SELECT users.emri, users.mbiemri, schedules.visit_date, schedules.visit_time\n" 
    . "FROM schedules\n" 
    . "INNER JOIN users"; 
    . "WHERE schedules.id_doc = '$result2'"; 
$result1 = mysqli_query($mysqli, $doc); 
?> 

So this doesn't work and when I debug it it says that the problem is at . "WHERE schedules.id_doc = '$result2'";

I'm not sure if i can do this on this way, or it have another way.

Upvotes: 0

Views: 62

Answers (2)

Rigel1121
Rigel1121

Reputation: 2034

You should create an alias of your table and you've forgot the ON clause. Your query should be like this:

$doc = "SELECT users.emri, users.mbiemri, schedules.visit_date,     
        schedules.visit_time\n" 
       . "FROM schedules A \n" 
       . "INNER JOIN users B ON A.id_doc=B.id" 
       . "WHERE A.id_doc = '$result2'"; 

Or put it in one line code. See below:

$doc = "SELECT users.emri, users.mbiemri, schedules.visit_date, schedules.visit_time FROM schedules A INNER JOIN users B ON A.id_doc=B.id WHERE A.id_doc = '$result2'"; 

Upvotes: 0

Rodrigo Taboada
Rodrigo Taboada

Reputation: 2727

You have a ; in the line . "INNER JOIN users";.

If you remove the ; your code will work.

Also you don't need the \n at the end of the lines.

Upvotes: 2

Related Questions