0

I am working with this php code which makes a query and then displays the information in a table. There is a column returned from this query called 'queue' which I want to store in a variable called '$queue_tittle' and then display this variable in index.php but when I try to do so, the variable is empty, it does not display any information.

query.php

<?php
session_start();
$queue_tittle = "";

function fill(){
    
    if (isset($_GET['vid'])){
        $vid = $_GET['vid'];

        if (!$conn = new mysqli("localhost", "root", "", "zendesk_data")){
            $output="Connection failed: " . $conn->connect_error;      
            } 
        else {
            $sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach, queue FROM $vid";
            if ( $result = $conn->query($sql) ){
                if ($result->num_rows > 0) {
                    $output="<table class='queue_table'> 
                    <tr align='left'>
                    <th>Status</th>
                    <th>ID</th>
                    <th>Subject</th>
                    <th>Requester</th>
                    <th>Requested</th>
                    <th>Requested Updated</th>
                    <th>Service</th>
                    <th>Next SLA Breach</th></tr>";
                    while($row = $result->fetch_assoc()) {
                        $queue_tittle = $row["queue"];
                        $output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
                        $row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
                    }
                    $output.="</table>";
                } else { 
                    $output= "0 results"; 
                }
            } else {
                    $output="Error en la consulta: ".$conn->error;
            }
            $conn->close();
        }
        echo $output;
        }    
    } 
?>

index.php

<div id="inner_cont">
   <div id="queue">
      <?php
         include '../utilities/query.php';
         fill();
         echo $queue_tittle;
       ?>  
  </div>

I know this is may be an easy question but I have tried to follow online tutorials to solve this but nothing seems to work for my case. All help will be highly appreciated, thanks beforehand.

2
  • What are query.php and fill.php? is there a mistake in file names? Commented Nov 9, 2020 at 19:35
  • No answer (you already got some) but please read about SQL injections (stackoverflow.com/a/60496/2008111) Commented Nov 9, 2020 at 19:36

2 Answers 2

1

You could just return the title from the function.

function fill() {
    $queue_tittle = "";
    // ...
        echo $output;
    }
    return $queue_tittle;
}

and

<?php
include '../utilities/fill.php';
$queue_tittle = fill();
echo $queue_tittle;
?>

If you need to return more bits of data, you can wrap them in an associative array.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to add global into fill to access $queue_tittle - like so:

function fill(){
    global $queue_tittle;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.