我正在尝试编写一个从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文件中获取数据&amp;然后将该数据传递给javascript函数。因此我打算调用get_hash();来自不同的php文件。