Drago100ful Drago100ful - 5 months ago 19
HTML Question

PHP function does not return array

I've been trying to extract an array from a PHP function. But I only get a NULL. Here's the code that the function involves:

Function itself:

public static function priceIDToLabel($priceID){
//Ermittelt mithilfe der PriceID das dazugehörige Ticket -> (label)
$conn = global_functions::connectdb();
$errormsg = '';
$sql = 'SELECT label FROM data WHERE priceID = ".$priceID."';
$result = $conn->query($sql);
//$ticketLabel = array();
while ($ticketLabel = $result->fetch_assoc()){

$ticketLabel[] = $result->fetch_assoc();

}
return($ticketLabel);
}


The site that should display the Array from the function above:

<table>
<!-- WARUM wird $ticketLabel nicht gefüllt, bzw nicht angezeigt?! -->
<!-- Weil wegen isset! Und zu wenig Kaffee!.. -->
<?php

$ticketLabel['label'] = global_functions::priceIDToLabel($selTicket);

echo '<textarea readonly>' .$ticketLabel['label'] . ' </textarea>';

?>


</table>

Answer

If you have only one label for this price, your loop is useless here, since you will not get an array of data, but just one data.

So, try with :

public static function priceIDToLabel($priceID){
    //Ermittelt mithilfe der PriceID das dazugehörige Ticket -> (label)
    $conn = global_functions::connectdb();
    $errormsg = '';
    $sql = 'SELECT label FROM data WHERE priceID = ".$priceID."';
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    return($row['label']);
}

And you don't need to use a ticketLabel array as well, just a label variable :

<table>
<!-- WARUM wird $ticketLabel nicht gefüllt, bzw nicht angezeigt?! -->
<!-- Weil wegen isset! Und zu wenig Kaffee!.. -->

<?php
    $label = global_functions::priceIDToLabel($selTicket);
    echo '<textarea readonly>' . $label . ' </textarea>';
?>

--

Or, if you really want the function to return the whole data object, just change the return argument :

Try with :

public static function priceIDToLabel($priceID){
    //Ermittelt mithilfe der PriceID das dazugehörige Ticket -> (label)
    $conn = global_functions::connectdb();
    $errormsg = '';
    $sql = 'SELECT label FROM data WHERE priceID = ".$priceID."';
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    return($row);
}

And :

<table>
<!-- WARUM wird $ticketLabel nicht gefüllt, bzw nicht angezeigt?! -->
<!-- Weil wegen isset! Und zu wenig Kaffee!.. -->

<?php
    $ticketLabel = global_functions::priceIDToLabel($selTicket);
    echo '<textarea readonly>' .$ticketLabel['label'] . ' </textarea>';
?>

Comments