Currently I'm working on a project, which using the depth first search to retrieve value, but I'm only able to echo the value, but I don't know how to store the value into the variable.
Here is my code
function calculate_ttl_member_agent ($conn, $id) {
$id_val = $level = "";
$search_dl_sql = "select * from table where foreign_ID = ".$id;
$search_dl_exe = mysqli_query($conn, $search_dl_sql);
while($result = mysqli_fetch_assoc($search_dl_exe)) {
$level = $result['level'];
$id_val = $result['ID'];
echo ",'".$level."'";
calculate_ttl_member_agent ($conn, $id_val);
}
}
I have been trying the use return, but it only give the first level value..
Create an array and add values to it.
function calculate_ttl_member_agent ($conn, $id) {
$id_val = $level = "";
$search_dl_sql = "select * from table where foreign_ID = ".$id;
$search_dl_exe = mysqli_query($conn, $search_dl_sql);
// define an empty array
$arr = [];
while($result = mysqli_fetch_assoc($search_dl_exe)){
$level = $result['level'];
$id_val = $result['ID'];
echo ",'".$level."'";
calculate_ttl_member_agent ($conn, $id_val);
array_push( $arr, [$level, $id_val, 'add anything else needed']);
return $arr;
}}
Store values in an array as commented by Alexanderp and use after
// inside while
$level = $result['level'];
$id_val = $result['ID'];
$level[]= $level;
or
$arr['level'][] = $result['level'];
$arr['id_val'][] = $result['ID'];
Related
I have to hide all empty arrays from the result or with value = 0. How can I do?
$return_arr = array();
$fetch = mysqli_query($conn, "SELECT...");
while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
$row_array['id'] = $row['ordr'];
$row_array['name'] = $row['name'];
$row_array['icon'] = $row['icon'];
$row_array['file_a'] = $row['file_a'];
$row_array['file_b'] = $row['file_b'];
$row_array['file_c'] = $row['file_c'];
$row_array['file_r'] = $row['file_r'];
array_push($return_arr,$row_array);
}
$response_arr = json_encode($return_arr);
return $response_arr;
}
There is missing code from your function so I have worked with what is available.
Try selecting only the cols you want data from and then check if col is empty per row. If there is a column that has data then the whole row is returned.
$return_arr = array();
$fetch = mysqli_query($conn, "SELECT name,icon,file_a,file_b,file_c,file_r FROM...");
while ($row = mysqli_fetch_array($fetch, MYSQLI_ASSOC)) {
$tmp = 0;
foreach( $row as $key => $val ){
if( !empty($row[$key]) ) $tmp++;
}
if( $tmp > 0 ) array_push($return_arr,$row_array);
}
$response_arr = json_encode($return_arr);
return $response_arr;
}
Also array_filter might be helpful as it can exclude all false values on the results set (used without callback).
More info on array_filter
I have created the following function to fetch data from my database, but its capabilities are limited. Currently it can fetch one value at a time, which is fine for fetching the value of one column of one row, but as I progress with my work, I now want to be able to fetch multiple values in one call.
The Function:
function retrieve($value, $identifier = null) {
// Check if identifier is given
$identifier = (is_null($identifier)) ? "`ID` = '{$_SESSION["ID"]}'" : $identifier;
// Connect to the database
$connection = connect("limited");
// Pass query, get result and fetch value out of it
$query = "SELECT * FROM `users` WHERE $identifier";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
return $data[$value];
}
mysqli_close($connection);
}
How I currently use it to fetch multiple values:
// Define variables
$x1 = retrieve("x1");
$x2 = retrieve("x2");
$x3 = retrieve("x3");
$x4 = retrieve("x4");
$x5 = retrieve("x5");
$x6 = retrieve("x6");
$x7 = retrieve("x7");
$x7 = retrieve("x8");
I have read other questions here on Stack Overflow, but none of them solves my problem as I use an optional parameter, which makes my life hard. For example, I thought of implementing the splat operator to allow unlimited parameters, but as I use the optional parameter $identifier, I can't make it into something like:
function retrieve($identifier = null, ...$value) {}
because it will use the first parameter as the identifier when I omit it.
I'm sure that regarding performance it would be better if I could fetch all the necessary values in one call of the function retrieve() instead of using it as shown above and that's why I would like to know:
How can I edit this function in order to fetch more values at once?
Calling it like so:
$x = retrieve($y);
$x1 = $y["x1"];
$x2 = $y["x2"];
...
EDIT:
Thanks to Manish Jesani for his help! I used his answer and modified to do exactly what I want. For anyone that may be interested in the future, here's the code:
function retrieve($value, $identifier = null) {
// Check if identifier is given
$values = array();
$identifier = (is_null($identifier)) ? "`ID` = '1'" : $identifier;
// Connect to the database
$connection = connect("limited");
// Pass query, get result and fetch value out of it
$query = "SELECT * FROM `users` WHERE $identifier";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
if (is_array($value)) {
foreach($value as $_value) {
$values[$_value] = $data[$_value];
}
return $values;
}
else {
return $data[$value];
}
}
mysqli_close($connection);
}
You can call the function with as many parameters you want. Τo do this you have to use func_num_args() to get all of them, as shown below:
function retrieve() {
$args = func_num_args();
$query = "SELECT '".implode("','", func_get_args())."' FROM `users` WHERE $identifier";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
return $data;
}
mysqli_close($connection);
}
You can call this function like this: $params = retrieve('x1','x2','x3').
Alternatively, you can retrieve them as variables list($x1, $x2, $x3) = retrieve('x1','x2','x3').
Please try this:
function retrieve($value, $identifier = null) {
// Check if identifier is given
$return = array();
$identifier = (is_null($identifier)) ? "`ID` = '{$_SESSION["ID"]}'" : $identifier;
// Connect to the database
$connection = connect("limited");
// Pass query, get result and fetch value out of it
$query = "SELECT * FROM `users` WHERE $identifier";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
$data = mysqli_fetch_assoc($result);
if(is_array($value))
{
foreach($value as $_value)
{
$return[$_value] = $data[$_value];
}
}
else
{
$return[$value] = $data[$value];
}
return $return;
}
mysqli_close($connection);
}
$x = retrieve(array("x1","x2","x3","x4","x5","x6"));
MySql query returns me a multi-dimensional array :
function d4g_get_contributions_info($profile_id)
{
$query = "select * from contributions where `project_id` = $profile_id";
$row = mysql_query($query) or die("Error getting profile information , Reason : " . mysql_error());
$contributions = array();
if(!mysql_num_rows($row)) echo "No Contributors";
while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
{
$contributions[$cnt]['user_id'] = $fetched['user_id'];
$contributions[$cnt]['ammount'] = $fetched['ammount'];
$contributions[$cnt]['date'] = $fetched['date'];
$cnt++;
}
return $contributions;
}
Now I need to print the values in the page where I had called this function. How do I do that ?
change the function like this:
while($fetched = mysql_fetch_array($row, MYSQL_ASSOC))
{
$contributions[] = array('user_id' => $fetched['user_id'],
'ammount' => $fetched['ammount'],
'date' => $fetched['date']);
}
return $contributions;
Then try below:
$profile_id = 1; // sample id
$result = d4g_get_contributions_info($profile_id);
foreach($result as $row){
$user_id = $row['user_id']
// Continue like this
}
Hi I would like to display the number of item in the database. The following is the php code:
$jobid = $_SESSION['SESS_MEMBER_JOB'];
$data = "SELECT * FROM attributes WHERE jobid = $jobid";
$attribid = mysql_query($data) or die(mysql_error);
$count = "SELECT count(*) FROM attributes WHERE jobid = $jobid";
$database_count = mysql_query($count);
//Declare the Array
$DuetiesDesc = array();
print_r ($database_count);
But instead of getting the desired result, I get :
Resource id #14
Please Assist
Should get it out of the way that you shouldn't be using mysql_* see Why shouldn't I use mysql_* functions in PHP?
See the code below... explanations are in comments
$jobid = $_SESSION['SESS_MEMBER_JOB'];
// escape variables using mysql_real_escape_string
$data = "SELECT * FROM attributes WHERE jobid =".mysql_real_escape_string($jobid);
$attrRes = mysql_query($data) or die(mysql_error());
// I'm assuming you want all of the attributes return in this query in an array
$attributes = array();
while($row = mysql_fetch_assoc($attrRes)){
$attributes[] = $row;
}
// Now if you want the count we have all of the records in the attributes array;
$numAttributes = count($attributes);
// here is an example of how you can iterate through it..
print "<p>Found ".$numAttributes." attributes</p>";
print "<table>";
foreach($attributes as $row){
print "<tr>";
foreach ($row as $cell){
print "<td>".$cell."</td>";
}
print "</tr>";
}
print "</table>";
Try this
<?php
$jobid = $_SESSION['SESS_MEMBER_JOB'];
$data = "SELECT * FROM attributes WHERE jobid =$jobid";
$attribid = mysql_query($data) or die(mysql_error);
$count=mysql_num_rows($attribid);
echo $count;
?>
try this
$jobid = $_SESSION['SESS_MEMBER_JOB'];
$data = "SELECT *FROM attributes WHERE jobid =$jobid";
$attribid = mysql_query($data) or die(mysql_error);
$count = "SELECT count(*) FROM attributes WHERE jobid = $jobid";
$database_count = mysql_query($count);
//Declare the Array
$DuetiesDesc = array();
$database_count=mysql_fetch_assoc($database_count);
echo $database_count['count(*)'];
In the example below I am taking an array of the ID's only to turn it into a while and then back into an array only to explode it on the comma on the page. Surely there must be an easier way of doing this.
I am after an array of the $row['ID']s
function get_other_elector_phone($telephone,$current,$criteria){
$the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = '';
while($row = mysql_fetch_array($the_others)) {
$results .= $row['ID'].','; } return $results;
}
$others = get_other_elector_phone(g('electors',$elector,'telephone'),$elector,$criteria);
if($others){ $others = explode(',',$others);
just as #daverandom said, just rebuild an array, this is the syntax :
function get_other_elector_phone($telephone,$current,$criteria){
$the_others=mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria");
$results = array();
$i=0;
while($row = mysql_fetch_array($the_others)) {
$results [$i]= $row['ID'];
$i++;
}
//results returned outside loop
return $results;
}
$others = get_other_elector_phone(g('electors',$elector,'telephone'),$elector,$criteria);
The $others will return an array.
instead of
function get_other_elector_phone($telephone,$current,$criteria){
$the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = '';
while($row = mysql_fetch_array($the_others)) {
$results .= $row['ID'].','; } return $results;
}
}
just return the result of mysql_fetch_array()
function get_other_elector_phone($telephone,$current,$criteria){
$the_others = mysql_query("SELECT * FROM electors WHERE telephone = '$telephone' $criteria"); $results = '';
return mysql_fetch_array($the_others);
}
then there is no need to explode the output of the function.