简体   繁体   中英

Undefined index: count(*) PDO

So I'm recreating link generation function from mysql to PDO and I have a problem.

    $sth = $resDB->prepare("SELECT count(*) FROM downloads WHERE downloadkey = '{$strKey}' LIMIT 1");
$arrCheck = $sth->fetchAll();
if($arrCheck['count(*)']){
    //key already in use
    return createKey();
}else{
    //key is OK
    return $strKey;
}

Everything goes well until this line: if($arrCheck['count(*)'])

I get the error like this: Undefined index: count(*)

I just don't know how to change that line to PDO. Can you help me ?

Use an alias to refer to the column

SELECT count(*) as cnt FROM ...

$arrCheck["cnt"]

Try this using alias,

$sth = $resDB->prepare("SELECT count(*) as count FROM downloads WHERE downloadkey = '{$strKey}' LIMIT 1");
$arrCheck = $sth->fetchAll();
if($arrCheck['count']){

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM