Hi,
I have what I hope is an easy PHP question. I'm fairly new to PHP, though not new to programming in general (I'm used to VB, though).
I have a search form that, when submitted, should run a search query and export the results to CSV, and I'm running into problems. I haven't found anything online about how to run mysqli_query() on a query with parameters (I need it to not leave itself wide open to SQL Insertion). The closest I've come is the following code (simplified; for details about what's getting passed, see the results of the debug statement):
This does export a CSV file, but the contents of the CSV file are (beginning with the output of the debug statement):
------------------
111 : c.ChemicalName : c.ChemicalName : %111% : SELECT c.`ChemicalID`,
c.`ChemicalName`,
c.`CommonName`,
c.`SDSDate`,
c.`Hazard`,
c.`ManufacturerID`,
m.ManufacturerName,
l.URL FROM `CONTENT_Chemicals` c
LEFT JOIN `CONTENT_Manufacturers` m ON c.ManufacturerID = m.ManufacturerID LEFT JOIN `CONTENT_ItemLinks` l ON c.`CurrPDFLinkID` = l.LinkID WHERE c.ChemicalName LIKE ? ORDER BY c.ChemicalName : <br />
<b>Warning</b>: mysqli_fetch_fields() expects parameter 1 to be mysqli_result, boolean given in <b>phpcnfg\include\listingfunctions.php</b> on line <b>21</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>phpcnfg\include\listingfunctions.php</b> on line <b>23</b><br />
<br />
<b>Fatal error</b>: Call to a member function fetch_array() on boolean in <b>phpcnfg\include\listingfunctions.php</b> on line <b>34</b><br />
------------------
Line 23: foreach ($fields as $field) {
Line 34: while ($row = $result->fetch_array(MYSQLI_NUM)) {
------------------
As far as I can tell, what I need for the CSV export to work, is to either change "$result = $stmt->execute();" to "$result = $mysqli->query($prep_stmt);" and have it somehow accept parameters, or transform the result of "$stmt->execute();" to a mysqli_result object, which I can then use just like the result of $mysqli->query. Anybody know how to do that? There must be a way; I can't be the only one who needs CSV files of query results that have user-submitted parameters that need to be free of SQL Injection, but Google is somehow turning up nothing.
Thanks!
Katie
I have what I hope is an easy PHP question. I'm fairly new to PHP, though not new to programming in general (I'm used to VB, though).
I have a search form that, when submitted, should run a search query and export the results to CSV, and I'm running into problems. I haven't found anything online about how to run mysqli_query() on a query with parameters (I need it to not leave itself wide open to SQL Insertion). The closest I've come is the following code (simplified; for details about what's getting passed, see the results of the debug statement):
PHP:
function export_query_to_csv($mysqli, $prep_stmt, $searchtext, $searchparam)
{
// $result = $mysqli->query($prep_stmt);
$stmt = $mysqli->prepare($prep_stmt);
if ($stmt) {
if ($searchtext>'') {
if (! $stmt->bind_param('s', $searchparam)) {
$err='Query failure: BINDING PARAMETERS FAILED ' . $searchparam;
header('Location: login_err.php?err=' . $err);
exit();
}
}
$result = $stmt->execute();
}
$fields = mysqli_fetch_fields($result);
$headers = array();
foreach ($fields as $field) {
$headers[] = $field->name;
}
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
header('Pragma: no-cache');
header('Expires: 0');
fputcsv($fp, $headers);
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}
}
function output_chem_listing_csv ($mysqli, $searchtext, $searchfor, $orderby, $showediting, $rootdirprefix, &$error_msg) {
$prep_stmt = get_chem_listing_query($searchtext, $searchfor, $orderby, $searchparam, 0);
echo $searchtext . ' : ' . $searchfor . ' : ' . $orderby . ' : ' . $searchparam . ' : ' . $prep_stmt . ' : '; //debug
export_query_to_csv($mysqli, $prep_stmt, $searchtext, $searchparam);
}
This does export a CSV file, but the contents of the CSV file are (beginning with the output of the debug statement):
------------------
111 : c.ChemicalName : c.ChemicalName : %111% : SELECT c.`ChemicalID`,
c.`ChemicalName`,
c.`CommonName`,
c.`SDSDate`,
c.`Hazard`,
c.`ManufacturerID`,
m.ManufacturerName,
l.URL FROM `CONTENT_Chemicals` c
LEFT JOIN `CONTENT_Manufacturers` m ON c.ManufacturerID = m.ManufacturerID LEFT JOIN `CONTENT_ItemLinks` l ON c.`CurrPDFLinkID` = l.LinkID WHERE c.ChemicalName LIKE ? ORDER BY c.ChemicalName : <br />
<b>Warning</b>: mysqli_fetch_fields() expects parameter 1 to be mysqli_result, boolean given in <b>phpcnfg\include\listingfunctions.php</b> on line <b>21</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>phpcnfg\include\listingfunctions.php</b> on line <b>23</b><br />
<br />
<b>Fatal error</b>: Call to a member function fetch_array() on boolean in <b>phpcnfg\include\listingfunctions.php</b> on line <b>34</b><br />
------------------
Line 23: foreach ($fields as $field) {
Line 34: while ($row = $result->fetch_array(MYSQLI_NUM)) {
------------------
As far as I can tell, what I need for the CSV export to work, is to either change "$result = $stmt->execute();" to "$result = $mysqli->query($prep_stmt);" and have it somehow accept parameters, or transform the result of "$stmt->execute();" to a mysqli_result object, which I can then use just like the result of $mysqli->query. Anybody know how to do that? There must be a way; I can't be the only one who needs CSV files of query results that have user-submitted parameters that need to be free of SQL Injection, but Google is somehow turning up nothing.
Thanks!
Katie