Reputation: 205
I am doing a sql query inside php file but i don´t know what Im doing wrong in the query. Perhaps I don´t concatenate properly the sql statement or I don't use properly the quotes, somebody can help me? Thank you. Here is my code:
$config['table_name'] = "peliculas";
$config['table_namedos'] = "opiniones";
$sql = "SELECT ".$config['table_name']." id_pelicula ".$config['table_name']." nombre ".$config['table_name']." caratula ".$config['table_name']." duracion ".$config['table_namedos']." nick ".$config['table_namedos']." minuto "." INNER JOIN ".$config['table_namedos']." ON ".$config['table_name']." id_pelicula =".$config['table_namedos']." id_pelicula";
Upvotes: 0
Views: 99
Reputation: 44874
You need to seperate each selected columns with a comma in the query which you are missing and also while doing the concatanation you are giving some space after the colname. fieldname.
Also missing the from table name
so it should be as
$sql = "SELECT
".$config['table_name'].".id_pelicula,
".$config['table_name'].".nombre,
".$config['table_name'].".caratula,
".$config['table_name'].".duracion,
".$config['table_namedos'].".nick,
".$config['table_namedos'].".minuto from
".$config['table_name'].
" INNER JOIN ".$config['table_namedos']." ON ".$config['table_name'].".id_pelicula =".$config['table_namedos'].".id_pelicula";
Upvotes: 3
Reputation: 194
Try it this way You missed the dots between tablename and columnname, also you need FROM and you need to seperate columns by comma
<?php
$sql = "SELECT
{$config['table_name']}.id_pelicula,
{$config['table_name']}.nombre,
{$config['table_name']}.caratula,
{$config['table_name']}.duracion,
{$config['table_namedos']}.nick,
{$config['table_namedos']}.minuto
FROM
{$config['table_name']}
INNER JOIN
{$config['table_namedos']}
ON
{$config['table_name']}.id_pelicula = {$config['table_namedos']}.id_pelicula";
If you do not like the { }
syntax you can also do it this way
<?php
$t1 = $config['table_name'];
$t2 = $config['table_namedos'];
// or in one statement: list($t1, $t2) = array($config['table_name'], $config['table_namedos']);
$sql = "SELECT
$t1.id_pelicula,
$t1.nombre,
$t1.caratula,
$t1.duracion,
$t2.nick,
$t2.minuto
FROM
$t1
INNER JOIN
$t2
ON
$t1.id_pelicula = $t2.id_pelicula";
If you are really really fond of string .
concatenating, I advise using '
instead of "
and also leave spaces around the .
operator so you can see better where . is part of the String and where it is the operator.
Upvotes: 0
Reputation: 1
I have had the same problem and what I did was assign it to a string {ex: $stringname} then use the string in the query.
So $stringtablename = $config['table_name']; $sql = "SELECT ".$stringtablename; ect. I'm sure you get the point with out me writing the whole thing out. =)
Upvotes: -1