【问题标题】:I have error in this code (mysql and php)? [duplicate]我在这段代码中有错误(mysql和php)? [复制]
【发布时间】:2017-12-24 13:48:20
【问题描述】:

这是我的 config.php

<?php mysql_connect('localhost', 'id2266180_test', 'testtest'); mysql_select_db ('id2266180_test');?>

这是错误

致命错误:未捕获错误:调用未定义函数 /storage/ssd2/180/2266180/public_html/config.php:9 中的 mysql_connect() 堆栈跟踪:#0 /storage/ssd2/180/2266180/public_html/index.php(3): include() #1 {main} 抛出 /storage/ssd2/180/2266180/public_html/config.php 在第 9 行

【问题讨论】:

  • mysql_* 函数从 PHP7 开始被删除。而是使用mysqli_* 函数。
  • MySQL 没了,如果你想使用它需要降级到 PHP 5.6

标签: php mysql


【解决方案1】:

您可以尝试以下代码:

<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");

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

【讨论】:

    【解决方案2】:

    您应该检查是否已加载 mysql 模块。尝试找出:

    <?php
        phpinfo();
    ?>
    

    如果没有加载,你必须编辑php.ini,通过加载mysql-module。但请注意,mysql-extension 自 PHP 5.5 起弃用,自 PHP 7.0 起删除。所以试着改用mysqli,就像其他人已经说过的那样。

    【讨论】:

      【解决方案3】:

      第一件事是 mysql_select_db 已被废弃。 使用此代码:

      <?php
      $con=mysqli_connect("localhost","my_user","my_password","my_db");
      // Check connection
      if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
      
      // ...some PHP code for database "my_db"...
      
      // Change database to "test"
      mysqli_select_db($con,"test");
      
      // ...some PHP code for database "test"...
      
      mysqli_close($con);
      ?> 
      

      【讨论】:

      • 如果我更改它Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd2/180/2266180/public_html/index.php on line 70 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /storage/ssd2/180/2266180/public_html/index.php on line 71 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /storage/ssd2/180/2266180/public_html/index.php on line 72,我会在index.html 中收到此警告
      • 你在 mysqli_query() 中传递了连接对象吗?
      【解决方案4】:

      你也可以这样试试:

      <?php mysqli_connect('localhost','id2266180_test','testtest','id2266180_test');?>
      

      【讨论】:

      • 如果我更改它Warning: mysqli_query() expects at least 2 parameters, 1 given in /storage/ssd2/180/2266180/public_html/index.php on line 70 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /storage/ssd2/180/2266180/public_html/index.php on line 71 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /storage/ssd2/180/2266180/public_html/index.php on line 72,我会在index.html 中收到此警告
      • 请给我完整的代码
      • 你的连接好像没有建立..!
      • 我会给你我的用户名和密码并请修复它
      • 不,没必要。让我们创建一个聊天
      猜你喜欢
      • 1970-01-01
      • 2018-02-15
      • 2012-07-17
      • 2016-11-29
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      相关资源
      最近更新 更多