Function inside while inside function inside while only triggers once each layer - php

Consider this (simplified) code... Function getLevelOne is called, runs fine, calls getLevelTwo, runs fine, and then stops, without continuing the while that was called in getLevelOne... The way its written, you'd assume that the second function would be called for each number in $list1 (2,4,5,6)... Am I missing something?
$list1 = "2,4,5,6";
$table1 = 'thistable';
getLevelOne($list1, $table1);
function getLevelOne($list1, $table1){
$q = "select * from $table1 where id IN ('$list1')";
$r = mysqli_query($db, $q);
while($row = mysqli_fetch_array($r)){
echo 'oh';
$table2 = 'nexttable';
$list2 = $row[$table2];
getLevelTwo($list2, $table2);
}
}
function getLevelTwo($list2, $table2){
$q2 = "select * from $table2 where id IN ('$list2')";
$r2 = mysqli_query($db, $q2);
while($row2 = mysqli_fetch_array($r2)){
echo 'shit';
}
}

Related

create function in php include while loop

i have this code to call posts from database
<?php
$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
while($post_info = mysqli_fetch_assoc($r)) { ?>
//html
it works fine but i like to create a function includes the query and the while loop and move it to functions.php and keep the html code in template.php but I don't know how with the while loop..
function data_post($dbc, $user_info['id']){
$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
while($post_info = mysqli_fetch_assoc($r))
return $post_info
}
I have tried this but there is no result
function ss()
{
...........
while($post_info = mysqli_fetch_assoc($r))
{
$new_array[]=$post_info;
}
return $new_array;
}
And return the $new_array outside of the while loop
And get the value like this
$mm= ss();
print_r($mm); here you get that $new_array values
Try this:
Function Body
function data_post($dbc, $user_id){
$q = "SELECT * FROM posts WHERE user_id = '$user_id' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);
$arrayPost = array();
while($post_info = mysqli_fetch_assoc($r)){
$arrayPost[] = array('id' => $post_info['postId'], 'name' => $post_info['postName']);
// or whatever data you want to return of post, insert in array
}
return $arrayPost;
}
Function Calling
$userPostArray = data_post($dbc, $user_info['id']);

Msqli query array

So I have my code
function GetApi($connection,$UserId){
global $Apicall;
$Apicall = array();
$Apiidquery = mysqli_query($connection, "SELECT ID FROM ` Characterapi` WHERE UserId = '$UserId'");
while($results = mysqli_fetch_assoc($Apiidquery)){
$Apicall[] = $results['ID'];
}
}
The output of this function if I call
$Apicall[0] = 3
$Apicall[1] = 11
and this is the information I want. But now I want to use a function like
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$Keyidquery = array();
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ID = '$Apicall'");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
This code does run if i set $Apicall ="3"; The issue im having is that I want the first function to get All the IDs associated with $userId in my data base then for each Id run the second function to to get the two specific pieces of information from that query.
In response to the comment below, this is the solution which I would use. However you should be wary of using this method as it does not parameterize the values, and as such not sanitized.
<?php
function Keyquery($Apicall,$connection ){
global $keyidcall, $keyid ,$Vcode;
$string = "ID IN('";
$string.= implode("','", $Apicall);
$string.="')";
$Keyidquery = mysqli_query($connection, "SELECT keyid, Vcode FROM `Characterapi` WHERE ".$string.";");
$results = mysqli_fetch_object($Keyidquery);
$keyid = $results->keyid;
$Vcode = $results->Vcode;
}
?>

Variable is undefined inside a function, cannot reach MySQL $connection inside a function

I have a code that works when I use it on a page but Im trying to make this a function. I cant get it to work, it seems like the variables $customer and $system arent being sent through to the code. Even if I type it in Raw. Any idea whats wrong? $Customer is the name of the customer, $system can be 'Source' or 'Target'.
function status_total($customer, $system){
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
$customer_selection = mysqli_query($conn,$sql_customer);
$customer_row = mysqli_fetch_assoc($customer_selection);
$env_lines = $customer_row["Env_Lines"];
$cust_id = $customer_row["Cust_ID"];
$sql_last_records = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
$record_selection = mysqli_query($conn, $sql_last_records);
$result = mysqli_fetch_all($record_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else
echo "Fail";
}
https://gist.github.com/R2D2-05/78d81566e4bf0eafd1fa
The problem with your code is $conn variable which is treated a local variable inside a function. You should:
function status_total($customer, $system){
global $conn;
$sql_customer = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
$customer_selection = mysqli_query($conn,$sql_customer);
$customer_row = mysqli_fetch_assoc($customer_selection);
$env_lines = $customer_row["Env_Lines"];
$cust_id = $customer_row["Cust_ID"];
$sql_last_records = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
$record_selection = mysqli_query($conn, $sql_last_records);
$result = mysqli_fetch_all($record_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else
echo "Fail";
}
Or you can also pass the $conn through the function, so change the function's definition to:
function status_total($conn, $customer, $system){...}

String delimiter to read table data mysql

instead of using String delimiter, is there any other way to do on line 7?
$q = "SELECT * FROM user";
$res = mysqli_query($conn, $q) or die(mysql_error());
$userList = "";
while($user = mysqli_fetch_array($res)) {
$userList .= $user['userList'].";;";
}
echo $userList;
$userList .= $user['userList'].";;";
means the variable named userList will be appended with the result from the query. after each result you are appending ";;" because of the period.
if you dont want to append the ;; do the following:
$q = "SELECT * FROM user";
$res = mysqli_query($conn, $q) or die(mysql_error());
$userList = "";
while($user = mysqli_fetch_array($res)) {
$userList .= $user['userList'];
}
echo $userList;

How to reuse the resulted variable after executing a mysql query

I am executing a query like this (PHP + MySQL):
$query = "SELECT * FROM tablename WHERE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
// PHP Statement
}
I want to use the same result again on the same page then i need to execute query again like this:
$query = "SELECT * FROM tablename WHERE 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
// PHP Code
}
then it works. But if I use only
while($row = mysql_fetch_array($result))
{
// PHP Code
}
Then it doesn't work. Is there any other way to use the result many times on the same page without executing query every time?
I know i can use the same result to make an array. but is there any other way?
I believe mysql_data_seek will do this for you.
<?php
function mysql_pointer_position($result_set) {
$num_rows = mysql_num_rows($result_set);
$i = 0;
while($result = mysql_fetch_array($result_set)) {
$i++;
}
$pointer_position = $num_rows - $i;
//Return pointer to original position
if($pointer_position <= $num_rows - 1) {
mysql_data_seek($result_set, $pointer_position);
}
return $pointer_position;
}
?>

Categories