Why i cannot pass array to function parametter with two items? here is the function, but i get just the first parameter Username

function selectSQL(Array $item, $table) {
    global $conn;
    foreach ($item as $field) {
        $sql = "SELECT `$field` FROM `$table`";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                return $row["$field"];
            }
        }
    }
}

calling it like this:

$selectFields = Array("Username", "Email");
echo selectSQL($selectFields, "upstrey");
Member Avatar for diafol

Not sure what you're trying to do. This works:

function selectSQL(Array $item, $table) {
    //global $conn;
    foreach ($item as $field) {
        echo $field;
    }
}

$selectFields = Array("Username", "Email");
selectSQL($selectFields, "upstrey");

Your problem is the return. You can only return once - so it breaks out of the loop and exits function after first iteration.

Im trying to make a simple function to select from different tables and fields in Database

This code only retrives the first value which is Username=Stefan

Im really confuzed someone please help ?

Member Avatar for diafol

I told you about the return issue

Even if i put out of the loop i get only the first result

function selectSQL(&$item, $table) {
    global $conn;
    foreach ($item as $field) {
        $sql = "SELECT `$field` FROM `$table`";
        $result = $conn->query($sql);
    }
    if ($result === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
    } else if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $var = $row["$field"];
        }
    }
    return $var;
}
Member Avatar for diafol

Obviously. You.re only returning one value. the last one in this case. Either change the return to an echo inside the loop or return an array and process further outside the function.

Actually it works changing to echo but im curious how would i do it with arrays ?

I was trying like this but as you said its breaking after first retrived value;

function selectSQL(&$item, $table) {
    global $conn;
    foreach ($item as $field) {
        $sql = "SELECT `$field` FROM `$table`";
        $result = $conn->query($sql);

        if ($result === false) {
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
        } else if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $arrayName = array($row["$field"]);
            }
        }
    }
    return $arrayName;
}

My PHP is more than a little rusty and my SQL even rustier, but this looks off...

            while($row = $result->fetch_assoc()) {
                $arrayName = array($row["$field"]);
            }

Looks like $arrayName will keep getting overwritten in the while loop? Are you trying to APPEND to $arrayName every trip through the while loop? If so, I imagine this would be better?

            $arrayName = array();
            while($row = $result->fetch_assoc()) {
                $arrayName[] = array($row["$field"]);
            }

Not 100% sure what is supposed to be in $arrayName in the end, but from your posts/code it seems like you are writing over $arrayName in your code and do not want to.

Member Avatar for diafol

Not sure whether you're using pdo or mysqli but pdo has a fetchAll method to get all db data. This can be returned for further processing. The mysqli version is a bit more tiresome. However if you want to run a loop and then return...

$r =[];
while(...){
    $r[] = ...;
 }
return $r;

Thank you guys, but i have another problem now i want to return array of the fields so i can use them, this code it gives me wierd results

function selectSQL(&$item, $table) {
    global $conn;
    foreach ($item as $field) {
        $sql = "SELECT `$field` FROM `$table`";
        $result = $conn->query($sql);

        if ($result === false) {
            trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
        } else if ($result->num_rows > 0) {
            $arrayName = array();
            while($row = $result->fetch_assoc()) {
                $arrayName[] = array($row["$field"]);
                echo var_dump($arrayName);
            }
        }
    }
}

Array ( [0] => Array ( [0] => Stefan ) ) 1 Array ( [0] => Array ( [0] => stefan@gmail.com ) ) 1

I have finnaly make it here is what i was searching.

function selectSQL(&$item, $table) {
    global $conn;

    $fields = implode(',', $item);

    $sql = "SELECT $fields FROM `$table`";
    $result = $conn->query($sql);

    if ($result === false) {
        trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $link->error, E_USER_ERROR);
    } else if ($result->num_rows > 0) {
        $fieldValue = array();
        while($row = $result->fetch_assoc()) {
            $itemsArray = array();
            foreach($item as $field) {
                $itemsArray[$field] = $row[$field];
            }
            $fieldValue[] = $itemsArray;
        }
        return $fieldValue;
    }
}

$selectFields = array("ID", "Username", "Email");
$output = selectSQL($selectFields, "upstrey");
$arrlength = count($output);
for ($i=0; $i < $arrlength; $i++) {
    echo "ID: " . $output[$i]["ID"] . "<br>";
    echo "Username: " . $output[$i]["Username"] . "<br>";
    echo "Email: " . $output[$i]["Email"] . "<br>";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.