我刚学PHP两天,写了一段代码:
<?php
$dbhost = 'localhost';
$dbuser = 'root'; //我的用户名
$dbpass = 'welcome'; //我的密码
$dbname = 'mydb'; //我的mysql库名
$connect = mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
if ($connect) {
echo "非常好,成功了!)";
} else {
echo "不好意思,失败了!";
}
?>
结果错误如下:
Fatal error: Call to undefined function mysql_connect() in D:\myPHP\mytest\conn.php on line 6
哪位大哥给我一段完整详细的代码或是帮我指点错误也行.
谢谢了!
<?php
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo $sql;
mysql_close($conn);
echo "Hello!数据库mycounter已经成功建立!";
?>
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo $sql;
mysql_close($conn);
echo "Hello!数据库mycounter已经成功建立!";
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-06-08
你这个不是数据库的问题,而是你的php还没有支持mysql,重装php,在prefix写上mysql。这样就不会有line 6的错误提示了。
第2个回答 2019-01-24
<?php
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE
DATABASE
mycounter
DEFAULT
CHARACTER
SET
gbk
COLLATE
gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE
TABLE
`counter`
(`id`
INT(255)
UNSIGNED
NOT
NULL
AUTO_INCREMENT
,`count`
INT(255)
UNSIGNED
NOT
NULL
DEFAULT
0,PRIMARY
KEY
(
`id`
)
)
TYPE
=
innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo
$sql;
mysql_close($conn);
echo
"Hello!数据库mycounter已经成功建立!";
?>
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='12345678';
$mysql_database='mycounter';
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database);
$sql='CREATE
DATABASE
mycounter
DEFAULT
CHARACTER
SET
gbk
COLLATE
gbk_chinese_ci;
';
mysql_query($sql);
$sql='CREATE
TABLE
`counter`
(`id`
INT(255)
UNSIGNED
NOT
NULL
AUTO_INCREMENT
,`count`
INT(255)
UNSIGNED
NOT
NULL
DEFAULT
0,PRIMARY
KEY
(
`id`
)
)
TYPE
=
innodb;';
mysql_select_db($mysql_database,$conn);
$result=mysql_query($sql);
//echo
$sql;
mysql_close($conn);
echo
"Hello!数据库mycounter已经成功建立!";
?>