Javascript 有没有更好的方法在php中编写folling代码

Javascript 有没有更好的方法在php中编写folling代码,javascript,php,mysql,Javascript,Php,Mysql,我正在尝试编写一个函数,从mysql获取一列并返回它。一些我无法编写get_hash()函数的原因。此外,使用$GLOBALS安全吗 <?php //Create Database Connection $dbhost = "localhost"; $dbuser = "developer"; $dbpass = "abc"; $dbname = "abc"; $connection = mysqli_connect($dbhost,$db

我正在尝试编写一个函数,从mysql获取一列并返回它。一些我无法编写get_hash()函数的原因。此外,使用$GLOBALS安全吗

<?php
    //Create Database Connection 
    $dbhost = "localhost";
    $dbuser = "developer";
    $dbpass = "abc";
    $dbname = "abc";
    $connection = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

    //Test Connection`enter code here`
    if(mysqli_connect_errno()) {
        die("Connection Failed" . 
            mysqli_connect_error() . 
            "(" . mysqli_connect_errorno() . ")"
        );  
    }

    // Query
    $query = "SELECT tweet FROM twc_hashtag";


    // Fetch Data
    function get_hash() {   
        if ($result=mysqli_query($GLOBALS['connection'], $GLOBALS['query'])){  

                while ($row=mysqli_fetch_row($result)){
                    //echo "<tr>";
                    //echo "<td>" . $row[0] . "</td>";
                    //echo "</tr>";
                    return $row[0];
                }   
            }

        //Test Query Error
        if(!$result){
            die("Database Query Failed" . mysqli_error($GLOBALS['connection'])); 
        }   

    }


    //Close The Connection
    mysqli_close($connection);

?> 


我需要在不同的php文件中获取数据&然后将该数据传递给javascript函数。因此,我将调用get_hash();从另一个php文件。

什么不起作用,确切地说?我建议在关闭数据库连接之前调用get_hash()函数
global
$GLOBALS
都是错误的标志。它们不应该是必要的。通过函数的参数列表将
$connection
$query
传递给函数。这就是它存在的原因。警告:mysqli_query():无法在第23行的/var/www/html/himanshu/db_fetch.php中获取mysqli警告:mysqli_error():无法在第35行数据库查询的/var/www/html/himanshu/db_fetch.php中获取mysqliFailed@Himanshu1988关闭连接其实并不重要,除非您将数据提供给许多用户,否则在执行结束时它会自动关闭。