This Code Works Perfectly For Mysqli
$sql = "SELECT * FROM 'mem_tree' ";
$res = mysqli_query($conn, $sql) or die("database error:".mysqli_error($conn));
while($row = mysqli_fetch_assoc($res)){
$data[] = $row;
}
$itemsByReference = array();
but When i'm trying to do with PDO it Doesn't Work
$sql = "SELECT * FROM 'mem_tree' ";
$stmt = $dbh->prepare($sql);
$stmt->execute();
while($row = $stmt->fetchAll(PDO::FETCH_ASSOC)){
$data[] = $row;
}
$itemsByReference = array();
Here i found Solution of it
$sql = "SELECT * FROM mem_tree";
$stmt = $dbh->prepare($sql);
$stmt->execute();
$final_arr = array();
$data = array();
$final_arr = $stmt->fetchAll(PDO::FETCH_ASSOC);
$data = $final_arr;
$itemsByReference = array();
Related
Fetch the data in array format like :
$value = array ("ABC","XYZ","OPQ");
$query = "SELECT * FROM designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
$designation= $row["designation"];
}
Need Result:
$value = array ("ABC","XYZ","OPQ");
Fetch the designation column, and push it into an array with the syntax $array[] = $value.
$designation = [];
$query = "SELECT designation FROM designation_master";
$result = mysqli_query($mysqli, $query);
while ($row = mysqli_fetch_array($result)) {
$designation[] = $row["designation"];
}
print_r($designation);
if the "ABC","XYZ","OPQ" are the same field [designation], you can convert $designation to array.
$query = "SELECT * FROM designation_master";
$result = mysqli_query($mysqli, $query);
$designation = array();
while ($row = mysqli_fetch_array($result))
{
$designation[] = $row["designation"];
}
How to connect 2 arrays? I want that $new[$code]=$color, how can I do this? Below is my code:
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}
Declare the variable outside the while loop
$new = array();
Then inside while loop
$new[$row['user_id']] = $row['user_color'];
In the while loop...
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$new[$row['user_id']] = $row['user_color'];
}
If you need the arrays seperate for some reason you can do it later using array_combine, http://php.net/manual/en/function.array-combine.php.
$sql = "SELECT user_id, user_color FROM dotp_users";
$result = mysql_query($sql) or die(mysql_error());
$code = $color = array();
while($row = mysql_fetch_assoc($result)) {
$code[] = $row['user_id'];
$color[] = $row['user_color'];
}
...
$new = array_combine($code, $color);
I need to merge categories and subcategories into one array. Here is code.
$sql = 'select * from category where id = ?';
$q = $db->prepare($sql);
$q->bindParam(1, $id);
$q->execute();
while($rows = $q->fetch()){
$cat[] = $rows['id'];
$s = 'select * from category where category_id = ?';
$q = $db->prepare($s);
$q->bindParam(1, $rows['id']);
$q->execute();
while($row = $q->fetch()){
$list[] = $row['id'];
}
$data = array_push($cat, $list);
echo $data;
}
I am getting 2 value when printing instead ids numbers
are you want to merge $cat and $list?
you can try:
array_merge($cat, $list);
I solved this problem with the help of this post combine-results-from-two-pdo-queries
$id = '1';
$results = array();
$sql = 'select * from category where id = ?';
$q = $db->prepare($sql);
$q->bindParam(1, $id);
$q->execute();
while($rows = $q->fetch(PDO::FETCH_ASSOC)){
$results[] = $rows['id'];
}
$s = 'select * from category where category_id = ?';
$q = $db->prepare($s);
$q->bindParam(1, $id);
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)){
$results[] = $row['id'];
}
var_dump($results);
I think what you want is this:
$sql = 'select * from category where id = ?';
$q = $db->prepare($sql);
$q->bindParam(1, $id);
$q->execute();
$data = array();
while($rows = $q->fetch()){
$cat[] = $rows['id'];
$s = 'select * from category where category_id = ?';
$q = $db->prepare($s);
$q->bindParam(1, $rows['id']);
$q->execute();
while($row = $q->fetch()){
$list[] = $row['id'];
}
$data = array_merge($data, $cat, $list);
//$data = array_unique($data);
print_r($data);
}
Notice the $data = array(); before the while loop. With array_merge you merge categories and subcategories with the $data array into the data array.
If you happen to get duplicted value you do not need or want, simply uncomment $data = array_unique($data); and it will erase duplicated entries for you.
Please clarify your question, if I do not address your problem and I will update my answer.
Have fun, may the source be with you.
How do I fetch data from db using select query in a function?
Example
function ec_select_query($student, $row = '', $fields=array()) {
$qry = "SELECT * FROM student";
$qry = mysql_query($qry);
while($row = mysql_fetch_assoc($qry)){}
return $row;
}
If you want to return all rows then first save it in an array in while loop then return this array.
function ec_select_query($student,$row='',$fields=array())
{
$qry = "SELECT * FROM student";
$qry = mysql_query($qry);
$result = array();
while($row = mysql_fetch_assoc($qry))
{
$result[] = $row;
}
return $result;
}
Its is running code. Modify it according to your needs
$con = mysql_connect('localhost','root','') or die("Unable to connect to MySQL");
mysql_select_db('demo', $con) or die("Database not found");
function ec_select_query($student)
{
$query = "SELECT * FROM $student";
$result = mysql_query($query);
$row = array();
$getData = array();
while($row = mysql_fetch_array($result))
{
$getData[]=$row;
}
return $getData;
}
$information = ec_select_query('accountplans');
echo "<pre>"; print_r($information); die;
Try it
function select_query($table, $where=array(),$fields=array()){
$select_fields = $table."*";
if(!empty($fields) && is_array($fields)){
$select_fields = implode(",", $fields);
}
$sql = "select ".$select_fields." from ".$table." where 1=1 ";
if(!empty($where) && is_array($where)){
foreach ($where as $key => $value) {
$sql .= " AND ".$value;
}
}
$query = mysql_query($sql);
$result = array();
while($row = mysql_fetch_assoc($result)){
$result[] = $row;
}
return $result;
}
Call Function
$fields = array("id","name","city");
$where = array('name = "abc"','city like "aaa"');
$students = select_query("studendts", $where, $fields);
This code might help you :
function ec_select_query($student,$row='',$fields=array())
{
$q = "SELECT * FROM student";
$q = mysql_query($qry);
while($row = mysql_fetch_array($qry))
{
return $row;
}
}
It is easiest way to produce entire data in array
function db_set_recordset($sql) {
$qry = mysql_query($sql);
$row= array();
while($out = mysql_fetch_assoc($qry)) {
$row[] = $out;
}
return $row;
}
$qry = "SELECT * FROM student";
$result = db_set_recordset($qry);
I need to store the rows in an array but can't seem to get it to work, the code is below:
I am sure $result = $stmt->get_result(); does not work.
When i test in the browser output is empty.
<?php
include "connect.php";
$uid = $_REQUEST["U_ID"];
$stmt = $mysqli->prepare("SELECT * FROM U_User WHERE U_ID = ?");
$stmt->bind_param("d", $uid);
$stmt->execute();
$stmt->fetch();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$users[] = $row;
}
$stmt->close();
$arr = array('user' => $users);
echo json_encode($arr);
mysqli_close($conn);
?>
Just like php_nub_qq said. I switched to PDO works great.
<?php
include "connect.php";
$uid = $_REQUEST["U_ID"];
$q = $conn->prepare("SELECT * FROM U_User WHERE U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$users[] = $r;
}
$q = $conn->prepare("SELECT * FROM P_Post u WHERE P_U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$posts[] = $r;
}
$q = $conn->prepare("SELECT * FROM T_Thread u WHERE T_U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$threads[] = $r;
}
$arr = array('user' => $users, 'post' => $posts, 'thread' => $threads);
echo json_encode($arr);
$conn = null;
?>