Pass $conn
as one parameter of the function too.
PHP Function that contains SQL query not working
Вопрос
I'm attempting to put together a function that will perform SQL queries as part of a code cleanup I'm doing. The query works outside of the function, but inside the function I get no results. Here's the applicable code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<?php
header("Cache-Control: no-cache, must-revalidate");
require_Once'../connect.php';
function callQuery($select,$from,$where){
$pull=array();
$query="SELECT ".$select." FROM ".$from." WHERE ".$where;
$run=sqlsrv_query($conn,$query);
while($result=sqlsrv_fetch_array($run,SQLSRV_FETCH_ASSOC)){
$resultS=var_dump($result);
}
return $query;
return $resultS;
}
echo callQuery("HDCaseNum","HDCase","HDCaseNum='8818'");
?>
</html></body>
The code is just a test setup to get the concept down.
EDIT This is what I was trying to accomplish:
function callQuery($select,$from,$where,$conn){
$resultS=array();
if($where){$where="WHERE $where";}
$query="SELECT ".$select." FROM ".$from." ".$where;
$run=sqlsrv_query($conn,$query);
while($result=sqlsrv_fetch_array($run,SQLSRV_FETCH_ASSOC)){
array_push($resultS,$result);
}
return $resultS;
}
I pass in the columns, table(s) and filters and it spits out a multidimensional array of results. Keeps me from having to retype all the sqlsrv functions over and over again.
Решение
Не связан с StackOverflow