phpMyadmin是一個被普遍應用的網路資料庫管理系統,使用起來較為簡單,可以自動創建,也可以運行SQL語句創建,下面分別演示兩種方式創建Mysql資料庫的步驟:
一、使用菜單自動創建資料庫
登陸phpMyAdmin
在php MyAdmin右邊窗口中,填寫資料庫名稱,點創建即可。 例如我們這里創建一個名字為:cncmstest 的資料庫
備註:其實兩種創建資料庫方式本質是一樣的,第一種是利用系統菜單自動組合建庫SQL並執行,第二種是手動輸入建庫SQL,如果熟悉SQL操作的話,第二種更快捷。
B. 如何實現PHP自動創建資料庫
你做好程序以後,把資料庫導出成sql文件
1、連接資料庫
2、讀取這個sql文件里的sql語句,並執行
3、生成一個資料庫連接參數的php文件
<?php
$con=mysql_connect("localhost","peter","abc123");
if(!$con)
{
die('Couldnotconnect:'.mysql_error());
}
if(mysql_query("CREATEDATABASEmy_db",$con))
{
echo"Databasecreated";
}
else
{
echo"Errorcreatingdatabase:".mysql_error();
}
mysql_close($con);
?>
<?php
classReadSql{
//資料庫連接
protected$connect=null;
//資料庫對象
protected$db=null;
//sql文件
public$sqlFile="";
//sql語句集
public$sqlArr=array();
publicfunction__construct($host,$user,$pw,$db_name){
$host=empty($host)?C("DB_HOST"):$host;
$user=empty($user)?C("DB_USER"):$user;
$pw=empty($pw)?C("DB_PWD"):$pw;
$db_name=empty($db_name)?C("DB_NAME"):$db_name;
//連接資料庫
$this->connect=mysql_connect($host,$user,$pw)ordie("Couldnotconnect:".mysql_error());
$this->db=mysql_select_db($db_name,$this->connect)ordie("Yoncannotselectthetable:".mysql_error());
}
//導入sql文件
publicfunctionImport($url){
$this->sqlFile=file_get_contents($url);
if(!$this->sqlFile){
exit("打開文件錯誤");
}else{
$this->GetSqlArr();
if($this->Runsql()){
returntrue;
}
}
}
//獲取sql語句數組
publicfunctionGetSqlArr(){
//去除注釋
$str=$this->sqlFile;
$str=preg_replace('/--.*/i','',$str);
$str=preg_replace('//*.**/(;)?/i','',$str);
//去除空格創建數組
$str=explode("; ",$str);
foreach($stras$v){
$v=trim($v);
if(empty($v)){
continue;
}else{
$this->sqlArr[]=$v;
}
}
}
//執行sql文件
publicfunctionRunSql(){
foreach($this->sqlArras$k=>$v){
if(!mysql_query($v)){
exit("sql語句錯誤:第".$k."行".mysql_error());
}
}
returntrue;
}
}
//範例:
header("Content-type:text/html;charset=utf-8");
$sql=newReadSql("localhost","root","","log_db");
$rst=$sql->Import("./log_db.sql");
if($rst){
echo"Success!";
}
?>
C. 怎麼使用php代碼建立mysql資料庫
$link = mysql_pconnect("localhost","root","");
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "成功";
} else {
echo "失敗" . mysql_error() . "\n";
}
注:不提倡使用函數mysql_create_db()。最好用mysql_query()來提交一條SQLCREATEDATABASE語句來替代。
D. php表單寫入mysql資料庫的代碼
<!--表單文件,拷入index.php-->
<!DOCTYPEhtml>
<html>
<head>
<style>
label{display:inline-block;width:100px;margin-bottom:10px;}
</style>
<title>Addstudents</title>
</head>
<body>
<!--資料庫用mysqli面向過程調用方法-->
<formmethod="post"action="write2db.php">
<!--資料庫用mysqli面向過程調用方法
<formmethod="post"action="write2db_sqlio.php">
-->
<!--資料庫用PDO調用方法
<formmethod="post"action="write2db_pdo.php">
-->
<label>FirstName</label>
<inputtype="text"name="first_name"/>
<br/>
<label>LastName</label>
<inputtype="text"name="last_name"/>
<br/>
<label>department</label>
<inputtype="text"name="department"/>
<br/>
<label>Email</label>
<inputtype="text"name="email"/>
<br/>
<inputtype="submit"value="Addstudents">
</form>
</body>
</html>
------------------------------
<?php
//拷貝命名為write2db.php,資料庫用mysqli面向過程調用方法
//print_r($_POST);
//createavariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';
$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//yourdatabasename
$dbname="test";
$tablename="student";//Createconnection
$connect=mysqli_connect($servername,$username,$password,$dbname);
if(!$connect){
die("Connectionfailed:".mysqli_connect_error());
}
//Executethequery
$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if(mysqli_query($connect,$sql)){
echo"Hooray!.Pleasecheckdatabase.";
}else{
echo"Error:".$sql."<br/>".mysqli_error($connect);
}
mysqli_close($connect);
?>
<?php
//拷貝命名為write2db_sqlio.php,資料庫用mysqli面向對象調用方法
//print_r($_POST);
//createavariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';
$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//databasename
$dbname="test";
$tablename="student";//Createconnection
$conn=newmysqli($servername,$username,$password,$dbname);
//Checkconnection
if($conn->connect_error){
die("Connectionfailed:".$conn->connect_error);
}
$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if($conn->query($sql)===TRUE){
echo"Newrecordcreatedsuccessfully";
}else{
echo"Error:".$sql."<br>".$conn->error;
}
$conn->close();
?>
<?php
//拷貝為文件write2db_pdo.php,資料庫用PDO調用方法
//print_r($_POST);
avariable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//調試用
echo"Yourinput:";
echo$first_name;
echo'<br/>';
echo$last_name;
echo'<br/>';
echo$department;
echo'<br/>';
echo$email;
echo'<br/>';
$servername="localhost";
//
//$username="username";
//$password="password";
$username="tester";
$password="testerPassword";
//yourdatabasename
$dbname="test";
$tablename="student";//Createconnection
try{
$conn=newPDO("mysql:host=$servername;dbname=$dbname",$username,$password);
//setthePDOerrormodetoexception
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql="INSERTINTO$tablename(first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
//useexec()
$conn->exec($sql);
echo"Newrecordcreatedsuccessfully";
}
catch(PDOException$e)
{
echo$sql."<br>".$e->getMessage();
}
$conn=null;
?>
--創建資料庫test,將此文件存為test.sql導入資料庫,或者手動創建表結構
--phpMyAdminSQLDump
--version4.7.4
--https://www.phpmyadmin.net/
--
--Host:127.0.0.1:3306
--GenerationTime:Mar12,2018at04:04AM
--Serverversion:5.7.19
--PHPVersion:7.1.9
SETSQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SETAUTOCOMMIT=0;
STARTTRANSACTION;
SETtime_zone="+00:00";
/*!40101SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101SET@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101SETNAMESutf8mb4*/;
--
--Database:`test`
--
----------------------------------------------------------
--
--Tablestructurefortable`student`
--
DROPTABLEIFEXISTS`student`;
CREATETABLEIFNOTEXISTS`student`(
`id`tinyint(3)UNSIGNEDNOTNULLAUTO_INCREMENT,
`first_name`varchar(20)NOTNULL,
`last_name`varchar(20)NOTNULL,
`department`varchar(50)NOTNULL,
`email`varchar(50)NOTNULL,
PRIMARYKEY(`id`)
)ENGINE=MyISAMAUTO_INCREMENT=2DEFAULTCHARSET=utf8;
--
--Dumpingdatafortable`student`
--
INSERTINTO`student`(`id`,`first_name`,`last_name`,`department`,`email`)VALUES
(1,'first1','last1','cs','[email protected]');
COMMIT;
/*!40101SETCHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101SETCHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS*/;
/*!40101SETCOLLATION_CONNECTION=@OLD_COLLATION_CONNECTION*/;
E. phpstudy怎麼創建資料庫
phpstudy資料庫創建步驟:
1、點擊打開phpstudy軟體,然後點擊mySQL管理器;
F. php 創建資料庫問題
if($database=="")
{
$query="use members";
if(mysql_query($query)==null)
{
$query="create database members";
if(mysql_query($query)==1)
{
//創建資料庫成功,開始連接資料庫
$database="members";
$conn=mysql_connect($server,$username,$password)
or die("could not connect mysql");//你連接資料庫的這個代碼應該放在if外面,這樣才能連接要不然你自己看看吧,在你執行語句的時候,你都還沒有走到mysql_connect這里,所以就沒有連接啊
mysql_select_db($database,$conn)
or die("could not open database");
}
else
{
echo "Error while creating database (Error".mysql_errno().":\"".mysql_error()."\")<br>";//創建資料庫出錯
}
}
G. PHP中怎麼使用SQLite資料庫,最好可以把創建和連接資料庫的代碼發出來。謝謝!急!急!急!
首先說基本配置:
PHP SQLite 的使用和配置方法:
在PHP 5.1.x 以後自帶了 SQLtie 資料庫功能,只需要在配置PHP.ini中開啟即可
;extension=php_sqlite.dll
在PHP 5.2.x 以後自帶了 SQLtie PDO資料庫功能,只需要在配置PHP.ini中開啟即可
;extension=php_pdo_sqlite.dll
SQLite 資料庫管理:
1、SQLiteManager與PHPmyadmin不同,需要添加管理的資料庫
2、Windows下使用添加路徑需要將 X: \**\** 改為 X:/**/**
3、 創建資料庫的時候需要指定SQLite 資料庫文件存放的路徑
再說操作:
<?php
$db_path = 'sqlite3_db_php';
$db = new SQLite3($db_path); //這就是創建資料庫,也是連接資料庫
if (!!$db) {
//下面創建一個表格
$db->exec('CREATE TABLE user (id integer primary key, name varchar(32), psw varchar(32))');
H. PHP代碼創建Mysql資料庫
先mysql_connect()到mysql再做mysql_query()的相關操作。
====================================================================
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "myabc";
$ranks = array(
1=>"newbie",
2=>"new member",
3=>"member",
4=>"high member",
5=>"very high member",
6=>"supreme member",
7=>"ultra member",
8=>"godlike member",
9=>"god member",
10=>"low god",
11=>"medium god",
12=>"high god",
13=>"very high god",
14=>"supreme god",
15=>"ultra god",
16=>"perfect"
);
$couldNotOpenDatabase = "Could not open database<BR>\n please check your settings in config.php";
$couldNotConnectMysql="Could not connect Mysql!";
$conn=mysql_connect($server,$username,$password) or die ($couldNotConnectMysql);
if (mysql_select_db($database,$conn))
{//資料庫存在,做相應操作
}
else
{//資料庫不存在,創建一個,並做相應操作
$query = "CREATE DATABASE $database";
$result = mysql_query($query);
mysql_select_db($database,$conn)or die ($couldNotOpenDatabase);
}
?>
I. 如何用php創建mysql資料庫
使用EclipsePHP Studio 3 創建一個PHP工程名稱為test1,在工程名下面userinfo的文件夾,然後在文件夾創建一個PHP文件(userinfo_create.php):
2
打開我們創建PHP文件:
先設置 地址,賬號,密碼:
$url = "127.0.0.1";//連接資料庫的地址
$user = "root"; //賬號
$password = "root";//密碼
//獲取連接$con = mysql_connect($url,$user,$password);
if(!$con){
die("連接失敗".mysql_error());
}
3
設置具體連接的數據,那我們這兒連接test資料庫,我們通過Navicat 打開mysql 資料庫
mysql_select_db("test");
J. 如何在php創建資料庫與數據表
創建資料庫:create database 資料庫名
創建數據表:
CREATE TABLE `users` (
`id` tinyint(10) auto_increment primary key NOT NULL,
`username` varchar(30) NOT NULL,
`age` int(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
執行這兩個sql語句就行