I'm fairly new to php and mysql. I'm trying to create a rest api from php, and because my server doesn't have mysqlndinstalled, I have to use bind_result and fetch.
$stmt = $this->conn->prepare("SELECT * from d WHERE d.id = ?");
$stmt->bind_param("i", 1);
if($stmt->execute()){
$stmt->bind_result($a, $b, $c);
$detail = array();
while($stmt->fetch()){
$detail["a"] = $a;
$detail["b"] = $b;
$detail["c"] = $c;
}
$stmt->close();
return $response;
} else {
return NULL;
}
Above code works but it can only return 1 line of information at a time.
For example if the statement return:
a b c
1 test test
1 test1 test1
it only returns
a: 1
b: test1
c: test1
where its supposed to be:
{
a: 1
b: test
c: test
},
{
a: 1
b: test1
c: test1
}
You're overwritting them, you could do something like this instead:
$detail = array();
while($stmt->fetch())
{
$temp = array():
$temp["a"] = $a;
$temp["b"] = $b;
$temp["c"] = $c;
$detail[] = $temp;
}
Or directly appending them with another dimension:
$detail = array();
while($stmt->fetch()) {
$detail[] = array('a' => $a, 'b' => $b, 'c' => $c);
// ^ add another dimension
}
Related
I have 2 tables. Table 'comments' contains all the comments with cid ,comment as fields. and another table 'reply' which consists of cid and rpid which stores which comment is the reply of which comment.
function array_gen($c, $array, $total, $conn, $f) {
$array[] = $c;
$sql = "SELECT rpid FROM reply WHERE cmid = '$c';";
$st = $conn->query($sql);
$st->setFetchMode(PDO::FETCH_ASSOC);
$rpid = $st->fetchAll();
$count = $st -> rowCount();
if ($count > 0) {
foreach ($rpid as $r) {
$array = array_gen($r['rpid'], $array, $total, $conn, $f);
}
}
else {
echo $c;
return $array;
}
while ($f <= $total) {
if (! in_array($f, $array)) {
$array = array_gen($f, $array, $total, $conn, $f);
}
$f++;
}
return $array;
}
I tried out a function with parameters total number of comments ($total), first comment ($c), empty array ($array), a flag variable $f initialised to value of $c and a PDO connection variable $conn.
It works perfectly upto 2 level of comment-reply. But when it comes to 3rd level i.e, a reply is given to another reply, it starts misbehaving. Function return is not working properly inside foreach loop.
i am using PDO to get some values of a table like : (table name is ban)
ID word
1 one
2 two
3 three
4 four
MY function is :
function retBans() {
global $connect;
$result = $connect->prepare("SELECT * FROM ban");
$result->execute();
$a = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$a = $row['word'].",";
}
return $a;
}
and in the main php file, i wanted to get them back with this code :
$a = array();
$a = retBans();
$b = explode(",",$a);
print_r($b);
I wanted to have this :
Array {
[0] => one
[1] => two
[2] => three
[3] => four
}
But , it just return and print_r the last value (four) in array.
How can i get them like i said ?
Use this instead -
$a = '';
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
$a .= $row['word'].",";
}
Then, you can use explode function
$a = retBans();
$b = explode(",",$a);
echo "<pre>"; print_r($b);
Hi I am trying to store MySQL result into a global array
class db
{
function CustomLoad($table_name,$Fields)
{
global $_MYSQL_DATA;
$_MYSQL_DATA=array();
$qry = mysql_query("SELECT * FROM $table_name") or die(mysql_error());
while($row = mysql_fetch_assoc($qry))
{
foreach($Fields as $r=>$key)
{
$_MYSQL_DATA[$r] = $row[$r];
}
}
}
}
I am calling like this
$dbObj = new db();
$fields = array("FIELD_1"=>"FIELD 1","FIELD_2"=>"FIELD 2","FIELD_3"=>"FIELD 3","FIELD_4"=>"FIELD 4");
$dbObj->CustomLoad("registrations",$fields);
print_r($_MYSQL_DATA);
The problem is I am getting the last result only. like Array ( [FIELD_1] => A [FIELD_2] => B [FIELD_3] => C [FIELD_4]=> D )
Just use the following:
$_MYSQL_DATA = array(); // you should declare your variables, even if it's not mandatory
while($row = mysql_fetch_assoc($qry)) // USE PDO or MySQLi !!!!!
{
$_MYSQL_DATA[] = $row;
}
Note
The [] operator generates the smallest, positive, numeric key that is not used in your array.
Examples:
$array = array(0 => 'b', 1 => 'a');
$array[] = 'c'; // will place it in $array[2]
$array = array();
$array[] = 'a'; // will place in $array[0]
And now ... the rant about PDO / MySQLi (because I have to say it :P).
MySQL is officially deprecated since PHP 5.5, and it will no longer be maintained. You should consider porting your code to either MySQLi or PDO.
in foreach there should be $row not $Field. Also run a counter
$i=0;
while($row = mysql_fetch_assoc($qry))
{
foreach($row as $r=>$key)
{
$_MYSQL_DATA[$i][$r] = $key;
}
$i++;
}
I need to convert a string from an ldap query. I am querying my Active Directory server for user accounts. This is the string that I pulled.
"CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local"
I would want it converted to an array that looks like this
$array['local']['OCtest']['Department & Buildings']['Duck Commander']['Users']['Phil Robertson']=1;
NOT
$array( [1]=>'local,[2]=>'OCtest',[3]='Depart',[4]='Duck Commander',[5]='Users');
So far I have
Example code ---
$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local",1);
unset($dnn2['count']);
echo "<pre>";
print_r(array_reverse($dnn2));
What am I needing?
Try this
$arrayvalue = array();
foreach($dnn2 as $dn)
{
$temp = explode('=',$dn);
$temp1 = substr($temp[1], 0, strpos($temp1[1], ','));
$arrayvalue[] = $temp1;
}
print_r($arrayvalue);
$dnn2 = ldap_explode_dn("CN=Phil Robertson,OU=Users,OU=Duck Commander,OU=Department & Buildings,DC=OCSDtest,DC=local",1);
unset($dnn2['count']);
$result = null;
$ref =& $result;
foreach (array_reverse($dnn2) as $dn) {
$ref = array($dn => null);
$ref =& $ref[$dn];
}
print_r($result);
Try this
$a = array();
foreach($dnn2 as $dn)
{
$arr = explode('=',$dn);
$a[] = $arr[1]; //or $a[] = array($arr[1]); for 6 dimensional array
}
print_r($a);
am trying to print out only the unique values. since am receiving a huge object array from the I am trying to use now the ArrayObject class of PHP to iterate
$arrayobject = new ArrayObject($data);
$iterator = $arrayobject->getIterator();
while($iterator->valid()){
echo $iterator->current()->USERID. " : " .$iterator->current()->SUBCATID."<br/>";
$iterator->next();
}
here's the current result of that
201087 : 1
201146 : 1
201087 : 3
201087 : 2
as you can see, the first data has two other duplicates
and also, the first and second data has similar subcatid..
the objective is, print only the unique userid and subcatid..
how to skip those duplicate data, given that sample code of mine
as a starting point ?
Not quite sure I understand the question but maybe....
You can either sort the array and remember the current userid so your script can skip duplicates until it reaches another id.
<?php
$data = data();
usort(
$data,
function($a,$b) {
return strnatcmp($a->USERID, $b->USERID);
}
);
$current = null;
foreach( $data as $e ) {
if ( $current!=$e->USERID ) {
$current = $e->USERID;
echo $e->USERID, ' ', $e->SUBCATID, "\n";
}
}
function data() {
$x = array(
array(201087,1),
array(201146,1),
array(201087,3),
array(201087,2),
array(222222,3)
);
foreach($x as $y) {
$o = new StdClass;
$o->USERID = $y[0];
$o->SUBCATID = $y[1];
$data[] = $o;
}
return $data;
}
or the script remembers all previously processed ids, e.g. in a hashmap/array
<?php
$data = data();
$processed = array();
foreach( $data as $e ) {
if ( !isset($processed[$e->USERID]) ) {
$processed[$e->USERID] = true;
echo $e->USERID, ' ', $e->SUBCATID, "\n";
}
}
function data() {
$x = array(
array(201087,1),
array(201146,1),
array(201087,3),
array(201087,2),
array(222222,3)
);
foreach($x as $y) {
$o = new StdClass;
$o->USERID = $y[0];
$o->SUBCATID = $y[1];
$data[] = $o;
}
return $data;
}
both scripts print
201087 1
201146 1
222222 3
$ids = array(1,2,3,4,4);
$ids = array_unique($ids); // remove duplicates