I'm trying to update multiple joined tables in codeigniter but it is giving the following error:
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '.carro_id INNER JOIN a ON ac.atributo_id = a.id INNER JOIN
m ON c.`modelo_' at line 1
UPDATE `carro c, atributo_carro ac, atributos a, modelos m, marcas ma
INNER JOIN ac ON c`.`id = ac`.`carro_id INNER JOIN a ON
ac`.`atributo_id = a`.`id INNER JOIN m ON c`.`modelo_id = m`.`id INNER
JOIN ma ON m`.`marca_id =` `ma`.`id` SET `marcas`.`marca` = 'Opel',
`modelos`.`modelo` = 'AMmmmmm', `carro`.`kilometragem` = '12000',
`carro`.`cor` = 'Azul', `carro`.`ano` = '1992', `carro`.`descricao` =
'dfghjk', `carro`.`valor` = '1288.00', `atributos`.`atributo` =
'rapido' WHERE `carro`.`id` = '10'
This is my function to update.
public function update_carro_query($id, $data)
{
// print_r($data); die();
$query =
$this->db->set('marcas.marca', $data['marca']);
$this->db->set('modelos.modelo', $data['modelo']);
$this->db->set('carro.kilometragem', $data['kilometragem']);
$this->db->set('carro.cor', $data['cor']);
$this->db->set('carro.ano', $data['ano']);
$this->db->set('carro.descricao', $data['descricao']);
$this->db->set('carro.valor', $data['valor']);
$this->db->set('atributos.atributo', $data['atributo']);
$this->db->where('carro.id', $id);
$this->db->update('carro c, atributo_carro ac, atributos a, modelos m, marcas ma INNER JOIN ac ON c.id = ac.carro_id INNER JOIN a ON ac.atributo_id = a.id INNER JOIN m ON c.modelo_id = m.id INNER JOIN ma ON m.marca_id = ma.id');
return $query;
}
This is my database tables and relationships
The query is generated by the function. What can be causing the syntax error? Am i missing something?
The update() method only expects a table name as first parameter, not a complete statement including a join and more. So it'll wrap the first parameter into backticks. This leads to the sql syntax error you have.
Better use query() to generate your (not straightforward) update statement.
Related
I have an internal intranet made in PHP and we had the Oracle server here, but it has been moved to an cloud server. Since that I'm having an issue.
If I execute a simple query from PHP like "select * from empresas" it executes normal, but if I try a more complicated query, with some joins and where clause, it gives the error ORA-03113. Trying to execute from PL/SQL Developer, this specific query takes something like 5 seconds to execute, it returns something like 15 rows only.
I have no acces to the Oracle logs, because this cloud is just one instance to us.
I think that maybe is a PDO configuration or maybe version that I need to configure/check to solve this problem, just don't know how to start.
The old Oracle server was a Windows server and the Oracle version was 12. The cloud is Oracle 11 and is a Linux server.
Here is the query:
`SELECT distinct
ct.descricao MARCA,
e.nome EMPRESA,
vi.cod_item CODIGO,
i.descricao DESCRICAO,
igi.cod_grupo_interno CODTIPO,
igi.descricao TIPO,
ic.cod_curva CLASSEABC,
v.cod_cliente COD_CLIENTE,
cd.nome NOME_CLIENTE,
ci.descricao CID_CLIENTE,
ci.uf UF_CLIENTE,
CASE WHEN cd.cpf<>' ' THEN 'F' ELSE 'J' END TIPO_CLIENTE,
cl.descricao DESCTP_CLIENTE,
SUM(vi.qtde) QTDE,
vi.preco_unitario PPUBLICO,
SUM(vi.qtde * vi.preco_unitario) FAT_BRT,
SUM(vi.TOTAL_DESCONTOS) DESCONTO,
SUM(vi.preco_liquido_final) FAT_LIQ,
SUM(vi.qtde * vi.preco_contabil) CUSTO,
SUM(vi.base_icms) BASE_ICMS,
SUM(vi.valor_icms) VLR_ICMS,
'0' VLR_COP,
'0' ACRESCIMO,
SUM(vi.valor_pis + vi.valor_cofins) VLR_PCOF,
SUM(vi.valor_ipi) VLR_IPI,
NULL VLR_MARGEM,
v.controle|| '-' || v.serie NF,
TO_CHAR(v.emissao, 'DD/MM/YYYY') DTNF,
n.cod_natureza CODNATUREZA,
n.descricao NATUREZA,
eu.id_funcionario VENDEDOR,
eu.nome_completo FUNCIONARIO,
eu.nome LOGIN,
eu.cod_funcao TIPO_FUN,
'N' STATUS,
v.numero_os OS,
v.chassi_resumido VEICULO,
cd.bairro BAIRRO_CLIENTE,
v.controle|| '-' || v.serie NOTA,
pg.descricao CONDICAO_PG,
pg.cod_forma_pgto COD_CONDICAO,
os.tipo CD_TP_OS,
ost.descricao DS_TP_OS,
'0' PERCUSTOOPER,
ed.cod_empresa_departamento DEPARTAMENTO,
ic.custo_fornecedor VLRREPOSICAO
FROM vendas v
INNER JOIN venda_itens vi ON vi.cod_empresa = v.cod_empresa and vi.controle = v.controle
INNER JOIN itens_custos ic ON ic.cod_empresa = v.cod_empresa AND ic.cod_item = vi.cod_item AND ic.cod_fornecedor = vi.cod_fornecedor
INNER JOIN itens_fornecedor if ON if.cod_item = vi.cod_item AND if.cod_fornecedor = vi.cod_fornecedor
INNER JOIN fornecedor_estoque fe ON fe.cod_fornecedor = if.cod_fornecedor
INNER JOIN itens i ON i.cod_item = vi.cod_item
INNER JOIN itens_grupo_interno igi ON igi.cod_grupo_interno = i.cod_grupo_interno
INNER JOIN empresas e ON e.cod_empresa = v.cod_empresa
INNER JOIN empresas_usuarios eu ON eu.nome = v.vendedor
INNER JOIN empresas_funcoes ef ON ef.cod_funcao = eu.cod_funcao
INNER JOIN empresas_departamentos ed ON ed.cod_empresa = v.cod_empresa AND ed.cod_empresa_departamento = v.cod_empresa_departamento
INNER JOIN parm_sys p ON p.cod_empresa = v.cod_empresa
INNER JOIN concessionaria_tipo ct ON ct.cod_tipo_concessionaria = p.tipo_concessionaria
INNER JOIN cliente_diverso cd ON cd.cod_cliente = v.cod_cliente AND cd.cod_empresa = v.cod_empresa
INNER JOIN clientes_tipo cl ON cl.cod_tipo = cd.cod_tipo
INNER JOIN cidades ci ON ci.cod_cidades = cd.cod_cidades
LEFT JOIN pagamento_venda pv ON pv.controle = v.controle AND pv.serie = v.serie
LEFT JOIN forma_pgto pg ON pg.cod_forma_pgto = pv.cod_forma_pgto
LEFT JOIN os ON os.cod_empresa = v.cod_empresa and os.numero_os = v.numero_os
LEFT JOIN os_tipos ost ON ost.tipo = os.tipo
LEFT JOIN natureza n ON n.cod_natureza = vi.cod_natureza and n.grupo = vi.grupo
LEFT JOIN operacoes ops ON ops.cod_empresa = v.cod_empresa and ops.cod_operacao = v.cod_operacao
WHERE v.status <> '1' AND v.total_produtos > 0
AND (ops.grupo is null or ops.grupo = 2)
AND v.emissao >= to_date(SYSDATE-20)
AND i.COD_GRUPO_INTERNO <> 13
GROUP BY ct.descricao, e.nome, vi.cod_item, i.descricao, igi.cod_grupo_interno, igi.descricao, ic.cod_curva, v.cod_cliente,
cd.nome, ci.descricao, ci.uf, CASE WHEN cd.cpf<>' ' THEN 'F' ELSE 'J' END, cl.descricao, vi.preco_unitario, vi.preco_contabil,
v.margem, v.controle|| '-' || v.serie, TO_CHAR(v.emissao, 'DD/MM/YYYY'), n.cod_natureza, n.descricao, eu.id_funcionario,
eu.nome_completo, eu.nome, eu.cod_funcao, v.numero_os, v.chassi_resumido, cd.bairro, v.controle|| '-' || v.serie,
pg.descricao,pg.cod_forma_pgto, os.tipo, ost.descricao, ed.cod_empresa_departamento, ic.custo_fornecedor`
I asked the administers for Oracle logs, it gave me nothing, the administers saw that my query even got to there, then I tried a lot of things and finally my boss found an answer. He saw that the problem is the size of the package the PHP are sending through the VPN connection we have with the cloud. So the solution (at least for now) is add to PDO connection the size we need using: "(DESCRIPTION=(SDU=1400) ..." on the connection string.
I'm really not sure what's going on at this point. I'm trying to create a view, and when I run the query below:
$view = oci_parse($conn, '
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
ARCUSTO.ID,
ORDERS.ID,
ORDERS.PONO,
ORDERS.ORDERNO,
ORDERS.DATE_TAKEN,
ORDERS.ARCUSTO_ID,
ORD_DETAIL.ORDERS_ID,
ORD_DETAIL.ID,
ORD_DETAIL.TOTAL_QTY_ORD,
ORD_DETAIL.CUMM_SHIPPED,
ORD_DETAIL.DISCOUNT,
ORD_DETAIL.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
a.ID = o.ARCUSTO_ID
AND
d.ORDERS_ID = o.ID
');
I get the error:
ORA-00904: "ORD_DETAIL"."UNIT_PRICE": invalid identifier
And if I try to delete ORD_DETAIL.UNIT_PRICE from the end of the select statement, then it tells me that ORD_DETAIL.DISCOUNT is now the invalid identifier. Whichever column is last in the list becomes the invalid identifier. If I remove the prefix to determine which table, then I get column ambiguously defined errors.
Any ideas??
EDIT 1
I've now changed the code to the following:
$view = oci_parse($conn, '
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
a.ID as CUSTOMER_ID,
o.ID as ORD_ID,
o.PONO,
o.ORDERNO,
o.DATE_TAKEN,
o.ARCUSTO_ID,
d.ORDERS_ID,
d.ID as DETAIL_ID,
d.TOTAL_QTY_ORD,
d.CUMM_SHIPPED,
d.DISCOUNT,
d.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
CUSTOMER_ID = o.ARCUSTO_ID
AND
ORD_ID = d.ORDERS_ID
');
But now it's saying that ORD_ID is an invalid identifier. This its the last line of the SQL statment.
How about trying :
CREATE OR REPLACE VIEW
V_ORD_HISTORY
AS
SELECT
a.ID as CUSTOMER_ID,
o.ID as ORD_ID,
o.PONO,
o.ORDERNO,
o.DATE_TAKEN,
o.ARCUSTO_ID,
d.ORDERS_ID,
d.ID as DETAIL_ID,
d.TOTAL_QTY_ORD,
d.CUMM_SHIPPED,
d.DISCOUNT,
d.UNIT_PRICE
FROM
ARCUSTO a,
ORDERS o,
ORD_DETAIL d,
ARINVT i
WHERE
a.id= o.ARCUSTO_ID
AND
o.id = d.ORDERS_ID
I'm working with a mysql query to select data from multiple tables using LEFT OUTER JOIN. Now i get the following error when i exequte the query:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'wg.werkbon_global_id = wk.werkbon_klant_globalid LEFT OUTER
JOIN users AS u' at line 16
Only the problem is that i can't find out what's wrong with my query.
PHP Query:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC;
$result = $db->loadAssoc($query);
I think my problem has something todo with left outer join but what?
You are missing the ON operator in your joins!
The correct syntax for a join is:
SELECT * FROM x LEFT JOIN y ON condition WHERE...
$query = "
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC";
$result = $db->loadAssoc($query);
Make sure there isn't missing quote
Problem soved thanks to arkascha
The fixed query is now:
$query = '
SELECT
wg.werkbon_global_id AS id,
wg.werkbon_global_status AS status,
wg.werkbon_global_date_lastedit AS date,
usr.user_firstname AS monteur_vn,
usr.user_insertion AS monteur_tv,
usr.user_lastname AS monteur_an,
wg.werkbon_global_type AS type,
wg.werkbon_global_layout AS layout,
wg.werkbon_global_werkzaamheden AS werkzaamheden,
wg.werkbon_global_opmerkingen AS opmerkingen,
wk.werkbon_klant_nummer AS klantnr
FROM
werkbon_klant AS wk
LEFT OUTER JOIN werkbon_global AS wg ON
wg.werkbon_global_id = wk.werkbon_klant_globalid
LEFT OUTER JOIN users AS usr ON
usr.user_id = wg.werkbon_global_monteur_finish
WHERE
wk.werkbon_klant_nummer = '.$db->Quote($klantid).'
ORDER BY id ASC';
$result = $db->loadAssoc($query);
#fred i don't need to add quotes by column names. You only need to add quotes by string/blob values.
#johny my $db->Quote() function will add qoutes automaticly. I don't need to add them and put everything in quote's.
Thanks all for help.
I was forced to update to MySQL 5 and PHP 5 as my service provider is no longer supporting older versions. I have a working site with this MySQL search that was working fine. The following is an include placed on the user's listing page. When I take the SQL statement out and run it on the server it returns the correct result. My guess is that it is something to do with formatting. Any ideas why I might be getting this error?
"Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY x.id_have LIMIT 0,30' at line 1"
With this syntax
<table cellpadding="15" border="0">
<tr>
<td valign="top">
So far, the following members have these items which match;
<ul>
<?
$peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username
FROM Want x
JOIN Want y
ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion)
inner join Version as v
on x.id_version = v.id_version
inner join Version as vv
on y.id_version = vv.id_version
inner join Game as g
on g.id_game = vv.id_game
inner join Customer as c
on y.id_customer = c.id_customer
inner join Game as gg
on gg.id_game = v.id_game
WHERE x.id_have = $hid
ORDER BY x.id_have
LIMIT 0, 30")or die("Query failed: " . mysql_error());
while($row = mysql_fetch_array($peoplewhomatch))
{ ?>
<span class='greenitalic'><?=$row['Member']?></span> has <span class='highshadow'><?=$row['Has']?></span> and wants <span class='ishadow'><?=$row['gwants']?></span><BR><?}?>
</td>
</tr>
</table>
You probably need to wrap $hid in quotes like so:
$peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username
FROM Want x
JOIN Want y
ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion)
inner join Version as v
on x.id_version = v.id_version
inner join Version as vv
on y.id_version = vv.id_version
inner join Game as g
on g.id_game = vv.id_game
inner join Customer as c
on y.id_customer = c.id_customer
inner join Game as gg
on gg.id_game = v.id_game
WHERE x.id_have = '$hid'
ORDER BY x.id_have
LIMIT 0, 30")or die("Query failed: " . mysql_error());
I am new to PHP PDO and converting some regular MySQL queries to work with PDO.
The query below when tested in phpMyAdmin works great when the assigned values replaced the current placeholders in the SQL statement. But when I configure it to work as it is now with PDO, it does not produce any results or errors. Can someone please tell me or show me what is it that I am doing wrong?
Someone told me that I cannot pass parameters as references in the array.
And if correct, what is the best way for creating a solution and by using only the user ID passed through to the variable $uid. Thanks.
<p>// For testing</p>
<pre>$uid = 1;</pre>
<p> </p>
<pre>$array = array(
':uId' => ''.$uid.'',
':aId' => 'u.user_id',
':gID' => 'a.group_id',
':eID' => 'a.entry_id',
':pID' => 'a.permit_id'
);</pre>
// create the sql for qd_user_usam table
$sql = "SELECT u.user_id, a.acl_id, g.group_name, e.entry_level, p.permit_level
FROM qd_users as u, qd_users_acl as a, qd_users_group as g, qd_users_entry as e, qd_users_permission as p
WHERE u.user_id = :uID
AND a.acl_id = :aID
AND g.group_id = :gID
AND e.entry_id = :eID
AND p.permit_id = :pID";
<p>try
{</p>
<p>// Build the database PDOStatement</p>
<pre>$_stmt = $this->_dbConn->prepare($sql);</pre>
<pre>$_stmt->execute($array);</pre>
<pre>}
catch(PDOException $e)
{</pre>
<pre>$this->_errorMessage .= 'Error processing user login access. <br /> Line #'.__LINE__ .' '.$e ;</pre>
<pre>die($this->_errorMessage);
}</pre>
<pre>$results = $_stmt->fetchAll(PDO::FETCH_ASSOC);</pre>
<pre>return $results;</pre>
<pre>$results = null;</pre>
<pre>$this->_dbConn = null;</pre>
You take prepared statements wrong.
Thy have to be used not to represent whatever value in the query, but dynamically added data only.
While a.group_id is a column name and have to be written as is, without prepared statements.
// For testing
$uid = 1;
// create the sql for qd_user_usam table
$sql = "SELECT u.user_id, a.acl_id, g.group_name, e.entry_level, p.permit_level
FROM qd_users as u, qd_users_acl as a, qd_users_group as
g, qd_users_entry as e, qd_users_permission as p
WHERE u.user_id = ?
AND a.acl_id = u.user_id
AND g.group_id = a.group_id
AND e.entry_id = a.entry_id
AND p.permit_id = a.permit_id";
$_stmt = $this->_dbConn->prepare($sql);
$_stmt->execute(array($uid));
The problem is that you're trying to write your JOINs implicitly by binding the joined columns as parameters, which would not work. The parameters can not reference another column; they are seen as strings in this case. If you rewrite the query like this it should fix the JOIN problem:
SELECT u.user_id, a.acl_id, g.group_name, e.entry_level, p.permit_level
FROM qd_users AS u
JOIN qd_users_acl AS a ON (u.user_id = a.acl_id)
JOIN qd_users_group AS g ON (g.group_id = a.group_id)
JOIN qd_users_entry AS e ON (e.entry_id = a.entry_id)
JOIN qd_users_permission AS p ON (p.permit_id = a.permit_id)
WHERE u.user_id = :uID