?php

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、做網(wǎng)站與策劃設(shè)計(jì),宿遷網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:宿遷等地區(qū)。宿遷做網(wǎng)站價(jià)格咨詢:18982081108
/*
查詢數(shù)據(jù)庫是否存在功能
$sql:查詢數(shù)據(jù)庫的SQL語句
$find_table:需要檢查的表名
*/
mysql_connect('localhost','root','2260375')?or?die('can\'t?not?connect?database');
if((int)check_table_is_exist('show?databases;','test')==1)
{
echo?'該表存在';
}
else?
{
echo?'該表不存在';
}
function?check_table_is_exist($sql,$find_table)
{
$row=mysql_query($sql);
$database=array();
$finddatabase=$find_table;
while?($result=mysql_fetch_array($row,MYSQL_ASSOC))
{
$database[]=$result['Database'];
}
unset($result,$row);
mysql_close();
/*開始判斷表是否存在*/
if(in_array($find_table,$database))
{
return?true;
}
else?
{
return?false;
}
}
?
材料/工具:電腦、PHP
1、首先,創(chuàng)建一個(gè)方法function來供調(diào)用。
2、先判斷id是否為0,為0則是不存在這條數(shù)據(jù)的。(假設(shè)判斷數(shù)據(jù)庫的數(shù)據(jù)是否存在相同id的數(shù)據(jù))
3、然后寫sql語句,能查詢對應(yīng)id的數(shù)據(jù)是否存在了。
4、然后調(diào)用查詢數(shù)據(jù)庫的方法,判斷返回來的結(jié)果是否為空。不為空則是數(shù)據(jù)已存在。
5、如果是判斷數(shù)據(jù)是否存在的關(guān)鍵字段是字符串的,我們稍為改一下代碼就行了,比如這里的,判斷email是否已經(jīng)存在于數(shù)據(jù)庫中。
6、我們這里的queryFirstColumn方法是封裝好的數(shù)據(jù)庫函數(shù)。
7、也可以將其改成一般的mysql_query的系統(tǒng)內(nèi)置方法來查詢的,代碼如圖。
?php
$data = array();
$db_name_php = 'books';
if (!mysql_connect('localhost', 'root', '123456')) {
echo '不能連接到mysql';
exit;
}
$result = mysql_query('show databases;');
While($row = mysql_fetch_assoc($result)){
$data[] = $row['Database'];
}
unset($result, $row);
mysql_close();
print_r($data);
echo 'brbr'; if (in_array(strtolower($db_name_php), $data))
echo '[',$db_name_php,']數(shù)據(jù)庫存在';
else
echo '[',$db_name_php,']數(shù)據(jù)庫不存在';
?
標(biāo)題名稱:php查看數(shù)據(jù)表是否存在 php查看數(shù)據(jù)類型
文章位置:http://www.yijiale78.com/article10/ddjgjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站營銷、定制網(wǎng)站、網(wǎng)站導(dǎo)航、響應(yīng)式網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)