Convert local variable to global. PHP

0

I am trying to collect the fields from a MySQL registry, and for this I use the following:

$reg = mysql_query("SELECT * FROM draws WHERE urlID='$urlID'");

    while($data = mysql_fetch_array($reg)) {
        $title = $data ['title'];
    }

Everything is collected correctly, but now I need to access "$ title" which is a local variable, I imagine. How can I do it?

I have tried to define previously the function of this mandera:

$title ='';

To try to keep it global, and simply overwrite its value, but it does not work. Can someone help me?

Thank you.

    
asked by ByBrayanYT - Tops y más 20.05.2018 в 11:54
source

3 answers

2

In the example that you expose $title belongs to the global scope, you can print it after while calmly

$reg = mysql_query("SELECT * FROM draws WHERE urlID='$urlID'");

    while($data = mysql_fetch_array($reg)) {
        $title = $data ['title'];
    }
// $title tendrá el valor de la ultima iteracion del while
echo $title;

If what you mean is that the piece of code is inside a function, you can return the value. Example:

function mi_funcion() {
    $reg = mysql_query("SELECT * FROM draws WHERE urlID='$urlID'");

        while($data = mysql_fetch_array($reg)) {
            $title = $data ['title'];
        }
    // retornamos el último valor de $title 
    return $title;
}
// asignamos el valor de retorno de la función a $titulo en el ámbito global
$titulo = mi_funcion();
echo $titulo;

If you want to pass a variable from the global scope to be used inside a function, you can pass it in the arguments of that function. Example:

function mi_funcion($titulo) {
    // hacer lo que necesitemos con titulo
    echo $titulo;
}

$var = 'Esto es un titulo';
mi_funcion($var);

You can see an explanation of the use of global variables in What is the correct use of functions and global variables? and scope of variables in What is the scope and "real" life cycle of a global variable in PHP?

    
answered by 20.05.2018 в 13:21
0

with the supervariable "globals"

example:

<?php
function test() {
    $foo = "variable local";

    echo '$foo en el ámbito global: ' . $GLOBALS["foo"] . "\n";
    echo '$foo en el ámbito simple: ' . $foo . "\n";
}

$foo = "Contenido de ejemplo";
test();
?>

If you need more info I leave a referenced link: link

    
answered by 20.05.2018 в 12:33
0

I do not understand what's the point of your code: go through all the records to stay with only the last one.

If that is the intention the best thing would be to filter the query so that it brings you that single record you want.

The problem that arises about the scope of the variable of interest could be resolved with a function that returns the result.

For example, this query would bring you the last record, assuming you have a% incremental auto% column.

function getTitulo($urlID){
    $title="";
    $sql="SELECT title FROM draws WHERE urlID='$urlID' ORDER BY id DESC LIMIT 1";
    $reg = mysql_query($sql);
    /*Dado que es un solo registro, no hace falta el while*/
    $data = mysql_fetch_array($reg);
    $title = $data ['title'];

    return $title;
}

If on the other hand you want all the records, then the correct thing would be to declare the variable outside the id as an array, which you would then fill in the while :

function getTitulo($urlID){
    $arrTitles=array();
    $sql="SELECT title FROM draws WHERE urlID='$urlID'";
    $reg = mysql_query($sql);

    while($data = mysql_fetch_array($reg)) {
        $arrTitles = $data ['title'];
    }
    return $arrTitles;
}

To use both functions from anywhere you just have to worry that the while function is in the scope where you want to use it. Then you call it, passing it the parameter and you store the value that the function returns or you use it directly.

$datosDeGetTitle=getTitle('elIdURL');

In getTitle() you will have what the function returned.

NOTES:

  
  • Depending on the scope and organization of your program, this could also be done through $datosDeGetTitle , creating instances of a   class which would have methods to return the different   elements.
  •   
  • Querying your code is vulnerable to SQL injection attacks. To shield the code it will be necessary, if it is possible for you, to go from   the extension clases to mysql.* or a mysqli . The extension you use   now it has been declared obsolete and will stop working in PHP 7.
  •   
    
answered by 20.05.2018 в 21:17