简体   繁体   中英

PDO code completion in NetBeans

I'm using NetBeans since its version 7.0 and its my favorite IDE for PHP since than, but today I was using it for a project and its giving me problem in PDO code completion. There are two cases,

1) If I am instantiating the PDO object on the same page, than all of the code completion is correct.

2) But when I am keeping my PDO object in another file and requiring that file, than the code completion doesn't work.

EXAMPLES

1)

<?php
/**
 * In this case the code completion works fine
 */
try {
    $db = new PDO('mysql:host=localhost;dbname=tshop', 'root', 'mypass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

2

<?php
/**
 * But in this case it doesn't work
 */
try {
    require_once '../../includes/database_connection.php';
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

Thank you Everyone in advance !

Adding a comment should fix the issue i beleive:

try {
    require_once '../../includes/database_connection.php';
    /* @var $db PDO */
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

That said if i were I would not go about establishing DB connections in the way because if you arent careful you can easily have multiple connections going by accident which could potentially exceed the max connections.

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