Search code examples
phpmysqlpdobitnami

PDO Exception 1049 when migrating PHP application from MySQL


I have been trying to migrate the following mysql connection function to PDO:

function validate_email_input($email)
    {
      $dbc = @mysql_connect('localhost', 'userName', '******');
      mysql_select_db('users', $dbc);

      $query = "SELECT email FROM user_list WHERE email='$email'";
      if ($result = mysql_query($query, $dbc))
        {
        $row = mysql_fetch_array($result);
        if ($row['email'] == $email)
            {
            return TRUE;
            }
          else
              {
              return FALSE;
              }
        }
        else
            {
            echo '<p class="error">Could not retrieve he data because:<br />' . mysql_error($dbc) . '.</p>
                    <p>The query being run was: ' . $query . '</p>';
            }
      }

This works just fine. But I get an error when I try the following PDO function to achieve the same result:

function validate_email_input($email)
    {
        // Step 1: Establish a connection
        $db = new PDO("mysql:host=localhost;dbname=users", "userName", "******");

        // Step 2: Construct a query
        $query = "SELECT * FROM user_list WHERE email = '$email'";

        // Step 3: Send the query
        $result = $db->query($query);

        // Step 4: Iterate over the results
        if ($result)
            {
            $row = $result->fetch(PDO::FETCH_ASSOC);
            if ($row['email'] == $email)
                {
                return TRUE;
                }
              else
                  {
                  return FALSE;
                  }
            }
        // Step 5: Free used resources
        $result->closeCursor();
        $db = null;
    }

And yes - I did try the above function with try-catch but that doesn't affect anything. I still get the error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1049] Unknown database 'users''

I'm using the apache2 module included with Bitnami RubyStack (v. 1.9.3-25), which uses PHP version 5.4.3 Already checked php.ini file and all the PDO extensions are uncommented. Please help! Thanks


Solution

  • Found the solution! Apparently it wanted me to specify the port number (localhost:3307) instead of just localhost when creating the PDO object. Anyone know how to change the default localhost port in Bitnami RubyStack for Windows?