① php怎麼獲得mysql資料庫的數據
首先你要看php.ini有沒有開啟mysql的拓展函數庫,然後mysql_connect()連接資料庫,mysql_query("set names utf8");設置編碼格式,然後mysql_select_db()設置查詢的資料庫
mysql_query()執行sql語句,mysql_fetch_array()或者mysql_fetch_assoc()或者mysql_fetch_num()獲取結果集,mysql_close()最後關閉資料庫連接,明白了么
② php如何獲取資料庫信息
代碼如下:?View
Code
PHP
include("conn.php");//調用資料庫連接文件
echo
"<table
width=572
height=56
border=0
cellspacing=1>
";
//創建html表格
echo
"<tr
bgcolor=#9999FF>";
echo
"<th
width=33
scope=col>id</th>";
echo
"<th
width=100
scope=col>user_name</th>
";
echo
"<th
width=100
scope=col>user_pass</th>
";
echo
"<th
width=100
scope=col>staus</th>";
echo
"<th
width=100
scope=col>insert_time</th>";
echo
"</tr>";
$SQL
=
"select
*
from
user_info";
$query
=
mysql_query($SQL);
//SQL查詢語句
while
($row
=
mysql_fetch_array($query)){
//使用while循環mysql_fetch_array()並將數據返回數組
echo
"<tr
onmouseout=this.style.backgroundColor=''
onMouseOver=this.style.backgroundColor='#99CC33'
bgcolor=#CCCCCC>";
echo
"<td>$row[0]</td>";
//輸出數組中數據
echo
"<td>$row[1]</td>";
echo
"<td>$row[2]</td>";
echo
"<td>$row[3]</td>";
echo
"<td>$row[4]</td>";
echo
"</tr>";
}
echo
"</table>";輸出記錄截圖
③ php從資料庫中讀取特定的行(實例)
有的時候我們需要從資料庫中讀取特定的數據,來檢驗用戶的輸入,這個時候需要執行的sql語句是:
select
*
from
table_name
where
id='num';
需要執行這樣的一個語句。
那麼怎樣來執行這樣的語句。
為了執行sql語句,我們需要和資料庫相連接
<?PHP
$servername='localhost';
$username="root";
$password="1234";
$dbname="mydb1";
//創建鏈接
$conn=new
mysqli($servername,$username,$password,$dbname);
if($conn->connect_error)
{
die("鏈接失敗".$connect_error);
}
//定義語句
$sql="select
*
from
users
where
username='wangdenggao'";
//執行語句
$result=$conn->query($sql);
if($result->num_rows>0)//這個檢驗是必須的,而且我們要知道的是$result也就是執行sql語句的返回的值是一個數組
這個數組看做一個對象,我們可以用箭頭得到他的一些的屬性,而且fetch_assoc()是獲取數據數組中的每一行
{
while
($row=$result->fetch_assoc())
{
echo
"<br>Username".$row['username']."<br>Password:".$row['password'];
}
}
?>
以上這篇php從資料庫中讀取特定的行(實例)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
④ php獲取MySQL的最新幾行數據
試編寫代碼示例如下:
<?php
//連接資料庫
$db=newmysqli('localhost','test','test','test');
if($db->connect_errno){
printf("連接資料庫失敗:%s ",$db->connect_error);
exit();
}
//對資料庫根據時間欄位按降序排序,並抽取前面的40條記錄
$data=$db->query('select*from數據表名orderby時間欄位名desclimit40');
$rows=$data->fetch_all(MYSQLI_ASSOC);
//隨機生成5條記錄數組
$rand_arr=array_rand($rows,5);
//根據生成的隨機數組,輸出記錄
for($i=0;$i<5;$i++)
{
echo$rows[$rand_arr[$i]]['name'].'|'.$rows[$rand_arr[$i]]['description'].'|'.$rows[$rand_arr[$i]]['update_time'].'<br/>';
}
?>
示例運行截圖:
⑤ php怎樣讀取資料庫的記錄
將$rs=mysql_fetch_object($result)改為
$rs=mysql_fetch_assoc($result)或者$rs=mysql_fetch_array($result)就可以實現。
具體原因:
mysql有四種方法獲取結果的當前行數據,分別是fetch_row(),fetch_assco(),fetch_array(),fetch_object()區別分別是返回的數據的類型不同,依次分別是索引數組(即索引是數字0,1,2等),關聯數組(即索引是具體的真實的鍵等),兼前二者,對象。
⑥ php從資料庫中提取出幾行數據,並把沒一行數據一一輸出
PHP從資料庫提取並顯示數據的典型代碼如下:
<?php
//連接資料庫,三個參數:資料庫伺服器IP,用戶名,密碼
if(mysql_connect('127.0.0.1','root','123456')){
//資料庫連接成功,開始查詢,下面是SQL語句
//此例子SQL查詢資料庫test裡面的表tab,假設有一個數值欄位score,查詢其值大於60的數據行
$sql="selectfromtest.tabwherescore>60";
if($res=mysql_query($sql)){
//資料庫查詢執行成功
while($row=mysql_fetch_array($res)){
//循環查詢滿足條件的所有記錄
echo'姓名:'.$row['name'].'成績:'.$row['score'].'<br>';
}
//刪除查詢
mysql_free_result($res);
}elseecho"執行資料庫查詢失敗,語句:$sql<br>原因:".mysql_error();
//關閉資料庫
mysql_close();
}elseecho"資料庫連接失敗,原因是:".mysql_error()l
⑦ 如何用php獲取資料庫信息並顯示
獲取ppq資料庫的所有表名的代碼:
?php
$server='localhost';
$user='root';
$pass='12345';
$dbname='ppq';
$conn=mysql_connect($server,$user,$pass);
if(!$conn)
die("資料庫系統連接失敗!");
$result=mysql_list_tables($dbname);
if(!$result)
die("資料庫連接失敗!");
while($row=mysql_fetch_row($result))
{
echo
$row[0]."
";
}
mysql_free_result($result);
?
mysql_list_tables
(PHP
3,
PHP
4
,
PHP
5)
mysql_list_tables
--
列出
MySQL
資料庫中的表
說明
resource
mysql_list_tables
(
string
database
[,
resource
link_identifier])
mysql_list_tables()
接受一個資料庫名並返回和
mysql_query()
函數很相似的一個結果指針。用
mysql_fetch_array()或者用mysql_fetch_row()來獲得一個數組,數組的第0列就是數組名,當獲取不到時
mysql_fetch_array()或者用mysql_fetch_row()返回
FALSE。
⑧ 如何正確理解PHP獲取顯示資料庫數據函數
1、PHP獲取顯示資料庫數據函數之 mysql_result()
mixed mysql_result(resource result_set, int row [,mixed field])
從result_set 的指定row 中獲取一個field 的數據. 簡單但是效率低.
舉例:
$link1 = @mysql_connect("server1",
"webuser", "password")
or die("Could not connect
to mysql server!");
@mysql_select_db("company")
or die("Could not select database!");
$query = "select id, name
from proct order by name";
$result = mysql_query($query);
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
mysql_close();
注意,上述代碼只是輸出結果集中的第一條數據的欄位值,如果要輸出所有記錄,需要循環處理.
for ($i = 0; $i <= mysql_num_rows($result); $i++)
{
$id = mysql_result($result, 0, "id");
$name = mysql_result($result, 0, "name");
echo "Proct: $name ($id)";
}
注意,如果查詢欄位名是別名,則mysql_result中就使用別名.
2、PHP獲取顯示資料庫數據函數之mysql_fetch_row()
array mysql_fetch_row(resource result_set)
從result_set中獲取整行,把數據放入數組中.
舉例(注意和list 的巧妙配合):
$query = "select id,
name from proct order by name";
$result = mysql_query($query);
while(list($id, $name)
= mysql_fetch_row($result)) {
echo "Proct: $name ($id)";
}
3、PHP獲取顯示資料庫數據函數之mysql_fetch_array()
array mysql_fetch_array(resource result_set [,int result_type])
mysql_fetch_row()的增強版.
將result_set的每一行獲取為一個關聯數組或/和數值索引數組.
默認獲取兩種數組,result_type可以設置:
MYSQL_ASSOC:返回關聯數組,欄位名=>欄位值
MYSQL_NUM:返回數值索引數組.
MYSQL_BOTH:獲取兩種數組.因此每個欄位可以按索引偏移引用,也可以按欄位名引用.
舉例:
$query = "select id,
name from proct order by name";
$result = mysql_query($query);
while($row = mysql_fetch_array
($result, MYSQL_BOTH)) {
$name = $row['name'];
//或者 $name = $row[1];
$name = $row['id'];
//或者 $name = $row[0];
echo "Proct: $name ($id)";
}
4、PHP獲取顯示資料庫數據函數之mysql_fetch_assoc()
array mysql_fetch_assoc(resource result_set)
相當於 mysql_fetch_array($result, MYSQL_ASSOC)
5、PHP獲取顯示資料庫數據函數之mysql_fetch_object()
object mysql_fetch_object(resource result_set)
和mysql_fetch_array()功能一樣,不過返回的不是數組,而是一個對象.
舉例:
$query = "select id, name
from proct order by name";
$result = mysql_query($query);
while($row = mysql_fetch_object
($result)) {
$name = $row->name;
$name = $row->id;
echo "Proct: $name ($id)";
}
以上這些函數就是PHP獲取顯示資料庫數據函數的全部總結。