【发布时间】:2013-04-23 21:33:55
【问题描述】:
大家好,决定开始学习 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;
?>
我试图搜索论坛寻找答案,但空手而归。唯一让我接近答案的是:
function tableExists($dbh, $id)
{
$results = $dbh->query("SHOW TABLE LIKE `$id`");
if(count($results)>0){echo 'table exists';}
}
但这只是说所有表都存在,而一半的表不存在。
编辑:如果有 1 行或更多行,则该表应该存在。
【问题讨论】:
-
通过创建同一张表的
n副本,您只会让您的生活更加困难。您需要有一个带有item_id列的一个 表。说真的。