php 資料庫 使用php訪問mysql

2022-05-06 17:15:09 字數 1930 閱讀 4954

連線

code

php//

下面使用了函式庫mysqli,所以需要將php.ini檔案中的extension=php.mysqli 開啟

//這裡採用了物件導向的語法,當然,mysqli也支援面向過程的語法

@ $db

=new

mysqli(

'localhost',

'root',

'tograce',

'books');

//以上**例項化了mysqli類並且建立了到主機'localhost'的連線,該連線使用的使用者名為和密碼分別是:root,tograce,同時連線上books資料庫if(

mysqli_connect_errno

())else

$query="

select * from books";

$result

=$db

->

query(

$query

);$num_results

=$result

->

num_rows;

echo

'number of books found: '.

$num_results.'

';$result

->

free();

$db->

close();

?>

插入資料

摘自code

<

html

>

<

head

>

<

title

>

book-o

-rama book entry results

title

>

head

>

<

body

>

<

h1>

book-o

-rama book entry results

h1>

php//

create short variable names

$isbn

=$_post['

isbn'];

$author

=$_post['

author'];

$title

=$_post['

title'];

$price

=$_post['

price'];

if(!$isbn||!

$author||!

$title||!

$price)if

(!get_magic_quotes_gpc

())@ 

$db=

newmysqli(

'localhost',

'root',

'tograce',

'books');

if(mysqli_connect_errno

())$query="

insert into books values('"

.$isbn."

', '".

$author."

', '".

$title."

', '".

$price."

')";$result

=$db

->

query(

$query

);if

($result

)echo

$db->

affected_rows.'

book inserted into database.';

$db->

close();

?>

body

>

html

>

PHP訪問資料庫

在php中,無論訪問哪種資料庫,都需要使用sql。php最適合與mysql資料庫配合使用,主要原因是php提供了很多操作mysql資料庫的內建函式,可方便地實現訪問和操作mysql資料庫的各種需要。php訪問mysql資料庫的具體步驟 1 建立與mysql伺服器的連線 2 設定字符集 3 選擇要操作...

php訪問資料庫

1.建立資料庫 lyb create database lyb create table lyb1 id int not null auto increment title varchar 200 not null content text not null author varchar 30 no...

PHP 資料庫訪問

一 過時方法 設定頁面編碼格式 header content type text html charset utf 8 1 生成鏈結 db connect mysql connect localhost root 517 localhost 伺服器的位址 root 資料庫的使用者名稱 517 資料庫...