重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
先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 databaseBR\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);
}
?
PHP连接数据库之PHP连接MYSQL数据库代码
?php $mysql_server_name= localhost ; //改成自己的mysql数据库服务器 $mysql_username= root ; //改成自己的mysql数据库用户名 $mysql_password= ; //改成自己的mysql数据库密码 $mysql_database= mycounter ; //改成自己的mysql数据库名 $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( ) UNSIGNED NOT NULL AUTO_INCREMENT `count` INT( ) UNSIGNED NOT NULL DEFAULT PRIMARY KEY ( `id` ) ) TYPE = innodb; ; mysql_select_db($mysql_database $conn); $result=mysql_query($sql); //echo $sql; mysql_close($conn); echo "Hello!数据库mycounter已经成功建立!"; ?
PHP连接数据库之PHP连接ACCESS数据库代码方法
? $conn = new ("ADODB Connection"); $connstr = "DRIVER={Microsoft Access Driver (* mdb)}; DBQ=" realpath("data/db mdb"); $conn Open($connstr); $rs = new ("ADODB RecordSet"); $rs Open("select * from szd_t" $conn ); while(! $rs eof) { $f = $rs Fields( ); echo $f value; $rs MoveNext(); } ?
PHP连接数据库之PHP连接MS SQL数据库代码方法
安装SQL服务器并添加PHP的MSSQL扩展
使用以下代码连接并测试
?php $myServer = localhost; //主机 $myUser = sa; //用户名 $myPass = password; //密码 $myDB = Northwind; //MSSQL库名 $s = @mssql_connect($myServer $myUser $myPass) or die(Couldnt connect to SQL Server on $myServer); $d = @mssql_select_db($myDB $s) or die(Couldnt open database $myDB); $query = SELECT TitleOfCourtesy+ +FirstName+ +LastName AS Employee ; $query = FROM Employees ; $query = WHERECountry=USA AND Left(HomePhone ) = ( ); $result = mssql_query($query); $numRows = mssql_num_rows($result); echo h $numRows Row ($numRows == ? : s) Returned / h ; while($row = mssql_fetch_array($result)) { echo li $row[Employee] /li; } ?
PHP连接数据库之PHP连接Oracle数据库
PHP提供了两套函数与Oracle连接 分别是ORA_和OCI函数 其中ORA_函数略显陈旧 OCI函数更新据说更好一些 两者的使用语法几乎相差无几 你的PHP安装选项应该可以支持两者的使用
? if ($conn=Ora_Logon("user@TNSNAME" "password")) { echo "SUCCESS ! Connected to databasen"; }else {echo "Failed : ( Could not connect to databasen";} Ora_Logoff($conn); phpinfo(); ? lishixinzhi/Article/program/PHP/201405/30761
你做好程序以后,把数据库导出成sql文件
1、连接数据库
2、读取这个sql文件里的sql语句,并执行
3、生成一个数据库连接参数的php文件
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?
?php
class ReadSql {
//数据库连接
protected $connect = null;
//数据库对象
protected $db = null;
//sql文件
public $sqlFile = "";
//sql语句集
public $sqlArr = array();
public function __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) or die("Could not connect: " . mysql_error());
$this-db = mysql_select_db($db_name, $this-connect) or die("Yon can not select the table:" . mysql_error());
}
//导入sql文件
public function Import($url) {
$this-sqlFile = file_get_contents($url);
if (!$this-sqlFile) {
exit("打开文件错误");
} else {
$this-GetSqlArr();
if ($this-Runsql()) {
return true;
}
}
}
//获取sql语句数组
public function GetSqlArr() {
//去除注释
$str = $this-sqlFile;
$str = preg_replace('/--.*/i', '', $str);
$str = preg_replace('/\/\*.*\*\/(\;)?/i', '', $str);
//去除空格 创建数组
$str = explode(";\n", $str);
foreach ($str as $v) {
$v = trim($v);
if (empty($v)) {
continue;
} else {
$this-sqlArr[] = $v;
}
}
}
//执行sql文件
public function RunSql() {
foreach ($this-sqlArr as $k = $v) {
if (!mysql_query($v)) {
exit("sql语句错误:第" . $k . "行" . mysql_error());
}
}
return true;
}
}
//范例:
header("Content-type:text/html;charset=utf-8");
$sql = new ReadSql("localhost", "root", "", "log_db");
$rst = $sql-Import("./log_db.sql");
if ($rst) {
echo "Success!";
}
?