简体   繁体   中英

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'"; . "WHERE schedules.id_doc = '$result2'";

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

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

If you remove the ; your code will work.

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

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'"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM