mysql_connect 不能使用 php?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14392859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:10:36  来源:igfitidea点击:

mysql_connect not working with php?

phpmysqldatabase-connection

提问by user1601000

My code only shows a blank page with no text. Why could the mysql_connectnot connect to the database?

我的代码只显示一个没有文本的空白页面。为什么mysql_connect无法连接到数据库?

I am using MySQL version is 5.0.77 and PHP version is 5.1.6

我使用的 MySQL 版本是 5.0.77,PHP 版本是 5.1.6

$user = "user";
$password = "user";
#$database = "database";

$con = mysql_connect("localhost",$user,$password);

if (!con) {
    echo "could not connect";
} else {
    echo "connected";
}

if (mysql_query("CREATE DATABASE my_db",$con)) {
     echo "databse created";
}

#@mysql_select_db($database) or die( "Unable to select database");
#$query="CREATE TABLE tablename(id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,field1-name varchar(20) NOT NULL,field2-name varchar(20) NOT NULL,field3-name varchar(20) NOT NULL,field4-name varchar(30) NOT NULL,field5-name varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
#mysql_query($query);
mysql_close($con);

回答by NullPoiиteя

Well i wold like to point out one by one

我想一一指出

1.you are using !con which is invalid and generate error

1.您使用的 !con 无效并产生错误

Notice: Use of undefined constant con - assumed 'con' in blabla on line bla connected

注意:在 bla 连接的 blabla 中使用未定义的常量 con - 假定 'con'

to use like this you need to declare constant which i think you don't want so use

像这样使用你需要声明常量,我认为你不想要这样使用

if(!$con){


2.The entire ext/mysqlPHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0and will be removed in the future. So use either PDOor MySQLi

2.整个ext/mysqlPHP 扩展,提供所有以前缀 mysql_ 命名的函数,从 PHPv5.5.0 开始正式弃用,将来会被删除。所以使用PDOMySQLi

Good read

好读

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
  3. Pdo Tutorial For Beginners
  1. mysql 扩展已弃用,将来会被删除:改用 mysqli 或 PDO
  2. MySQL 开发人员的 PDO 教程
  3. Pdo 初学者教程


3.as i saw in comment you have not turned on error reporting you can do this by in php.ini file

3.正如我在评论中看到的,您没有打开错误报告,您可以在 php.ini 文件中执行此操作

ini_set('display_errors', 1);
error_reporting(E_ALL);


4.do not use #for comment however its same as //comment and cause no problem till 5.5.0alpha3live result. however its(#) deprecated Comments starting with '#' are now deprecated in .INI files.so its better to use

4.不要#用于评论,但它与//评论相同,并且在5.5.0alpha3实时结果之前不会造成任何问题。然而,它的( #) 已弃用 以“#”开头的注释现在在 .INI 文件中已弃用。所以最好使用

  1. For single line comment // this is single line comment
  2. For multiline comment /* this is multiline comment */
  1. 对于单行注释 // this is single line comment
  2. 对于多行注释 /* this is multiline comment */

回答by Authorize

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. Check here for detailed information

这个扩展在 PHP 5.5.0 中被弃用,在 PHP 7.0.0 中被移除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。 在这里查看详细信息

回答by ????? ???

First try Following code only

首先尝试以下代码

<?php
$user="user";
$password="user";
$con=mysqli_connect("localhost",$user,$password);
if(!$con)
{
echo "could not connect" ;
}
else
{
echo "connected";
}
?>

Then see the result and update here

然后在这里查看结果并更新

回答by George

Use mysqli_connectinstead of mysql_connect.

使用mysqli_connect代替mysql_connect