无法将mysqli响应显示为字符串

时间:2018-04-29 20:59:55

标签: php mysqli

我正在尝试在php中创建一个web服务,这是我到目前为止,它返回的东西,但只有null,我无法弄清楚为什么会这样。

我知道数据库连接正在运行,但此代码仍然只返回NULL。我不知道我得到了什么,一个mysqli对象?我需要它作为一个字符串。在php中工作时调试不顺利,我得到服务器错误500响应很多。我已经尝试了几天,我无法让它工作,所以我需要帮助。

我在这里使用我的功能,也许我会以完全错误的方式完成这个。

我的代码:

//Create connection to DB and return it
function db_connect() {

  // Define connection as a static variable, to avoid connecting more than once 
  static $connection;

  // Try and connect to the database, if a connection has not been established yet
  if(!isset($connection)) {

      $servername = "myservername";    
      $dbname = "mydbname";
      $password = "secret";
      $username = "username";
      $connection = new mysqli($servername,$username,$password);  
  }

  // If connection was not successful, handle the error
  if($connection->connect_error) {
      // Handle error - notify administrator, log to a file, show an error screen, etc.
      $file = "log.txt";
      $message = "The connection was bad...";
      file_put_contents($file, $message, FILE_APPEND | LOCK_EX);
      return mysqli_connect_error(); 
  }
  else
  {
      $file = "log.txt";
      $message = "The connection was good...";
      file_put_contents($file, $message, FILE_APPEND | LOCK_EX);
  }
  return $connection;
}

我的数据库连接功能:

{{1}}

我正在尝试学习一些只是为了好玩的PHP。

0 个答案:

没有答案