-1

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>

2 Answers2

1

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>';
?>

ArnoHolo
  • 311
  • 3
  • 13
  • Doesn't seem to work. The textarea has a character inside it but none of the information that should be inside it. – Drago100ful Jun 11 '16 at 13:34
  • Try to detect on which part of your script there is a problem, add `echo $conn;` or `echo $result;` or `var_dump($ticketLabel);` like Fil said and see if what's displayed seems to be right. – ArnoHolo Jun 11 '16 at 13:40
  • The variable stays NULL. That's my initial problem – Drago100ful Jun 11 '16 at 13:44
  • $ticketLabel . So I guess the variable isn't set properly or I might have a typo in the sql query – Drago100ful Jun 11 '16 at 13:45
1

Firstly, your SQL query is constructed wrongly, you're mixing up single and double quotes. I believe it should be:

$sql = 'SELECT label FROM data WHERE priceID = "' . $priceID . '"';

Next, if I understand your requirement correctly, you only expect at most one row to be returned from that query, so fetch_assoc is enough to retrieve the single row. You don't need the while loop.

So the whole function should be:

public static function priceIDToLabel($priceID){
    $conn = global_functions::connectdb();
    $errormsg = '';
    $sql = 'SELECT label FROM data WHERE priceID = "' . $priceID . '"';
    $result = $conn->query($sql);
    $result_row = $result->fetch_assoc();

    if (!empty($result_row))
        return($result_row['label']);
    else
        return "";
}

But stop!

You really should consider using PDO to prepare your queries. If you are using MySQL, there are many tutorials out there to follow. As mentioned in the comments, SQL injections are really easy to combat but can cause serious damage if it happens to you. So make sure you don't let this function go into production like that.

Irvin Lim
  • 2,393
  • 17
  • 20
  • Thanks really didn't notice it. I might consider PDO in later projects – Drago100ful Jun 11 '16 at 13:48
  • 2
    @Drago100ful You really should go and learn PDO, it's fairly simple to implement but it helps prevent one of the easiest form of attacks on your database. Make sure that this code doesn't go into production like this. – Irvin Lim Jun 11 '16 at 13:50