《PHP教程:PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫》要點(diǎn):
本文介紹了PHP教程:PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
環(huán)境PHP實(shí)戰(zhàn)
開發(fā)包:appserv-win32-2.5.10PHP實(shí)戰(zhàn)
服務(wù)器:Apache2.2PHP實(shí)戰(zhàn)
數(shù)據(jù)庫:phpMyAdminPHP實(shí)戰(zhàn)
語言:php5,javaPHP實(shí)戰(zhàn)
平臺(tái):windows 10PHP實(shí)戰(zhàn)
java驅(qū)動(dòng):mysql-connector-java-5.1.37PHP實(shí)戰(zhàn)
需求PHP實(shí)戰(zhàn)
編寫一個(gè)PHP腳本語言,連接到phpMyAdmin數(shù)據(jù)庫的test庫PHP實(shí)戰(zhàn)
編寫一個(gè)java web服務(wù)端,連接到phpMyAdmin數(shù)據(jù)庫的test庫PHP實(shí)戰(zhàn)
代碼PHP實(shí)戰(zhàn)
php連接方式PHP實(shí)戰(zhàn)
mysql.phpPHP實(shí)戰(zhàn)
<?php /***************************** *數(shù)據(jù)庫連接 *****************************/ $conn = @mysql_connect("localhost","root","123"); if (!$conn){ die("連接數(shù)據(jù)庫失敗:" . mysql_error()); } mysql_select_db("test", $conn); //字符轉(zhuǎn)換,讀庫 mysql_query("set character set utf8"); mysql_query("set names utf8"); ?>
test.php測(cè)試PHP實(shí)戰(zhàn)
<?php error_reporting(0); //防止報(bào)錯(cuò) include('mysql.php'); $result=mysql_query("select * from user"); //根據(jù)前面的計(jì)算出開始的記錄和記錄數(shù) // 循環(huán)取出記錄 $six; while($row=mysql_fetch_row($result)) { echo $row[0]; echo $row[1]; } ?>
?運(yùn)行截圖 :PHP實(shí)戰(zhàn)
PHP實(shí)戰(zhàn)
java 連接方式PHP實(shí)戰(zhàn)
1.新建一個(gè)java project為mysqlTestPHP實(shí)戰(zhàn)
2.加載JDBC驅(qū)動(dòng),mysql-connector-java-5.1.37PHP實(shí)戰(zhàn)
PHP實(shí)戰(zhàn)
MySQLConnection.javaPHP實(shí)戰(zhàn)
package com.mysqltest; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /* * **Mysql連接** * * 參數(shù): * conn 連接 * url mysql數(shù)據(jù)庫連接地址 * user 數(shù)據(jù)庫登陸賬號(hào) * password 數(shù)據(jù)庫登陸暗碼 * 方法: * conn 獲取連接 */ public class MySQLConnection { public static Connection conn = null; public static String driver = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://127.0.0.1:3306/post"; public static String user = "root"; public static String password = "123"; /* * 創(chuàng)建Mysql數(shù)據(jù)連接 第一步:加載驅(qū)動(dòng) Class.forName(Driver) 第二步:創(chuàng)建連接 * DriverManager.getConnection(url, user, password); */ public Connection conn() { try { Class.forName(driver); } catch (ClassNotFoundException e) { System.out.println("驅(qū)動(dòng)加載錯(cuò)誤"); e.printStackTrace(); } try { conn = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("數(shù)據(jù)庫鏈接錯(cuò)誤"); e.printStackTrace(); } return conn; } }
Work.javaPHP實(shí)戰(zhàn)
package com.mysqltest; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /* * mysql增刪改查 */ public class Work { /* * insert 增加 */ public static int insert() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 獲取連接 PreparedStatement pst; // 執(zhí)行Sql語句 int i = 0; String sql = "insert into user (username,password) values(?,?)"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "lizi"); pst.setString(2, "123"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("數(shù)據(jù)寫入失敗"); e.printStackTrace(); } return i; } /* * select 寫入 */ public static void select() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 獲取連接 PreparedStatement pst; // 執(zhí)行Sql語句(Statement) ResultSet rs; // 獲取返回結(jié)果 String sql = "select * from user"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); rs = pst.executeQuery(sql);// 執(zhí)行sql語句 System.out.println("---------------------------------------"); System.out.println("名字 | 暗碼"); while (rs.next()) { System.out.println(rs.getString("username") + " | " + rs.getString("password")); } System.out.println("---------------------------------------"); conns.close(); pst.close(); rs.close(); } catch (SQLException e) { System.out.println("數(shù)據(jù)查詢失敗"); e.printStackTrace(); } } /* * update 修改 */ public static int update() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 獲取連接 PreparedStatement pst; // 執(zhí)行Sql語句(Statement) int i = 0; String sql = "update user set password = ? where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "123"); pst.setString(2, "lizi"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("數(shù)據(jù)修改失敗"); e.printStackTrace(); } return i; } /* * delete 刪除 */ public static int delete() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 獲取連接 PreparedStatement pst; // 執(zhí)行Sql語句(Statement) int i = 0; String sql = "delete from user where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement(sql); pst.setString(1, "lizi"); i = pst.executeUpdate(); pst.close(); conns.close(); } catch (SQLException e) { System.out.println("數(shù)據(jù)刪除失敗"); e.printStackTrace(); } return i; } /* * test */ public static void main(String[] args) { // System.out.println(insert()); select(); // System.out.println(update()); // System.out.println(delete()); } }
?test截圖PHP實(shí)戰(zhàn)
PHP實(shí)戰(zhàn)
ps:php操作MySQL數(shù)據(jù)庫中語句PHP實(shí)戰(zhàn)
我們常常用conn.php文件來建立與數(shù)據(jù)庫的鏈接,然后在所需的文件中利用include 進(jìn)行調(diào)用.這樣有效防止對(duì)數(shù)據(jù)庫屬性的改動(dòng) 而引起其他有關(guān)文件對(duì)數(shù)據(jù)調(diào)用的差錯(cuò).PHP實(shí)戰(zhàn)
? 現(xiàn)在來看一個(gè)conn.php文件,代碼如下:PHP實(shí)戰(zhàn)
<?php $conn=@mysql_connect("localhost","root","")or die("數(shù)據(jù)庫連接錯(cuò)誤");//鏈接數(shù)據(jù)庫服務(wù)器 mysql_select_db("messageboard",$conn);//選擇數(shù)據(jù)庫名為messageboard mysql_query("set names 'utf'");//使用utf編碼,這里不克不及寫成utf-否則將顯示亂碼,但UTF不區(qū)分大小寫 ?>
學(xué)習(xí)積累,收集了PHP操作MYSQL的幾個(gè)基礎(chǔ)函數(shù):PHP實(shí)戰(zhàn)
.使用mysql_connect()函數(shù)連接MySQL服務(wù)器:mysql_connect("hostname", "username","password");
如,$link = mysql_connect("localhost", "root", "") or die("不能連接到數(shù)據(jù)庫服務(wù)器!可能是數(shù)據(jù)庫服務(wù)器沒有啟動(dòng),或者用戶名暗碼有誤!".mysql_error());
PHP實(shí)戰(zhàn)
.使用mysql_select_db()函數(shù)選擇數(shù)據(jù)庫文件:mysql_query("use 數(shù)據(jù)庫名",$link);
PHP實(shí)戰(zhàn)
如,$db_selected=mysql_query("use example",$link);
PHP實(shí)戰(zhàn)
.使用mysql_query()函數(shù)執(zhí)行SQL語句:mysql_query(string query(SQL語句),$link);
PHP實(shí)戰(zhàn)
如:
PHP實(shí)戰(zhàn)
添加會(huì)員:$result=mysql_query("insert into tb_member values('a','')",$link);
PHP實(shí)戰(zhàn)
修改會(huì)員:$result=mysql_query("update tb_member setuser='b',pwd=''where user='a'",$link);
PHP實(shí)戰(zhàn)
刪除會(huì)員:$result=mysql_query("delecte from tb_member where user='b'",$link);
PHP實(shí)戰(zhàn)
查詢會(huì)員:$sql=mysql_query("select * from tb_book");
PHP實(shí)戰(zhàn)
模糊查詢:$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'");
PHP實(shí)戰(zhàn)
//通用符%表現(xiàn)零個(gè)或任意多個(gè)字符.
PHP實(shí)戰(zhàn)
顯示表布局:$result=mysql_query("DESC tb_member");
PHP實(shí)戰(zhàn)
.使用mysql_fetch_array()函數(shù)從數(shù)組成果集中獲得信息:
PHP實(shí)戰(zhàn)
語法布局:array mysql_fetch_array(resource result[,int result_type])
PHP實(shí)戰(zhàn)
參數(shù)result資源類型的參數(shù),整形型參數(shù),要傳入的是由mysql_fetch_array()函數(shù)返回的數(shù)據(jù)指針;
PHP實(shí)戰(zhàn)
參數(shù)result_type:可選項(xiàng),php操作MySQL數(shù)據(jù)庫語句基礎(chǔ)整數(shù)型參數(shù),要傳入的是MYSQL_ASSOC(關(guān)聯(lián)索引)、MYSQL_NUM(數(shù)字索引) MYSQL_BOTH(包含前兩者,默認(rèn)值)
PHP實(shí)戰(zhàn)
如:PHP實(shí)戰(zhàn)
<>$sql=mysql_query("select * from tb_book"); $info=mysql_fetch_object($sql); <>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'"); $info=mysql_fetch_object($sql);
.使用mysql_fetch_object()函數(shù)從成果集中獲取一行作為對(duì)象:
PHP實(shí)戰(zhàn)
語法布局:object mysql_fetch_object(resource result);
PHP實(shí)戰(zhàn)
如:PHP實(shí)戰(zhàn)
<>$sql=mysql_query("select * from tb_book"); $info=mysql_fetch_object($sql); <>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'"); $info=mysql_fetch_object($sql);
mysql_fetch_object()函數(shù)與mysql_fetch_array()函數(shù)類似,只有一點(diǎn)區(qū)別,即返回一個(gè)對(duì)象而不是數(shù)組,該函數(shù)只能通過字段名來拜訪數(shù)組.拜訪結(jié)果集中行的元素的語法結(jié)構(gòu):$row->col_name(列名)
PHP實(shí)戰(zhàn)
.使用mysql_fetch_row()函數(shù)逐行獲得成果集中的每條記錄:
PHP實(shí)戰(zhàn)
語法布局:array mysql_fetch_row(resource result)
PHP實(shí)戰(zhàn)
如:PHP實(shí)戰(zhàn)
<>$sql=mysql_query("select * from tb_book"); $row=mysql_fetch_row($sql); <>$sql=mysql_query("select * from tb_book where bookname like '%".trim($txt_book)."%'"); $row=mysql_fetch_row($sql);
.使用mysql_num_rows()函數(shù)獲取成果集中地記錄數(shù):
PHP實(shí)戰(zhàn)
語法布局:int mysql_num_rows(resource result)
PHP實(shí)戰(zhàn)
如:PHP實(shí)戰(zhàn)
$sql=mysql_query("select * from tb_book"); ...... <?php $nums=mysql_num_rows($sql);echo $nums;?>
注:若要獲得insert、update、delete語句的所影響到的數(shù)據(jù),則必需使用mysql_affected_rows()函數(shù)來實(shí)現(xiàn).
PHP實(shí)戰(zhàn)
.mysql_query("set names gb");//設(shè)置MySQL的編碼格式為 gb類型,以屏蔽亂碼.
PHP實(shí)戰(zhàn)
.封閉記錄集:mysql_free_result($sql);
PHP實(shí)戰(zhàn)
.封閉MySQL數(shù)據(jù)庫服務(wù)器:mysql_close($conn);PHP實(shí)戰(zhàn)
歡迎參與《PHP教程:PHP隨手筆記整理之PHP腳本和JAVA連接mysql數(shù)據(jù)庫》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/8352.html