简体   繁体   English

php、mysql编码错误

[英]Php, mysql coding error

Please tell me that where is error.请告诉我错误在哪里。 I am in tention due to this,我有意为此,

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\\xampp\\htdocs\\kalooo1\\includes\\db.php:2 Stack trace: #0 C:\\xampp\\htdocs\\kalooo1\\index.php(21): include() #1 {main} thrown in C:\\xampp\\htdocs\\kalooo1\\includes\\db.php on line 2致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\kalooo1\\includes\\db.php:2 中未定义的函数 mysql_connect() 堆栈跟踪:#0 C:\\xampp\\htdocs\\kalooo1\\index.php(21) : include() #1 {main} 在第 2 行的 C:\\xampp\\htdocs\\kalooo1\\includes\\db.php 中抛出

db.php数据库文件

<?php
    mysql_connect("localhost","root","");``
    mysql_select_db("kalooo");
?>

index.php(21)索引.php(21)

include("includes/db.php");

You are getting that error simple because you are using a newer version of php that does not support mysql_* functions, those functions have been depreciated and completely removed from the latest version of php.您收到该错误很简单,因为您使用的是不支持mysql_*函数的较新版本的 php,这些函数已被弃用并从最新版本的 php 中完全删除。

You should use mysqli prepared functions or pdo prepared statements.您应该使用mysqli准备好的函数或pdo准备好的语句。

using mysqli to connect to database you will use it like this:使用 mysqli 连接到数据库,您将像这样使用它:

<?php
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdatabse";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

using PDO, you would connect like this:使用 PDO,你会像这样连接:

<?php

$host = 'localhost';
$db   = 'yourdb';
$user = 'yourusername';
$pass = 'yourpassword';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$pdo = new PDO($dsn, $user, $pass, $opt);

?>

The are good tutorials on the net that you can use to better your understanding, I personally enjoy this site https://phpdelusions.net/pdo You should visit it you will be a pro in no time.网上有很好的教程,您可以使用它们来更好地理解,我个人很喜欢这个网站https://phpdelusions.net/pdo您应该访问它,您很快就会成为专业人士。

use mysqli_connect("localhost","root","","db_name");使用mysqli_connect("localhost","root","","db_name"); or use store this in separate php file.或使用将其存储在单独的 php 文件中。 include that file like this.像这样包含该文件。 include 'db.php'; try this....尝试这个....

try this尝试这个

<?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();
  }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM