phpmysqlsqlpdomysql-error-1064

PHP mysql read function error (Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;)


This is database. php file: and I am getting fatal error on line 30($result = $stm->execute($data);)

The data and query there goes from the read function in the folder named user.class.php and I carry that function with it.

Class Database
{
    public static $con;

    public function __construct()
    {
        try{
            $string = DB_TYPE .": host=" . DB_HOST . ";dbname=" . DB_NAME;
            self::$con = new PDO($string,DB_USER,DB_PASS);
        }catch (PDOException $e){
            die($e->getMessage());
        }
    }

    public static function getInstance()
    {
        if(self::$con)
        {
            return self::$con;
        }

        return $instance = new  self();
    }

    public function read($query, $data=array())
    {
        $stm= self::$con->prepare($query);
        $result = $stm->execute($data);

        if($result)
        {
            $data = $stm->fetchAll(PDO::FETCH_OBJ);
        
            if(is_array($data) && count($data) >0)
            {
                return $data;
            }
        }
        return false;

        
    }

user.class.php read function:

$data['url_adress'] = $this->get_random_string_max(60);
        //kayıtlı url_adress var mı
        $arr = false;
        $sql = "SELECT * from users where url_adress = :url_adress limit ";
        $arr['url_adress'] = $data['url_adress'];
        $check = $db->read($sql,$arr);

        if(is_array($check))
        {
            $data['url_adress'] = $this->get_random_string_max(60);
        }

So I have this error and I dont know waht to do error message:

Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1 in C:\xampp\htdocs\ozimusic\app\core\database.php:30 Stack trace: #0 C:\xampp\htdocs\ozimusic\app\core\database.php(30): PDOStatement->execute(Array) #1 C:\xampp\htdocs\ozimusic\app\models\user.class.php(54): Database->read('SELECT * from u...', Array) #2 C:\xampp\htdocs\ozimusic\app\controllers\signup.php(12): User->signup(Array) #3 C:\xampp\htdocs\ozimusic\app\core\app.php(40): Signup->index('home') #4 C:\xampp\htdocs\ozimusic\public\index.php(15): App->__construct() #5 {main} thrown in C:\xampp\htdocs\ozimusic\app\core\database.php on line 30


Solution

  • The SQL query is wrong as a LIMIT should have, at least, a row count.

    Solution 1

    Handle the limit and change the query to:

    $sql = "SELECT * from users where url_adress = :url_adress limit :row_count";
    

    Don't forget to specify the expected limit:

            $arr['row_count'] = 42;
    

    Solution 2

    Remove the limit if you want to retrieve all results:

    $sql = "SELECT * from users where url_adress = :url_adress";
    

    Documentation