简体   繁体   中英

PHP PDO not working

i have declared a PDO and for some reason, the script stalls when declaring it. It gives me no error, just stalls for some reason. I cant seem to get to the bottom of it. If i am doing something wrong, please let me know

<?php

 // this function creates a PDO that can be used to connect to our mysql server
 function getConnectionPDO($dbName){
     if(!empty($dbName)){
         $pdo = new PDO('mysql:host=localhost;port=8889;dbname='.$dbName,'root','root');
         return $pdo;
     }
     else {
         return null;
     }
 }

 // gets login parameters
 function getLogin($username,$password){
     if(!empty($username) && !empty($password)){
         $pass_hash1 = md5($password);
         $pass_hash2 = md5($pass_hash1); // hashes the password twice
         //creating connection
         $pdo = getConnectionPDO('User_Data');
         if($pdo != null){
             $query_string = 'SELECT * FROM users WHERE username=:username AND password=:password';
             $prepare = $pdo->prepare($query_string); // preparing for query
             // binding parameters
             $prepare->bindParam(':username', $username);
             $prepare->bindParam(':password', $pass_hash2);
             // testing if query passed successfully
             $columns = $prepare->fetchColumn();
             if($columns == 1){
                 return true;
             }
             else {
                 return false;
             }
         }
         else {
             return false;
         }
     }
 }
?>

thanks in advance for the help =)

The code you have posted doesn't seem to have a error, so it could be anything.

You might find it easier to debug if you pass the constructor its forth 'options' parameter with PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION .

This will make PDO throw an exception when it encounters an error.

$pdo = new PDO('mysql:host=localhost;port=8889;dbname='.$dbName,'root','root', array(
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));

Give it a try:

 new PDO("mysql:host=localhost;port=8889;dbname=$dbName",'root','root');

Also add an

$prepare->execute(); 

after the last bindParam

It sounds like your connection is timing out. Can you verify that MySQL is running, and is running on port 8889?

If you're on Linux, run netstat -tulpn , find the PID/Program Name matching XXXX/mysqld and make sure the local address is 127.0.0.1:8889 , if it's not then your PDO has the wrong port configuration. If you're on Windows, you can run (in a command prompt with Administrator privilages netstat -a -b (You may need to press CTRL+C to stop the execution) and look for mysqld.exe, and make sure the local address is 0.0.0.0:8889 .

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