尝试将 xampp 数据库表连接到 php:错误未定义变量:mysqli_query 和致命错误

Posted

技术标签:

【中文标题】尝试将 xampp 数据库表连接到 php:错误未定义变量:mysqli_query 和致命错误【英文标题】:Trying to connect a xampp database table to php: errors Undefined variable: mysqli_query and Fatal error 【发布时间】:2020-10-29 20:36:12 【问题描述】:

我尝试搜索很多关于我无济于事的错误。我对 php 的经验为零,我被扔在了将 XAMPP Apache 数据库显示为浏览器中的表的任务中。领导我的程序员有一个星期的假期,所以我正在寻求帮助。

这是连接数据库的代码:

<?php
$link = mysqli_connect('localhost', 'root','', 'csvdb6');
// mysql_select_db('csvdb6');
if (!$link) 
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

这是要在浏览器中显示的 php 代码:

<?php
include_once('db_connection.php');
$query="select* from pmid";
$result= $mysqli_query($GLOBALS['link'],$query);
?>

<!DOCTYPE html>
<html>
<head>
<title>Table with database</title>
<style>
table 
border-collapse: collapse;
width: 100%;
color: #588c7e;
font-family: monospace;
font-size: 25px;
text-align: left;

th 
background-color: #588c7e;
color: white;

tr:nth-child(even) background-color: #f2f2f2
</style>
</head>
<body>
<table>
<tr>
<th>PMID</th>
<th>ResearchGroup</th>
<th>Species</th>
<th>Method</th>
</tr>
<?php
$servername="localhost";
$username="root";
$password="";
//review below code line
$conn = mysql_connect("localhost", "root", "", "csvdb6");
// Check connection
if ($conn->connect_error) 
die("Connection failed: " . $conn->connect_error);

$sql = "SELECT PMID, ResearchGroup, Species, Method FROM pmid";
$result = $conn->query($sql);
if ($result->num_rows > 0) 
// output data of each row
while($row = $result->fetch_assoc()) 
echo "<tr><td>" . $row["PMID"]. "</td><td>" . $row["ResearchGroup"] . "</td><td>"
. $row["Species"]. "</td></tr>" . $row["Method"]. "</td><td>";

echo "</table>";
 else  echo "0 results"; 
$conn->close();
?>
</table>
</body>
</html>

我现在得到的错误如下:

注意:未定义变量:mysqli_query in C:\xampp\htdocs\scripts\testing2.php 第 4 行

致命错误:未捕获的错误:函数名称必须是字符串 C:\xampp\htdocs\scripts\testing2.php:4 堆栈跟踪:#0 main 抛出 在第 4 行的 C:\xampp\htdocs\scripts\testing2.php 中

我知道你们收到了很多这些“愚蠢的问题”,我感谢您的任何意见和帮助。干杯。

【问题讨论】:

$mysqli_query($GLOBALS['link'],$query); 更改为mysqli_query($GLOBALS['link'],$query);。如果在它前面加上$,它会被读取为变量,而不是函数。 另外,在您的连接代码中,您最终关闭了连接 (mysqli_close($link)),因此如果不建立新连接,您将无法在其余代码中使用它。 您还使用旧的(并且自 PHP 7 已删除)mysql_* API 在代码中进一步建立新连接,但随后您尝试将该连接用作mysqli 连接。 mysql_*mysqli_* 是两个完全不同的 API,不能混用。 【参考方案1】:

当您删除它将运行的美元符号时,您正试图在第 3 行调用名为“$mysqli_query”的变量。

【讨论】:

以上是关于尝试将 xampp 数据库表连接到 php:错误未定义变量:mysqli_query 和致命错误的主要内容,如果未能解决你的问题,请参考以下文章

错误:ORA-00905:将表连接到选择查询时缺少关键字

通过 php 中的 PDO 将 xampp 连接到在线 MSSQL 数据库

无法使用 pdo 连接到 mysql

Mysql - 数据库连接 - xampp - 使用 PHP [重复]

What Joins 用于将多个表连接到一个视图中

如何将带有前缀列的表连接到 PHP MySQL 或 Laravel 中的另外两个表