How to create multidimensional array with name of columns in PDO - php

I have this query and I would like to create multidimensional array. I tried this:
$columns = array("col1", "col2", "col3", "col4");
$query = "SELECT " . implode(",", $columns) . " FROM my_table";
$sql = $db->prepare($query);
$sql->execute();
$data = array();
while ($row = $stm->fetch()) {
$nestedData = array();
for ($i = 0, $m = count($columns); $i < $m; $i++) {
$value = $row[$columns[$i]];
$nestedData[] = empty($value) === false ? $value : "";
}
$data[] = $nestedData;
}
I get something like this:
[["value11","value12","value13","value14"],
["value21","value22","value23","value24"], etc]
I would like to have there also names of columns (like this):
[["col1":"value11","col2":"value12","col3":"value13","col4":"value14"],
["col1":"value21","col2":"value22","col3":"value23","col4":"value24"], etc]
Could anybody help me how to achieve this?

Have a look at "fetch_style" properties:
http://php.net/manual/en/pdostatement.fetch.php
And use
http://php.net/manual/en/pdostatement.fetchall.php
$columns = array("col1", "col2", "col3", "col4");
$query = "SELECT " . implode(",", $columns) . " FROM my_table";
$sql = $db->prepare($query);
$sql->execute();
$data = $sql->fetchAll(PDO::FETCH_ASSOC);

I believe what you're looking for is that PDO returns an associative array. This can easily be achieved by doing this:
$results = $stmt->fetch(PDO::FETCH_ASSOC);
or if you want this to be default for all your queries, you can create your connection that way:
$conn = new PDO($connStr, $usr, $pass, [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
For more information or options, read the php manual: http://php.net/manual/en/pdostatement.fetch.php

Firstly, $results = $stmt->fetch(PDO::FETCH_ASSOC);
Then you need:
$nestedData[] = empty($value) === false ? $value : "";
needs to look like
$nestedData[$column_name] = empty($value) === false ? $value : "";
where $column_name is the name of the field. Look at the manual to work out how to get the field names that you need.
Something like this might work:
foreach ($row AS $key => $value)
{
$nestedData[$key] = empty($value) === false ? $value : "";
}

Instead for loop you can do foreach loop
foreach($array as $key=>$element){
$nesteddata[$key] = $element;
}

Related

PHP with stored procedure - cursor output in Oracle

I've been trying to get the result from a stored procedure in Oracle using PHP. This stored procedure has some input parameters and one output cursor which give me the result.
Here is my stored procedure:
CREATE OR REPLACE PROCEDURE "TEST1" (
pPARAM1 CLOB,
pPARAM2 VARCHAR2,
p_record OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN p_record FOR
SELECT * FROM PARAMS
WHERE id_param = pPARAM1 AND id_param2 = pPARAM2;
END;
Here is my code:
$conexion = oci_connect($username, $password, $connection);
if (!$conexion) {
$e = oci_error();
echo $e['message'];
}
$params = array("pPARAM1" => 'apple', "pPARAM2" => 'banana');
$params_values = [];
$params_marks = [];
$marks = "";
$i = 0;
if(count($params) > 0) {
foreach ($params as $key => $value) {
$params_marks[$i] = ":" . $key;
$params_values[$key] = $value;
$i++;
}
$marks = implode(",", $params_marks);
$query = 'BEGIN TEST1(' . $marks . ',:cursbv); END;';
} else {
$query = 'BEGIN TEST1(:cursbv); END;';
}
$rs = oci_parse($conexion, $query) or die();
foreach ($params_values as $key => $value) {
oci_bind_by_name($rs, $key, $params_values[$key]);
}
$cursor = oci_new_cursor($conexion);
oci_bind_by_name($rs, ":cursbv", $cursor, -1, OCI_B_CURSOR) or die();
oci_execute($rs);
oci_execute($cursor);
if ($rs) {
while (($row = oci_fetch_array($cursor, OCI_ASSOC + OCI_RETURN_NULLS)) != false) {
var_dump($row);
}
}
oci_free_statement($rs);
oci_free_statement($cursor);
This give me a warning in oci_fetch_array but don't understand why because $cursor is a oci-resource. Need any suggestion. Thanks.
Note: My table have some CLOB columns and the stored procedure may have some CLOB input parameters.
If it's usefull for someone else: because of the use of CLOB columns and parameters I needed to add...
Binding variables:
$clob['apple'] = oci_new_descriptor($conexion, OCI_D_LOB);
oci_bind_by_name($rs, 'apple', $clob['apple'], -1, OCI_B_CLOB);
$clob['apple']->writeTemporary($params_values['apple']);
oci_bind_by_name($rs, 'banana', $params_values['banana']);
Fetching result:
while (($row = oci_fetch_array($cursor, OCI_ASSOC + OCI_RETURN_LOBS + OCI_RETURN_NULLS)) != false) {

Executing pre-build-up array with PDO Execute

I am trying to pass through any query to a function using PDO.
I have build up the array through a loop function and try to insert it into the execute(array(....)) function, but it's not getting through.
FUNCTION CODE
public function ShowData($sql,$variable)
{
$execute_string = "";
echo "<pre>";
print_r($variable);
echo "</pre>";
$q = $this->conn->prepare($sql);
for($i = 0; $i < sizeof($variable); $i++)
{
if($i != 0) $execute_string .= ",";
$placeholder = $i + 1;
$execute_string .= "':$placeholder' => '".$variable[$i]."'";
}
echo $sql."<br>";
echo $execute_string;
$q->execute(array($execute_string));
echo "<br>Execute Succeeded";
return $row = $q->fetchAll();
}
VIEWPAGE CODE
$author = "Nemoza";
$name = "MBICO_mailer";
$project = $data->ShowData("SELECT * FROM mbico_projects WHERE author=:1 AND name=:2", array($author,$name));
OUTPUT FROM FUNCTION W/ DEBUGGING
Array
(
[0] => Nemoza
[1] => MBICO_mailer
)
SELECT * FROM mbico_projects WHERE author=:1 AND name=:2
':1' => 'Nemoza',':2' => 'MBICO_mailer'
However, the 'Execute Succeeded' text is not being printed, and the execute(array...)) is not actually executing.
What am I doing wrong, and how else should I do it?
here's an example you can use:
public function ShowData($sql,$variable) {
$bind = [];
foreach ($variable as $key => $value) {
$ph = $key+1;
$bind[":" . $ph] = $value;
}
$stmt = $this->conn->prepare($sql);
$stmt->execute($bind);
return $stmt->fetchAll();
}
it's used like this:
$sql = 'select * from users where username = :1 or username = :2';
$bind = ['123', '456'];
$db->ShowData($sql, $bind);
as mentioned in the comments to your question, you need to send an array to execute() function, and not a string.
Managed to do it like this:
public function ShowData($sql,$variable)
{
$execute_string = array();
$q = $this->conn->prepare($sql);
foreach($variable as $item)
{
$execute_string[] = $item;
}
$q->execute($execute_string);
return $q->fetchAll();
}
$project = $data->ShowData("SELECT * FROM mbico_projects WHERE author=? AND title=?", array($author, $title));

getting duplicated results from nested foreach loop

I'm trying to get the following output from mysql for Google Line Chart API:
[["product","diameter","width"],["Product 1","2","4"],["Product 2","4","8"]]
I have set up several input checkboxes to send field names (e.g width,diameter) to the database via $_POST["info"] and retrieve the values from those fields. Here's the part that generates the data from mysql:
$result = $users->fetchAll();
$comma = "";
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
foreach($_POST["info"] as $p)
{
$d .= $comma.$r[$p]; // trying to get "$r["width"],$r["diameter"]"
}
$comma = ",";
$data[$i] = array($r["name"], $d);
$i++;
}
echo json_encode($data);
My desired output should be like this:
[["product","diameter","width"],["Product 1","2","4"],["Product 2","4","8"]]
But that code is generating duplicated results like this
[["product","diameter","width"],["Product 1","24"],["Product 2","24,4,8"]]
I guess I shouldn't be using the nested foreach to loop over $_POST. Can anyone tell me how to fix that?
Full PHP Code:
$info = $_POST["info"]; // It contains an array with values like width,diameter,thickness etc...
$comma = "";
foreach($info as $in)
{
$field .= "".$comma."b.".$in."";
$comma = ",";
}
$sql = "
SELECT {$field},a.user_id,a.name
FROM `product_detail` a INNER JOIN
`attr` b ON a.model = b.model
WHERE a.user_id = ?
GROUP BY a.model
";
$users = $dbh->prepare($sql);
$users->bindValue(1, $_SESSION["user_id"]);
$users->execute();
$result = $users->fetchAll();
$comma = "";
$data="";
$i = 1;
$data[0] = array_merge(array(product),$info);
foreach ($result as $r)
{
foreach($_POST["info"] as $p)
{
$d .= $comma.$r[$p];
}
$comma = ",";
$data[$i] = array($r["name"], $d);
$i++;
}
echo json_encode($data);
$_POST["info"] Content:
Array
(
[0] => diameter
[1] => width
)
try it like this:
$result = $users->fetchAll();
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
$d[]=$r["name"];
foreach($_POST["info"] as $p)
{
$d[]= $r[$p];
}
$data[$i] = $d;
$d=array(); //set $d to empty not to get duplicate results
$i++;
}
echo json_encode($data);
The end result you are looking for, is valid JSON. You should not try to manually generate that.
Instead you should make an array of arrays in php and use json_encode($array) to get the result you are looking for.
Also note that by injecting your POST variables directly in your query, you are vulnerable to sql injection. When accepting fields, you should check them against a white-list of allowed values.
Try the below solution:
$result = $users->fetchAll();
$data="";
$data[0] = array_merge(array(product),$info);
$i = 1;
foreach ($result as $r)
{
$d = array();
foreach($_POST["info"] as $p)
{
$d[] = $r[$p]; // trying to get "$r["width"],$r["diameter"]"
}
$data[$i] = array($r["name"]) +$d;
$i++;
}
echo json_encode($data);

Mysqli get_result alternative

I've just changed all my sql queries to prepared statements using mysqli. To speed this process up I created a function (called performQuery) which replaces mysql_query. It takes the query, the bindings (like "sdss") and the variables to pass in, this then does all the perpared statement stuff. This meant changing all my old code was easy. My function returns a mysqli_result object using mysqli get_result().
This meant I could change my old code from:
$query = "SELECT x FROM y WHERE z = $var";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)){
echo $row['x'];
}
to
$query = "SELECT x FROM y WHERE z = ?";
$result = performQuery($query,"s",$var);
while ($row = mysql_fetch_assoc($result)){
echo $row['x'];
}
This works fine on localhost, but my web hosting server does not have mysqlnd available, therefore get_result() does not work. Installing mysqlnd is not an option.
What is the best way to go from here? Can I create a function which replaces get_result(), and how?
Here is a neater solution based on the same principle as lx answer:
function get_result( $Statement ) {
$RESULT = array();
$Statement->store_result();
for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
$Metadata = $Statement->result_metadata();
$PARAMS = array();
while ( $Field = $Metadata->fetch_field() ) {
$PARAMS[] = &$RESULT[ $i ][ $Field->name ];
}
call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
$Statement->fetch();
}
return $RESULT;
}
With mysqlnd you would normally do:
$Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
$Statement->bind_param( 's', $z );
$Statement->execute();
$Result = $Statement->get_result();
while ( $DATA = $Result->fetch_array() ) {
// Do stuff with the data
}
And without mysqlnd:
$Statement = $Database->prepare( 'SELECT x FROM y WHERE z = ?' );
$Statement->bind_param( 's', $z );
$Statement->execute();
$RESULT = get_result( $Statement );
while ( $DATA = array_shift( $RESULT ) ) {
// Do stuff with the data
}
So the usage and syntax are almost identical. The main difference is that the replacement function returns a result array, rather than a result object.
I encountered the same problem and solved it using the code provided in the answer of
What's wrong with mysqli::get_result?
My function looks like this now (error handling stripped out for clarity):
function db_bind_array($stmt, &$row)
{
$md = $stmt->result_metadata();
$params = array();
while($field = $md->fetch_field()) {
$params[] = &$row[$field->name];
}
return call_user_func_array(array($stmt, 'bind_result'), $params);
}
function db_query($db, $query, $types, $params)
{
$ret = FALSE;
$stmt = $db->prepare($query);
call_user_func_array(array($stmt,'bind_param'),
array_merge(array($types), $params));
$stmt->execute();
$result = array();
if (db_bind_array($stmt, $result) !== FALSE) {
$ret = array($stmt, $result);
}
$stmt->close();
return $ret;
}
Usage like this:
$userId = $_GET['uid'];
$sql = 'SELECT name, mail FROM users WHERE user_id = ?';
if (($qryRes = db_query($db, $sql, 'd', array(&$userId))) !== FALSE) {
$stmt = $qryRes[0];
$row = $qryRes[1];
while ($stmt->fetch()) {
echo '<p>Name: '.$row['name'].'<br>'
.'Mail: '.$row['mail'].'</p>';
}
$stmt->close();
}
I found the anonymous advice posted as a note at the API documentation page for mysqli_stmt::get_result very useful (I couldn't think of a better way than the eval trick), as we very often want to fetch_array() on our result. However, because I wanted to cater for a generic database object, I found it a problem that the code assumed numeric array was fine for all callsites, and I needed to cater for all callers using assoc arrays exclusively. I came up with this:
class IImysqli_result {
public $stmt, $ncols;
}
class DBObject {
function iimysqli_get_result($stmt) {
$metadata = $stmt->result_metadata();
$ret = new IImysqli_result;
if (!$ret || !$metadata) return NULL; //the latter because this gets called whether we are adding/updating as well as returning
$ret->ncols = $metadata->field_count;
$ret->stmt = $stmt;
$metadata->free_result();
return $ret;
}
//this mimics mysqli_fetch_array by returning a new row each time until exhausted
function iimysqli_result_fetch_array(&$result) {
$stmt = $result->stmt;
$stmt->store_result();
$resultkeys = array();
$thisName = "";
for ( $i = 0; $i < $stmt->num_rows; $i++ ) {
$metadata = $stmt->result_metadata();
while ( $field = $metadata->fetch_field() ) {
$thisName = $field->name;
$resultkeys[] = $thisName;
}
}
$ret = array();
$code = "return mysqli_stmt_bind_result(\$result->stmt ";
for ($i=0; $i<$result->ncols; $i++) {
$ret[$i] = NULL;
$theValue = $resultkeys[$i];
$code .= ", \$ret['$theValue']";
}
$code .= ");";
if (!eval($code)) {
return NULL;
}
// This should advance the "$stmt" cursor.
if (!mysqli_stmt_fetch($result->stmt)) {
return NULL;
}
// Return the array we built.
return $ret;
}
}

mysql query for each of the values of an array

for ($i=0; $i<=$gsayi-1; $i++)
{
$a[] = mysql_result($aylik,$i);
}
foreach ($a as $value)
{
$m = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
echo $m;
}
a[ ] array is
2,2,1
I am trying to execute a mysql query with each of that values. I am using that values as ID.
I tried foreach loop but it shows me Resource id #7Resource id #7
How can I solve this problem?
You have to fetch the result
$m = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
$result = mysql_fetch_assoc($m);
echo $result['puan'];
You have to get the results in some form.
http://php.net/manual/en/function.mysql-query.php
mysql_query returns a resource...not the results of the query. To get the results you have to use one of the mysql_fetch_* functions, in this example we fetch an object with the query results.
http://www.php.net/manual/en/function.mysql-fetch-object.php
Try something like this:
for ($i=0; $i<=$gsayi-1; $i++) {
$a[] = mysql_result($aylik,$i);
}
foreach ($a as $value) {
$result = mysql_query("SELECT puan FROM sorular WHERE ID = $value");
while($row = mysql_fetch_object($result) {
echo $value . ' = ' . $row->puan . ' | ';
}
}

Categories