【问题标题】:PHP file on website - Getting fatal error in mysql_connect()网站上的 PHP 文件 - 在 mysql_connect() 中出现致命错误
【发布时间】:2015-03-30 06:53:54
【问题描述】:

我正在尝试在我的网站上托管此 php 文件以连接到 MySQL 数据库。

<?php

// Create connection
$con=mysqli_connect("localhost","username","pass","database");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

// Loop through each row in the result set
while($row = $result->fetch_object())
{
    // Add each row into our results array
    $tempArray = $row;
    array_push($resultArray, $tempArray);
}

// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}

// Close connections
mysqli_close($con);
?>

当我尝试使用 http://writecodeonline.com/php/ 验证代码时,它说

致命错误:在第 2 行调用未定义函数 mysqli_connect()

四处搜索说我需要更改 php.ini 文件,但我的网站上什至没有托管文件。代码有问题吗?

当我尝试在 www.mydomain.com/service.php 访问它时,它说找不到文件错误...但它肯定存在。我正在学习本教程 - http://codewithchris.com/iphone-app-connect-to-mysql-database/

【问题讨论】:

  • 在不属于您的网站上测试 php 将不允许您连接到 /the/their 的数据库。如果您想测试服务器端脚本,我建议您安装 wamp 服务器。我发现很难理解您的问题,因为您似乎在问题中回答了它。我希望这篇文章有所帮助。

标签: php mysql sql hosting


【解决方案1】:

你需要在你的服务器上安装 php-mysql 依赖。

yum install php-mysql -y

或您的操作系统上的等价物。

【讨论】:

  • 如果你看那个教程,他们没有安装任何东西,它工作。这就是为什么我很困惑。我正在使用同一个网站来托管数据库(Bluehost)。它不应该已经有了吗?
  • 您的代码没有任何问题。但这就是那个错误的意思。
猜你喜欢
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多