简体   繁体   English

mysql_connect无法使用php?

[英]mysql_connect not working with php?

My code only shows a blank page with no text. 我的代码只显示一个没有文字的空白页面。 Why could the mysql_connect not 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);

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 注意:使用未定义的常量con - 假设'con'在blabla线上bla连接

to use like this you need to declare constant which i think you don't want so use 要像这样使用你需要声明常量,我认为你不想这样使用

if(!$con){

2. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. 2.整个ext/mysql PHP扩展,提供所有以前缀mysql_命名的函数, 从PHP v5.5.0开始正式弃用,将来会被删除。 So use either PDO or MySQLi 所以使用PDOMySQLi

Good read 好读

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead 不推荐使用mysql扩展,将来会删除它:使用mysqli或PDO代替
  2. PDO Tutorial for MySQL Developers MySQL开发人员的PDO教程
  3. Pdo Tutorial For Beginners 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.0alpha3 live result . 4.不要使用#作为注释,但与//注释相同,直到5.5.0alpha3 实时结果 才会导致问题。 however its( # ) deprecated Comments starting with '#' are now deprecated in .INI files. 但是它的( # )已弃用。以'#'开头的注释现在已在.INI文件中弃用。 so its better to use 所以它更好用

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

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 然后查看结果并在此处更新

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 此扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。 Instead, the MySQLi or PDO_MySQL extension should be used. 相反,应该使用MySQLi或PDO_MySQL扩展。 Check here for detailed information 点击此处查看详细信息

使用mysqli_connect而不是mysql_connect

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

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