警告:mysql_connect():读取问候数据包时出错 [英] Warning: mysql_connect(): Error while reading greeting packet
问题描述
<?php``
if(mysql_connect("localhost","root",""))
echo"connect";
else
echo "not connect";
?>
这是我的代码,但未连接.将错误提示为警告
this is my code but it's not connected . give the error as warning
警告:mysql_connect():MySQL服务器已在第2行的C:.. \ pro1.php中消失
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2
警告:mysql_connect():读取问候数据包时出错.第2行的C:.. \ pro1.php中的PID = 2296
Warning: mysql_connect(): Error while reading greeting packet. PID=2296 in C:..\pro1.php on line 2
警告:mysql_connect():MySQL服务器在第2行的C:.. \ pro1.php中消失了 无法连接
Warning: mysql_connect(): MySQL server has gone away in C:..\pro1.php on line 2 not connect
推荐答案
您可以尝试使用MySQLi
或PDO
及其准备好的语句,以提高安全性.
You can try using either MySQLi
or PDO
and their prepared statement to be more secure.
如果您打算仅使用MySQL,请使用以下代码初始化数据库连接.
If you intend using just MySQL then use the below code to initialize your Database connection.
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
有关参考,请参见 http://php.net/manual/zh/function .mysql-connect.php
或者使用MySQLi
Alternatively kindly use MySQLi
<?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();
}
?>
有关参考,请参见 http://php.net/manual/en/mysqli.construct .php
如果您考虑使用PDO,请尝试
If you consider using PDO then try
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
foreach($dbh->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
有关参考,请参见 http://php.net/manual/en/pdo.connections .php
这篇关于警告:mysql_connect():读取问候数据包时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!