小编DrD*_*Dog的帖子

PDO SHOW TABLES数组

只是使用这个功能,并没有按计划进行.它应该抓取数据库中的所有表名并将它们存储在一个数组中.但是,数组的结果使下面示例中显示的数组加倍:

Array ( [0] => 113340 ) 
Array ( [0] => 113340 [1] => 116516 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 [3] => 20731 ) 
Array ( [0] => 113340 [1] => 116516 [2] => 139431 [3] => 20731 ... )
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码:

function itemDiscontinued($dbh, $id, $detail) {
  try {
    $tableList = array();
    $result = $dbh->query("SHOW TABLES");
    while ($row = $result->fetch(PDO::FETCH_NUM)) {
      $tableList[] …
Run Code Online (Sandbox Code Playgroud)

php mysql arrays pdo

9
推荐指数
1
解决办法
2万
查看次数

使用PDO检查表是否存在

大家好,我们决定开始学习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行或更多行,则表应该存在.

php mysql pdo

1
推荐指数
1
解决办法
9702
查看次数

PDO创建表不起作用

只是在使用PDO插入新表时遇到问题.我得到的错误是:

SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误; 检查与MySQL服务器版本对应的手册,以便在''25500'附近使用正确的语法(第149440行的id INT(11)NOT NULL AUTO_INCREMENT,dateTIMESTAMP NOT NULL DEFAUL'不存在!

请注意,$ id是表名.

function createTable($dbh, $id, $description)
{
try {
$stmt = "CREATE TABLE '$id' (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`prodtitle` VARCHAR(50) NOT NULL ,
`unitsize` VARCHAR(10) NOT NULL ,
`price` VARCHAR(10) NOT NULL ,
`wasprice` VARCHAR(10) NOT NULL ,
`specprice` VARCHAR(10) NOT NULL ,
PRIMARY KEY (`id`) ) COMMENT = '$description'";

if($dbh->exec($stmt) !== false) echo 'The sites table …
Run Code Online (Sandbox Code Playgroud)

php mysql pdo

1
推荐指数
1
解决办法
1539
查看次数

标签 统计

mysql ×3

pdo ×3

php ×3

arrays ×1