Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

What's wrong with the following $SQL: $sql = "SELECT * FROM teachers_table WHERE id = '$row.['teacherid']'";

Hi,

There is something wrong with the syntax on the following line of code I created.
$sql = "SELECT * FROM teachers_table WHERE id = '$row.['teacherid']'"; 

Open in new window


I have three tables. A teachers_table, a students_table and a favourites.  The favourites table consists of a unique id and a teachers id and a students id. The teachers id in the favourites table matches the teachers id in the teachers table and the students id in the favourites table matches the students id in the the students table.

So I started by using INNER JOIN to get the teacher ids which are contained in both teachers_table and favourites for the student id (of the student logged in).

$sql = "Select teacherid FROM favourites INNER JOIN teachers_table ON favourites.teacherid = teachers_table.id WHERE studentid = '$ids'";
$result = mysqli_query($db_connection, $sql); 		

while ($row=mysqli_fetch_assoc($result)) {
//echo $row['teacherid']."<br>";

Open in new window


This appeared to work, as the echo statement above correctly displayed the teacher ids in the favourites table of the student logged in.

My next step was to display more than just the teacher ids. I wanted to display all the information in the teachers_table associated with those ids:

            
            
$sql = "SELECT * FROM teachers_table WHERE id = '$row.['teacherid']'"; 
		$result_favs = mysqli_query($db_connection, $sql); 

Open in new window


However, the following error check showed that that syntax isn't correct.
            
            if($result_favs === false){
            echo"<b>Query failed!</b><br/>\n<b>Query:</b>{$result_favs_sql}<br />\n<b>Error:</b>".mysqli_error($db_connection);
            exit();
            }


It returned an error relating to the right syntax to use near 'teacherid']'' at line 1. I was trying to use the value I got from $row['teacherid']. (which I was able to echo and see displayed okay) to define (match) what comes out of teachers_table column id.  However, I'm not sure if I can use $row.['teacherid'].

Is there a correct way of doing this by amending the syntax a bit?

Many thanks for reading.  

Adam
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Hi Chris,

Yes - that was it. I needed the curly braces. I had previously removed the period but  then had a different error message. With the period removed and the curly braces, it works like a charm.

Many thanks again,

Adam
Avatar of Adam

ASKER

I can't award points anymore it seems? I guess they'll be awarded automatically or something?

Anyway, many thanks Chris for a really quick and accurate response.