英文:
PHP mysql read function error (Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;)
问题
这是数据库的PHP文件,并且我在第30行($result = $stm->execute($data);)上遇到了致命错误。
数据和查询来自名为user.class.php的文件夹中的read函数,并且我将该函数与它一起使用。
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函数:
$data['url_adress'] = $this->get_random_string_max(60);
//检查是否存在已注册的url_adress
$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);
}
所以我遇到了这个错误,我不知道该怎么办。错误消息如下:
致命错误:未捕获的PDOException:SQLSTATE[42000]:语法错误或访问冲突:1064,您在SQL语法中有错误;请检查与您的MariaDB服务器版本相对应的手册,以确定在第1行附近使用正确的语法。C:\xampp\htdocs\ozimusic\app\core\database.php:30堆栈跟踪:#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} 在C:\xampp\htdocs\ozimusic\app\core\database.php的第30行抛出
英文:
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
答案1
得分: -1
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论