新聞中心
php調(diào)用數(shù)據(jù)庫字段
我說一下幾個步驟:

創(chuàng)新互聯(lián)公司專業(yè)成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),集網(wǎng)站策劃、網(wǎng)站設(shè)計、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文平臺等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計,讓網(wǎng)站在運行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。
1、首先你得有一個存儲這些數(shù)據(jù)的數(shù)據(jù)庫表,比如數(shù)據(jù)庫表的結(jié)構(gòu)是這樣的。
數(shù)據(jù)庫表名為:user
字段:編號(id),姓名(name),手機(mobile),產(chǎn)品名稱(productName) 主鍵為id
2、實現(xiàn)你需要的功能:
第一步:你需要連接數(shù)據(jù)庫,有一個連接數(shù)據(jù)庫的文件:conn.php。內(nèi)容如下:
// 我假設(shè)你的數(shù)據(jù)庫是mysql的,假設(shè)你的數(shù)據(jù)庫用戶名為root,密碼為123456,根據(jù)你數(shù)據(jù)庫的實際情況改寫成你的。數(shù)據(jù)庫名稱假設(shè)為db_889888658
?php
$conn=mysql_connect("localhost","root","123456") or die("數(shù)據(jù)庫連接失敗,請檢查用戶名或密碼");
mysql_select_db("db_889888658",$conn);
mysql_query("SET NAMES 'gb2312'");
?
第二步:你需要一個添加數(shù)據(jù)的表單,就相當于一個注冊或添加數(shù)據(jù)的頁面。如文件為:add.html內(nèi)容如下:
form action="reg.php" method="post"
input type="text" name="name"br/
input type="text" name="mobile"br/
input type="text" name="productName"/br
input type="submit" name="submit" value="添加數(shù)據(jù)"
/form
第三步:寫一個處理你表單提交的數(shù)據(jù)的文件reg.php。內(nèi)容如下:
?php
include "conn.php";
if(isset($_POST["submit"])){
$name=$_POST["name"];
$mobile=$_POST["mobile"];
$productName=$_POST["productName"];
$sql="INSERT INTO 'user'(id,name,mobile,productName) VALUES (NULL,$name,$mobile,$productName)";
$query=mysql_query($sql);
$num=mysql_affected_rows($conn);
if($num=1){
echo "scriptalert('數(shù)據(jù)添加成功');location.href='add.html';/script";
}else{
echo "scriptalert('數(shù)據(jù)添加失敗');history.back();/script";
}
}
?
第四步,第三步已經(jīng)實現(xiàn)你說的第一個功能。下面說一下你的第二個功能。寫一個表單,輸入你要查詢的手機號,點擊“查詢”按鈕查詢你想要的字段。
?php
if($_POST["submit"]){
$mobile=$_POST["mobile"];
if(!empty($mobile)){
include "conn.php";
$sql="SELECT * FROM 'user' WHERE 'mobile'='$mobile'";
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
$str="查詢結(jié)果:br/";
$str.="用戶名:".$rs["name"]."?";
$str.="產(chǎn)品名:".$rs["name"]."?";
}
echo "您查詢的手機號為".$mobile."的數(shù)據(jù)信息如下:br/";
echo $str;
}else{
echo "請輸入手機號";
}
}
?
form action="" method="post"
請輸入您要查詢的手機號:input type="text" name="mobile" input type="submit" name="submit" value="查詢"
/form
php中獲取數(shù)據(jù)庫中字段值。
取到值后,在輸出的時候%=變量*100%/100,這樣用PHP把數(shù)據(jù)輸入,后面跟/100的字符就可以了。
PHP如何取到mysql數(shù)據(jù)庫中某個字段的值
查詢成功了,但是你沒有把查詢的值賦給變量,所以你肯定得不到值,
$sql
=
"SELECT
meta_value
FROM
wp_postmeta
WHERE
meta_key
=
'img-link'
and
post_id
=1";
$result=mysql_query($sql);
while
($row
=
mysqli_fetch_assoc($result))//用myql_fetch_assoc函數(shù)取值,可以確保多行數(shù)據(jù)時能循環(huán)輸出,mysql_fetch_array只能取得最前面的一行數(shù)據(jù)
{
echo
$row['meta_value'];//輸出你想要的字段值
}
如何在php中獲取數(shù)據(jù)庫中字段值?
?php
$sql = "SELECT name FROM user WHERE ID=1 LIMIT 0,1";
$result = mysql_query($sql);
$rs = mysql_result($result,0);
?
網(wǎng)頁標題:php調(diào)用數(shù)據(jù)庫的字段值 php調(diào)用mysql查詢函數(shù)
當前鏈接:http://www.dlmjj.cn/article/ddoesgp.html


咨詢
建站咨詢
