Note that there are some explanatory texts on larger screens.

plurals
  1. POPDO Execute Returns Nothing
    primarykey
    data
    text
    <p>Assume that I have a class:</p> <pre><code>class pdoc { private static $db_connect_pool; public static function openConnect() { try { $connect_options_arr = array(PDO::ATTR_PERSISTENT =&gt; true); self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr); } catch (Exception $e) { print_r($e); } } public static function getConnection() { return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.'']; } public static function qry($sql) { self::openConnect(); $db_handle = self::getConnection(); $st_handle = $db_handle-&gt;prepare($sql); return $st_handle-&gt;execute(); } } </code></pre> <p>Then, to call the class:</p> <pre><code>$sql = "SELECT * FROM sometable"; if(pdoc::qry($sql)) echo "y"; else echo "n"; </code></pre> <p>Why the code always return <code>n</code>? I have check the connection that has been connected successfully, but while I've tried to execute some query, it returns nothing. Any ideas? Thanks.</p> <hr> <h2>UPDATE <i>(@Robbie's code)</i></h2> <pre><code>class pdoc { private static $db_connect_pool; public static function openConnect() { try { $connect_options_arr = array(PDO::ATTR_PERSISTENT =&gt; true, PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION); self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.''] = new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", DB_USER, DB_PASS, $connect_options_arr); } catch (Exception $e) { print_r($e); } } public static function getConnection() { return self::$db_connect_pool[''.DB_DRIVER.'_'.DB_NAME.'']; } public static function qry($sql) { self::openConnect(); $db_handle = self::getConnection(); try { $st_handle = $db_handle-&gt;prepare($sql); $retval = $st_handle-&gt;execute(); //--&gt; Got error on this line } catch (Exception $e) { Die('Need to handle this error. $e has all the details'); } return $retval; } } </code></pre> <p>The error said: <code>exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected'</code>.</p> <hr> <h2>ANSWER</h2> <p>Change:</p> <pre><code>... new PDO("".DB_DRIVER.":host=".DB_HOST.";db_name=".DB_NAME."", ... </code></pre> <p>into:</p> <pre><code>... new PDO("".DB_DRIVER.":host=".DB_HOST.";dbname=".DB_NAME."", ... </code></pre> <p>After catch the error message (from the updated code) and referring to <a href="https://stackoverflow.com/questions/6300446/write-php-pdo-queries-as-dbname-tablename-as-opposed-to-tablename-why">this thread</a>, I found that <code>dbname</code> part has written as <code>dn_name</code> on my code. So I change it into <code>dbname</code> and it works perfect! Thanks @Robbie for your code! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload