新聞中心
php怎樣驗證表單文本區(qū)域內(nèi)容是否已存在數(shù)據(jù)庫中?
方法:查詢數(shù)據(jù),若數(shù)據(jù)存在則輸出(前端這個要自己寫,我只寫了一個echo)
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比土默特左旗網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式土默特左旗網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋土默特左旗地區(qū)。費(fèi)用合理售后完善,10多年實體公司更值得信賴。
代碼如下:
//$conn = new mysqli($sql_server_name, $sql_username, $sql_password, $sql_db);
$timu = $_GET["timu"];
$sql = $conn-query("查詢 * from problems where timu
='". $timu . "'"); // 從problems庫里查 注意把“查詢”改成“select”,因為百度知道會屏蔽sql語句
if (mysqli_fetch_assoc($sql) 0) {
echo "已存在";
} else {
echo "不存在";
}
PHP代碼里怎么判斷數(shù)據(jù)庫是不是存在呢?
?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("test_db", $con);
if (!$db_selected)
{
die ("Can\'t use test_db : " . mysql_error());
}
mysql_close($con);
?
其實就是先登錄再選擇數(shù)據(jù)庫
mysql_select_db
PHP中如何判斷一個數(shù)據(jù)已經(jīng)存在于數(shù)據(jù)庫中?
材料/工具:電腦、PHP
1、首先,創(chuàng)建一個方法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怎么判斷數(shù)據(jù)庫數(shù)據(jù)是否存在
?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ù)庫不存在';
?
分享題目:php是否數(shù)據(jù)庫有 php的數(shù)據(jù)庫配置在哪
新聞來源:http://ef60e0e.cn/article/doipggj.html