Wednesday, 28 December 2016

mysql - Syntax error or access violation php sql




i have added this sql in my code ,



function devenir_client_dataforform() {
$type = $_POST['clientType'];
//$produitname = $_POST['produitname'];
$produitnid = $_POST['produitnid'];
$query = db_select('node', 'n');

$query->leftjoin('field_data_field_description_col_1', 'img', 'img.entity_id = n.nid');
$query->leftjoin('field_data_field_pdf_file_image', 'pdf', 'pdf.entity_id = n.nid');

$query->leftjoin('taxonomy_term_data', 't', 't.tid ='.$type);



$query->condition('n.nid', $produitnid, '=')
//->condition('t.tid', $type, '=')
->fields('n', array('title'))
->fields('t', array('name'))
->fields('pdf', array('field_pdf_file_image_fid'))
->fields('img', array('field_description_col_1_value'));


$result = $query->execute();
foreach ($result as $record) {
if(!empty($record->field_description_col_1_value)) {
$fid = $record->field_description_col_1_value;
$produitname = $record->title;
$pdf = $record->field_pdf_file_image_fid;
$tp = $record->name;

}

}
$file = file_load($pdf);
$uri = $file->uri;

$path = file_create_url($uri);
//$uri = image_style_path( $path);
$items[] = array(
"type" => $tp ,
"produitname" => $produitname,
"produitnid" => $produitnid,

"image" => $fid,
"pdf" => $path,
"typeid" => $type
);
return $items;
}


but in the last step there is an error





PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE (n.nid = NULL)' at line 4: SELECT n.title AS title, t.name
AS name, pdf.field_pdf_file_image_fid AS field_pdf_file_image_fid,
img.field_description_col_1_value AS field_description_col_1_value
FROM {node} n LEFT OUTER JOIN {field_data_field_description_col_1} img
ON img.entity_id = n.nid LEFT OUTER JOIN
{field_data_field_pdf_file_image} pdf ON pdf.entity_id = n.nid LEFT

OUTER JOIN {taxonomy_term_data} t ON t.tid = WHERE (n.nid =
:db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0]
=> ) in devenir_client_dataforform() (line 189 of /home/dotit_bh/bh-website.dotit.mobi/sites/all/modules/devenir_client/devenir_client.module).




enter image description here


Answer



Error in this part of SQL Query:



 WHERE (n.nid = NULL)



Correct SQL Query:



WHERE (n.nid is NULL)


You shall rewrite your script:



$nid_operator = ($produitnid ? '=' : 'is');

$query->condition('n.nid', $produitnid, $nid_operator)
//->condition('t.tid', $type, '=')
->fields('n', array('title'))
->fields('t', array('name'))
->fields('pdf', array('field_pdf_file_image_fid'))
->fields('img', array('field_description_col_1_value'));

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...