PHP Fatal Error

Hello! So I am making something in PHP but I came across a fatal error. Here it is:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /Users/DannyTran/Desktop/WebServer/app/classes/DB.php:12 Stack trace: #0 /Users/DannyTran/Desktop/WebServer/app/classes/DB.php(12): PDOStatement->execute(Array) #1 /users/DannyTran/Desktop/WebServer/app/register.php(24): DB::query('INSERT INTO use...', Array) #2 {main} thrown in /Users/DannyTran/Desktop/WebServer/app/classes/DB.php on line 12

I do not understand what it means so I was hoping someone can help me.

Here is Register.php

<?php
    include('./classes/DB.php');
    $pdo = new PDO('mysql:host=127.0.0.1;DBname=socialnetwork;charset=utf8', 'root', 'password');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if(isset($_POST['register'])) {
        $fn = strip_tags(@$_POST['fname']);
        $ln = strip_tags(@$_POST['lname']);
        $un = strip_tags(@$_POST['uname']);
        $em = strip_tags(@$_POST['email']);
        $pswd = strip_tags(@$_POST['password']);
        $pswd2 = strip_tags(@$_POST['password2']);
        $country = strip_tags(@$_POST['country']);
        $gender = strip_tags(@$_POST['gender']);
        $date = date("m-d-Y"); // Year - Month - Day
        $no = 0;
        $role = 'Member';
        $status = 'Hello world!';
        $website = '';  
        $bio = '';



        DB::query('INSERT INTO users VALUES (:username, :first_name, :last_name, :email, :password, :gender, :country, :verified, :role, :bio, :website, :status, :register, :last, :activated)', array(':username'=>$un,':first_name'=>$fn, ':last_name'=>$ln, ':email'=>$em, ':password'=>$pswd, ':gender'=>$gender, ':country'=>$country, ':verified'=>$no, ':role'=>$role, ':bio'=>$bio, ':website'=>$website, ':status'=>$status, ':register'=>$date, ':last'=>$date, ':activated'=>$no));
        echo "Welcome!";
    };

?>




<h1>Register Now</h1>
        <form action="#" method="post">
            <input type="text" placeholder="First Name" name="fname" required/><br /><br />
            <input type="text" placeholder="Last Name" name="lname" required/><br /><br />
            <input type="text" placeholder="Username" name="uname" required/><br /><br />
            <input type="email" placeholder="E-Mail Address" name="email" required/><br /><br />
            <input type="password" placeholder="Password" name="pswd" required/><br /><br />
            <input type="password" placeholder="Confirm Password" name="pswd2" required/><br /><br />
            <select name="gender">
                <option value="" disabled selected>Gender</option>
                <option value="Male">Male</option>
                <option value="Female">Female</option>
                <option value="Other">Other</option>
            </select><br /><br />
            <?php require_once './options/countries.php'; ?><br /><br />
            <input name="register" type="submit" value="Login" /><br /><br />
        </form>
        <a href="login.php">Have an account? Login!</a>

Here is my DB.php

<?php
	class DB {
		
		private static function connect() {
			$pdo = new PDO('mysql:host=127.0.0.1;DBname=socialnetwork;charset=utf8', 'root', 'password');
    		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    		return $pdo;
		}

		public static function query($query, $params) {
			$statement = self::connect()->prepare($query);
			$statement->execute($params);
		}

	}

?>

Thank you for your help!

The way in which you have setup your database class with static calls I would not advise, anyhow to temporarily solve your problem you can do:

$pdo = DB::connect();

instead of

$pdo = new PDO('mysql:host=127.0.0.1;DBname=socialnetwork;charset=utf8', 'root', 'password');

As you are apparently starting with PDO, let me recommend you my PDO tutorial, which explicitly says

Note that it’s important to follow the proper format - no spaces or quotes or other decorations have to be used in DSN, but only parameters, values and delimiters, as shown in the manual.

Means you shouldn’t decorate a DSN at all, including capitalization of DBname which should go as plain dbname.

Besides, with your DB class you are effectively creating a new connection to the database for the every query you run. Which is an awful overkill and overload. Create a connection only once.

Besides, your query function ought to return the statement or you won’t be able to use the query results.

As a solution for all these problems let me suggest you my simple PDO wrapper which is doing everything you want but without such drawbacks. Note the Examples section which is real neat and could be run as is, without any setup.

@Andres_Vaquero it won’t help because he is explicitly calling connect() in the query() every time it is called.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.