大家好,我们决定开始学习PDO.但是我在创建一个函数来检查我的数据库中是否存在表时遇到了问题.
每个单独的项目都有自己的表格,表格名称为($ id).
<?php
include_once('config.php');
include_once('simple_html_dom.php');
$html = file_get_html('http://localhost:8888/CDOnline%20Online.html');
foreach($html->find('div.product-stamp-inner') as $content) {
$detail['itemid'] = $content->find('a',0)->href;
$detail['itemid'] = substr($detail['itemid'], 40, 6);
if (strpos($detail['itemid'], "?") !== false) {
$detail['itemid'] = substr($detail['itemid'], 0, 5);
}
$id = $detail['itemid'];
tableExists($dbh, $id);
}
function tableExists($dbh, $id)
{
//check if table exists
}
$dbh = null;
?>
Run Code Online (Sandbox Code Playgroud)
我试图搜索论坛寻找答案,但空手而归.让我接近我答案的唯一一件事是:
function tableExists($dbh, $id)
{
$results = $dbh->query("SHOW TABLE LIKE `$id`");
if(count($results)>0){echo 'table exists';}
}
Run Code Online (Sandbox Code Playgroud)
但是这只是表示所有表都存在,当一半的表不存在时.
编辑:如果有1行或更多行,则表应该存在.
hek*_*mgl 12
你正在使用附魔$id.将其更改为单引号.像这样:
"SHOW TABLES LIKE '$id'"
Run Code Online (Sandbox Code Playgroud)
进一步注意,该声明count($results)>0不起作用.你必须改为使用$results->rowCount().
Fxing这两个错误都会给你以下功能:
function tableExists($dbh, $id)
{
$results = $dbh->query("SHOW TABLES LIKE '$id'");
if(!$results) {
die(print_r($dbh->errorInfo(), TRUE));
}
if($results->rowCount()>0){echo 'table exists';}
}
Run Code Online (Sandbox Code Playgroud)