#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2

    I get database not selected


    I have went ahead and just learned how use pdo and I having a problem when I am trying to put the path into the database.

    here is my connect file:
    PHP Code:
    <?php

    $connect
    ['db'] = array(

        
    'dbhost'  => '127.0.0.1',
        
        
    'dbname'  => 'portfolio',
        
        
    'user'  => 'root',
        
        
    'pass'  => ''

    );


    $db = new PDO('mysql:host= ' $connect['db']['dbhost'] . '; dbname = '$connect['db']['dbname'], $config['db']['user'], $config['db']['password']);


    ?>

    Is there an error in my syntax? If not can it be included into another file?

    then here is where the above script is being included:

    PHP Code:
    <?php

    //error_reporting(0);


    if($_POST['submit']){
            
        
    //assigning variables to different values of $_FILES array
        
    $name addslashes($_FILES['upload']['name']);

        
    $tn   addslashes($_FILES['upload']['tmp_name']);

        
    $size $_FILES['upload']['size'];
        
        
        
    //other variables
        
    $album_name $_POST['album_name'];
        
        
    $file_directory 'upload/upload/'.$album_name;

        
        
    //upload sec
        
    $to 'upload/upload/'$album_name .'/'.$name;
        
        
    $exts = array('jpg''jpeg''gif''png');
        
        
    $ext strtolower(end(explode('.'$name)));


        
    //vars for db
           
            //RIGHT HERE    
            
    include 'upload/connect.php';
        
        
    $directory_path 'upload/upload/'.$album_name;
        
        
    $desc $_POST['description'];
                
        
    $sql "INSERT INTO Group_1(dir_path, image_description) VALUES(:directory_path, :desc)";    

        
    $q $db -> prepare($sql);    
        
        
        
        if(
    in_array($ext$exts) === FALSE){
            
            echo 
    'That file type is not supported!<br /><br /><a href="upload/index.php">Go Back</a>';
            
        }elseif(
    $size 1073741824){
            
            echo 
    'Sorry, but that file is to big! Only files that are no bigger than 1gb!<br /><br /><a href="upload/index.php">Go Back</a>';
            
        }else{
                
            if(!
    file_exists($file_directory)){
                    
                
    mkdir('upload/upload/'.$album_name);
                
                    
    $q -> exacute(array(
                                  
                                  
    ':directory_path'  => '$directory_path',
                    
                      
    ':desc' => '$desc'
                    
                    
    ));
                    
                if(!
    move_uploaded_file($tn$to)){
                        
                    echo 
    'Sorry, but the upload failed!<br /><br /><a href="upload/index.php">Go Back</a>';
                        
                }else{
                    
                    echo 
    'File successfully uploaded!<br /><br /><a href="upload/index.php">Go Back</a>';    
                
                }
                        
            }else{
                    
                if(!
    move_uploaded_file($tn$to)){
                        
                    echo 
    'Sorry, but upload failed!<br /><br /><a href="upload/index.php">Go Back</a>';
                        
                }else{
                        
                    echo 
    'File successfully uploaded!<br /><br /><a href="upload/index.php">Go Back</a>';
                        
                }
                    
            }
                    
        }
                
    }

    ?>
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    For one thing you never instantiate $q.

    P.S. It is 'execute' not 'exacute'.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    I dont quite know what you mean about instantiate $q. I am new to PDO and still learning how to do because I am use to using the old method. Could I please have an example if is not to much trouble of what your talking about?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    Sounds like you are unfamiliar with OOP. Northie wrote an excellent introduction so go here first. For a better job than I can do on using PDO go here.
    Last edited by gw1500se; February 22nd, 2013 at 07:09 AM.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    No offense, gw1500se, but I think you're really on the wrong track here.

    $q doesn't have to be "instantiated", it simply holds the return value of $db, as you can see in this line:
    PHP Code:
    $q $db -> prepare($sql); 
    So nothing wrong with how he/she uses PDO or OOP in general.

    As far as I can tell, the problem is due to the spaces in the DSN string. Remove them so that you only have
    Code:
    mysql:host=...;dbname=...
    And turn on your error reporting. The PDO instantiation alone should spill out plenty of error messages.

    Comments on this post

    • gw1500se agrees : None taken, I am here to learn too. Dopey me completely missed that somehow.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    I changed it to this and still I get no database selected:

    PHP Code:
    <?php

    $dbhost 
    'localhost';

    $dbname 'portfolio';

    $dbuser 'root';

    $dbpass '';


    $db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

    //THIS LINE DOES NOT RETURN NOTHING=
    $db ->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);

    ?>
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    You need to add some error checking:
    PHP Code:
    try {
       
    $db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    }
    catch (
    PDOException $e) {
       die(
    "Connection failed: ".$e->getMessage();

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    I tried the above code and I get no errors, just a blank page
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    Oops. Syntax error in my code. Add the missing ).
    PHP Code:
    die("Connection failed: ".$e->getMessage()); 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    I caught that syntax error and still no errors just a blank page. I it is quite allright, everybody makes mistakes. You should see some of the noob mistakes i make.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    If you're still getting a blank page then there must be a syntax error somewhere. Check the httpd log.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    87
    Rep Power
    2
    Well I have to go to work now but will be on later. If you were talking about httpd.pid in the logs folder of xampp then all it said was 387 but when I went into the error log it said this:

    [Fri Feb 22 11:48:08 2013] [warn] Init: Session Cache is not configured [hint: SSLSessionCache]
    [Fri Feb 22 11:48:11 2013] [notice] Digest: generating secret for digest authentication ...
    [Fri Feb 22 11:48:11 2013] [notice] Digest: done
    [Fri Feb 22 11:48:12 2013] [notice] Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming normal operations

    I dont know if this means anything?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    Not with respect to the PHP problem. Make sure error logging is turned on in php.ini.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,399
    Rep Power
    594
    Not with respect to the php error. Make sure error logging is turned on in php.ini.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo