How to use Session in SELECT FROM

I'm confused right now.

I've been coding for some while now, to fix a problem, but I'm going blind on how to do, can anyone help me?

First, I have an datatable with all my customers in, then I click "USE" I should go to another page where I can make an case. This is all good, if I only should use one table, but in my project I need more then 1 table, so my question is:

$sql = "SELECT customers.cust_id, 
customers.customer_name, numberplate.car 
FROM customers , numberplate WHERE SESSION = numberplate.cust_id";

But I can't see how to do it, I know how to make a profilepage and so on, but this ting really tricks my brian right now. Anyone to help?

My session look like this:

     if (isset($_GET['cust_id']) && $_GET['cust_id'] != "") {
 $id = $_GET['cust_id'];
 } else {
 $cust_id = $_SESSION['cust_id'];
 }

Upvotes: 0

Views: 68

Answers (1)

Matthew Knight
Matthew Knight

Reputation: 631

You're confusing your SQL with PHP.

$_SESSION is the variable which contains PHP's session data. but your MYSQL statement: "WHERE SESSION = numberplate..." is referencing a column in the mysql table.

You want to use sql like:

SELECT customers.cust_id, 
customers.customer_name, numberplate.car 
FROM customers , numberplate WHERE numberplate.cust_id = ?";

and then bind the value of $_SESSION['cust_id'] to the database call.

But additionally, you're trying to JOIN two tables together without any details on how to do that...

so your SQL then becomes:

SELECT customers.cust_id, customers.customer_name, 
numberplate.car 
FROM customers,
JOIN numberplate ON customers.cust_id = numberplate.cust_id
WHERE numberplate.cust_id = ?

Upvotes: 1

Related Questions