Does any one have a better solution for this specific problem ?

问题描述 投票:0回答:1
The goal is to select the data where the g_id is equal with the value I pass to it.

How I create the query string:

then in a function I add this

then I do this

SELECT s_id AS id,
 s_title AS title,
 genre.g_name AS genreName,
 accounts.ac_public_name AS producerName,
 s_price AS price, 
 DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') AS lastModifiedDate, 
 DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s') AS addedDate,
 s_downloads AS downloads,
 s_sales AS sales,
 s_rating AS rating, 
 s_status AS STATUS
FROM song
JOIN accounts ON accounts.ac_id = song.s_producer
JOIN genre ON genre.g_id = song.s_genre
WHERE 1=1 AND genre.g_id = '1'
ORDER BY s_status ASC, s_added_date DESC
LIMIT 0, 5;

and finally

 if(isset($filterData["genreId"]) && $filterData["genreId"] !== ""){
    $queryString .= " AND genre.g_id = :genreId";
 }

This is the part that is related to the problem. I can't post the entire function, it's really long.

 if(isset($filterData["genreId"]) && $filterData["genreId"] !== ""){
    $genreParam = $filterData["genreId"];
    $stmt->bindParam('genreId', $genreParam);
 }

This is used to search data based on some inputs or combinations of inputs. The entire DAO class is really big :)

OS: Windows 10, PhP version: 7.2.19, Apache version: 2.4.35, MariaDB version: 10.4Heidi SQL: 10.2.0.5599

One more edit:

 if(isset($filterData["genreId"]) && $filterData["genreId"] !== ""){
    $queryString .= " AND genre.g_id2 = :genreId";
 }

If I add the columun in the queryString when I first declare it:

and not inside $this->filterDataQuery($queryString, $filterData, "songs"); when I build the queryString based on selected filters it works perfectly. This is the select from the browser console now:

SELECT s_id AS id,
 s_title AS title,
 genre.g_name AS genreName,
 accounts.ac_public_name AS producerName,
 s_price AS price, 
 DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') AS lastModifiedDate, 
 DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s') AS addedDate,
 s_downloads AS downloads,
 s_sales AS sales,
 s_rating AS rating, 
 s_status AS STATUS
FROM song
JOIN accounts ON accounts.ac_id = song.s_producer
JOIN genre ON genre.g_id = song.s_genre
WHERE 1=1 AND accounts.ac_id = '999999'
ORDER BY s_status ASC, s_added_date DESC
LIMIT 0, 5;

Isn't this one the same with the first? For it is. I am blind ?song

SELECT s_id AS id,
 s_title AS title,
 genre.g_name AS genreName,
 accounts.ac_public_name AS producerName,
 s_price AS price, 
 DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') AS lastModifiedDate, 
 DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s') AS addedDate,
 s_downloads AS downloads,
 s_sales AS sales,
 s_rating AS rating, 
 s_status AS STATUS
FROM song
JOIN accounts ON accounts.ac_id = song.s_producer
JOIN genre ON genre.g_id = song.s_genre
WHERE 1=1 AND s_status = '0'
ORDER BY s_status ASC, s_added_date DESC
LIMIT 0, 5;

I found myself in a really weird situation in PDO. A query doesn't want to execute when called from PhP but it does when called from HeidiSQL. The error is in title. SQL query from statement ...

After rereading your first post dozens of times and testing locally all sorts of normal things without getting the same error, I think the

variable inside

 $queryString = "SELECT s_id as id,
                            s_title as title,
                            genre.g_name as genreName,
                            accounts.ac_public_name as producerName,
                            s_price as price, 
                            DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') as lastModifiedDate,
                            DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s')  as addedDate,
                            s_downloads as downloads,
                            s_sales as sales,
                            s_rating as rating,     
                            s_status as status
                            FROM song 
                            JOIN accounts on accounts.ac_id = song.s_producer
                            JOIN genre on genre.g_id = song.s_genre
                            WHERE 1=1 ";

holds a total different query than posted here. And it just doesn't include the table

 if(isset($filterData["genreId"]) && $filterData["genreId"] !== ""){
    $queryString .= " AND genre.g_id = :genreId";
 }

as shown in the error message.If you're not using that function, seeing your second last code block, all works well (if I understand you correctly).

$queryString .= " ORDER BY s_status asc, s_added_date desc";
$queryString .= " LIMIT :offset, :limit;";

Also according to the code shown, I don't understand how you would call

  $stmt = $dbConnector->getConnection()->prepare($queryString);

to add criteria to the querystring, and directly binding the params to the statement object that's prepared later on and thus is not available there (or not the one you would expect).

To prove my theory my complete test code, still using

this db-fiddle

:

$queryString = "SELECT s_id as id,
                            s_title as title,
                            genre.g_name as genreName,
                            accounts.ac_public_name as producerName,
                            s_price as price, 
                            DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') as lastModifiedDate,
                            DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s')  as addedDate,
                            s_downloads as downloads,
                            s_sales as sales,
                            s_rating as rating,     
                            s_status as status
                            FROM song 
                            JOIN accounts on accounts.ac_id = song.s_producer
                            JOIN genre on genre.g_id = song.s_genre
                            WHERE 1=1 AND genre.g_id = :genreId";
        $queryString = $this->filterDataQuery($queryString, $filterData, "songs");
        $queryString .= " ORDER BY s_status asc, s_added_date desc";
        $queryString .= " LIMIT :offset, :limit;";
        $stmt = $dbConnector->getConnection()->prepare($queryString);
        $genreParam = $filterData["genreId"];
        $stmt->bindParam(':genreId', $genreParam);

SELECT s_id AS id,

 s_title AS title,

 genre.g_name AS genreName,

 accounts.ac_public_name AS producerName,

 s_price AS price, DATE_FORMAT(s_last_modified_date, '%d/%m/%Y %H:%i:%s') AS lastModifiedDate, DATE_FORMAT(s_added_date, '%d/%m/%Y %H:%i:%s') AS addedDate,

 s_downloads AS downloads,

 s_sales AS sales,

 s_rating AS rating, 

 s_status AS STATUS
FROM song
JOIN accounts ON accounts.ac_id = song.s_producer
JOIN genre ON genre.g_id = song.s_genre
WHERE 1=1 AND genre.g_id = '1'
ORDER BY s_status ASC, s_added_date DESC
LIMIT 0, 5;

First, I don't pass the $stmt variable to filterDataQuery. I pass it to bindParams. bindParams function is called aftert filterDataQuery is done and after adding the strings for order by and limit.

I really have to thank you,Piemol , for trying to help me.
php sql pdo heidisql mariadb-10.4
1个回答
0
投票

The problem was not in the one that was getting the data, the one I posted about. The problem was with the one doing the count. I did not added the joins there : 未找到列:1054 未知列 PhP PDO MariaDB10$stmt filterDataQuery()我发现自己在PDO中遇到了一个非常奇怪的情况。一个查询在PhP中调用时不想执行,但在HeidiSQL中调用时却能执行。genre错误在标题中。

SQL查询从语句debugDumpParams 。filterDataQuery()接下来是我在查询字符串中添加genre.g_id = :id的部分。

我把它绑在哪里在这两种情况下,$filterData["genreId"]都被设置了,而且它有一个值,所以if没有问题。如果$filterData["genreId"]是空的或者没有设置,查询就没有问题。而错误:SQLSTATE[42S22]: Column not found: 1054 Unknown column `genre.g_id/'in `where clause/'.

ini_set('display_errors', 1);
error_reporting(E_ALL);

$DB_USER = '*****';
$DB_PASS = '*****';
$db = new PDO('mysql:host=localhost;dbname=testing.project', $DB_USER, $DB_PASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$select = 'SELECT s_id AS id,
 s_title AS title,
 genre.g_name AS genreName,
 accounts.ac_public_name AS producerName,
 s_price AS price, 
 DATE_FORMAT(s_last_modified_date, "%d/%m/%Y %H:%i:%s") AS lastModifiedDate, 
 DATE_FORMAT(s_added_date, "%d/%m/%Y %H:%i:%s") AS addedDate,
 s_downloads AS downloads,
 s_sales AS sales,
 s_rating AS rating, 
 s_status AS STATUS
FROM song
JOIN accounts ON accounts.ac_id = song.s_producer
JOIN genre ON genre.g_id = song.s_genre';
$where = ' WHERE genre.g_id = :genreId';
$order = ' ORDER BY s_status ASC, s_added_date DESC';
$limit = ' LIMIT 0, 5';

// No problemo:
$sql = $select . $where . $order . $limit;
$stmt = $db->prepare($sql);
$genreId = 1;
$stmt->bindParam(':genreId', $genreId);
$stmt->execute();

// Trigger error column not found, $stmt containing an unexpected query:
$select = 'SELECT * FROM `song`';
$sql = $select . $where . $order . $limit;
$stmt = $db->prepare($sql);
$genreId = 1;
$stmt->bindParam(':genreId', $genreId);
$stmt->execute();
但是等等,还有更多。如果我把genre.g_id2而不是genre.g_id放进去,它就会像这样。

0
投票

表中存在100%的列。这个查询也有类似的问题。

似乎问题只出现在表中的列上,而不是表上的列

JOIN与。

下一个查询完美运行。

  $responseMessage = json_encode(AdminDAO::getInstance()->getFilteredSongsList($message));
  $count = AdminDAO::getInstance()->getFilteredSongsListItemsCount($message); // this one was the problem

2天了,没有解决。我找到的大多数解决方案是再次检查该列是否真的存在:/www.heypasteit.comclip0IUPWG。

第一个是失败的,第二个是工作的。我非常想念Java的log4j。谢谢大家想帮助我。

对于有这种怪问题的人,检查日志,放回声,不要把注意力放在AJAX响应日志的第一件事上。

© www.soinside.com 2019 - 2024. All rights reserved.