Thursday 28 November 2019

php - using prepared mysqli statements to bind parameters into the SELECT section of a query



I am building a web app that imports data from data feeds in php/mysql. I import the data into a buffer/temp holding table. As each data format is different I choose the column to select based on the particular source.



I am having trouble getting this query to work in this context :



$stmt = $this->dbObj->prepare("SELECT mk.PK_phone_maker, b.?, b.phoneDescription
b.thumbPic,

FROM buffer_table b left join mobile_phone pm on b.? = pm.phoneModel
LEFT JOIN phone_maker mk on mk.CompanyName = b.?
WHERE pm.phoneModel is null
group by b.?");
$stmt->bind_param('ssss',$phoneModelField, $phoneModelField, $phnMakerField,$phoneModelField);
$stmt->execute();


I recieve the error msg:




Fatal error: Call to a member function bind_param() on a non-object


This refers to the line:



 $stmt->bind_param('ssss',$phoneModelField, $phoneModelField, 


And I assume this is because the "prepare" on my sql hasnt worked as $stmt is not an object




As such it appears to me that you can not bind parameters to select columns and join fields, you can only bind to the where clause. Am I right in this assertion or am I missing something?


Answer



Prepared statements only allow you to bind values, other constructs (such as fields, tables or functions, let alone whole bits of SQL) are not allowed.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...