Unknown column in 'where clause', php - php

I installed something called the "STHS Web Portal" (Here: http://www.simhl.net/node/8)
It's on an older php version and I have been going through and fixing the many small problems that keep coming up in the different files.
I got to one file, and after fixing most the problems, this happens:
Unknown column 'o.PlayerType' in 'where clause'
This seems to be the section where the problem is coming from:
$query_GetTransactions = sprintf("SELECT o.*, 'False' as PosG, p.Number, p.Name FROM playerscontractoffers as o, players as p WHERE o.Player=p.Number AND o.PlayerType='player' AND (o.Type='Extension' OR o.Type='Signed') AND o.Approved='True' UNION ALL SELECT o.*, 'True' as PosG, p.Number, p.Name FROM playerscontractoffers as o, goalies as p WHERE o.Player=p.Number AND o.PlayerType='goalie' AND (o.Type='Extension' OR o.Type='Signed') AND o.Approved='True' ORDER BY DateCreated DESC LIMIT 0,50");
$GetTransactions = mysqli_query($connection, $query_GetTransactions) or die(mysqli_error($connection));
$row_GetTransactions = mysqli_fetch_assoc($GetTransactions);
$totalRows_GetTransactions = mysqli_num_rows($GetTransactions);
I'm quite new to php, and I have no idea what to do. If you need more of the file, please let me know.

Related

Error Code: 1054 Unknown column 'opdrachten.OpdrachtID' in 'field list'

When I run this query I get an error message but I dont know what Im doing wrong.
SELECT deelnemers.StudentenID, voorkeur.VoorkeurID,
opdrachten.OpdrachtID, voorkeur.Voorkeur
FROM deelnemers
INNER JOIN voorkeur, opdrachten ON voorkeur.StudentenID=deelnemers.StudentenID
WHERE VoorkeurID=1
AND voorkeur.Voorkeur=opdrachten.OpdrachtID
This is query is inserted in an table. Using PHP but when I tried running the query with SQLyog but still the same problem. I also checked multiple times if the column exists and yes it does
SELECT deelnemers.StudentenID, voorkeur.VoorkeurID, opdrachten.OpdrachtID,
voorkeur.Voorkeur FROM deelnemers
INNER JOIN voorkeur ON voorkeur.StudentenID = deelnemers.StudentenID INNER JOIN
opdrachten
ON voorkeur.StudentenID = deelnemers.StudentenID
WHERE VoorkeurID=1 AND voorkeur.Voorkeur = opdrachten.OpdrachtID
This was the what I used and Now I got what I want

Magento Upgrade Issue 1.5.1.0 to 1.9.2.1

I have just upgraded Magento from 1.5.1.0 to 1.9.2.1 and I am stuck at this error when I try to view a product:
a:5:{i:0;s:1427:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'rule.calculate_subtotal' in 'field list', query was: SELECT `main_table`.`tax_calculation_rate_id`, `main_table`.`tax_calculation_rule_id`, `main_table`.`customer_tax_class_id`, `main_table`.`product_tax_class_id`, `rule`.`priority`, `rule`.`position`, `rule`.`calculate_subtotal`, `rate`.`rate` AS `value`, `rate`.`tax_country_id`, `rate`.`tax_region_id`, `rate`.`tax_postcode`, `rate`.`tax_calculation_rate_id`, `rate`.`code`, IF(title_table.value IS NULL, rate.code, title_table.value) AS `title` FROM `tax_calculation` AS `main_table` INNER JOIN `tax_calculation_rule` AS `rule` ON `rule`.`tax_calculation_rule_id` = main_table.tax_calculation_rule_id INNER JOIN `tax_calculation_rate` AS `rate` ON rate.tax_calculation_rate_id = main_table.tax_calculation_rate_id LEFT JOIN `tax_calculation_rate_title` AS `title_table` ON rate.tax_calculation_rate_id = title_table.tax_calculation_rate_id AND title_table.store_id = '1' WHERE (customer_tax_class_id = 3) AND (product_tax_class_id IN ('2')) AND (rate.tax_country_id = 'GB') AND (rate.tax_region_id IN(0, 0)) AND (rate.zip_is_range IS NULL) AND (rate.tax_postcode IS NULL OR rate.tax_postcode IN('*', '', 'ig9 5bw', 'ig9 5bw*', 'ig9 5b*', 'ig9 5*', 'ig9 *', 'ig9*', 'ig*', 'i*')) ORDER BY `priority` ASC, `tax_calculation_rule_id` ASC, `tax_country_id` DESC, `tax_region_id` DESC, `tax_postcode` DESC, `value` DESC";i:1;s:6703:"#0 /home/wwwarabesquecost/public_html/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
I have tried to manually add it using this link : https://sarfarazlaghari.wordpress.com/2015/06/06/magento-sqlstate42s22-column-not-found-1054-unknown-column-rule-calculate_subtotal-in-field-list/ but this doesn't make a difference.
Has anybody had this issue before?
Regarding
I have tried to manually add it using this link : https://sarfarazlaghari.wordpress.com/2015/06/06/magento-sqlstate42s22-column-not-found-1054-unknown-column-rule-calculate_subtotal-in-field-list/ but this doesn't make a difference
Are you saying you've added the column to the tax_calculation_rule table but you're still getting the above error? If that's the case then you've updated the wrong database table, or a configuration file in app/etc is pointed at a different database from the one you think it is.
If that's not what you're saying then be clearer in your question.
The query that you found at that link has an obvious error: it create a new field in the table with the same name of the table.
The right query, which solve the error, is the following:
ALTER TABLE tax_calculation_rule
ADD calculate_subtotal TINYINT(1) NOT NULL DEFAULT 0
Hope it helps.
You shouldn't run sql alternations just like that. The OP mentioned this has happened after an upgrade to 1.9x.
Having such errors means that your codebase is upgraded but your database is still outdated.
The best way for this would be to alter the core_resource table for the module (e.g. tax_setup) to the version that his database was aware previously, and let magento handle the database updates.

PHP PDO too slow on SELECT with some joins

I'm having a performace problem with the execution of a select in PHP PDO.
Using a script available here at stackoverflow (Simplest way to profile a PHP script), I identified where the problem IS, but I have not found a solution.
My select that is the problem is:
SELECT REDACAO.ID_REDACAO AS ID_REDACAO,
DATE_FORMAT(REDACAO.DATA,'%d/%m/%Y') AS DATAE,
ALUNO.ID_ALUNO AS ID_ALUNO,
(SELECT IFNULL((DATEDIFF(DATE_ADD((SELECT MAX(DATA) FROM REDACAO WHERE ID_ALUNO = ALUNO.ID_ALUNO AND ID_REDACAO NOT IN (SELECT ID_REDACAO FROM CORRECAO)), INTERVAL 7 DAY), now())),NULL) as DATA FROM REDACAO LIMIT 1) AS ULTIMA,
ALUNO.NOME as ALUNO,
REDACAO.ID_TEMA AS ID_TEMA,
TEMA.TITULO as TEMA,
TEMA.MOTIVACIONAIS AS MOTIVACIONAIS,
REDACAO.TEXTO AS TEXTO,
REDACAO.ID_STATUS AS STATUS,
B.NOTA as NOTA,
B.RCORRIGIDA AS CORRIGIDA,
B.NOTA1,
B.COMENTARIO1,
B.NOTA2,
B.COMENTARIO2,
B.NOTA3,
B.COMENTARIO3,
B.NOTA4,
B.COMENTARIO4,
B.NOTA5,
B.COMENTARIO5,
B.COMENTARIO6,
C.COMENTARIO AS COMENTARIO
FROM REDACAO
LEFT OUTER JOIN (SELECT SUM(CORRECAO.C1+CORRECAO.C2+CORRECAO.C3+CORRECAO.C4+CORRECAO.C5) AS NOTA, RCORRIGIDA AS RCORRIGIDA, CORRECAO.C1 as NOTA1, CORRECAO.COM1 as COMENTARIO1, CORRECAO.C2 as NOTA2, CORRECAO.COM2 as COMENTARIO2, CORRECAO.C3 as NOTA3, CORRECAO.COM3 as COMENTARIO3, CORRECAO.C4 as NOTA4, CORRECAO.COM4 as COMENTARIO4, CORRECAO.C5 as NOTA5, CORRECAO.COM5 as COMENTARIO5, CORRECAO.COMGERAL AS COMENTARIO6, CORRECAO.ID_REDACAO FROM CORRECAO GROUP BY CORRECAO.ID_REDACAO) B
ON B.ID_REDACAO = REDACAO.ID_REDACAO
JOIN ALUNO ON ALUNO.ID_ALUNO = REDACAO.ID_ALUNO
JOIN TEMA ON TEMA.ID_TEMA = REDACAO.ID_TEMA
LEFT OUTER JOIN (SELECT (COUNT(COMENTARIO.ID_COMENTARIO)) AS COMENTARIO, COMENTARIO.ID_REDACAO FROM COMENTARIO GROUP BY COMENTARIO.ID_REDACAO) C
ON C.ID_REDACAO = REDACAO.ID_REDACAO
WHERE REDACAO.ID_PROFESSOR = $CodProfessor
and REDACAO.ID_STATUS != 6
ORDER BY (CASE WHEN REDACAO.ID_STATUS = 4 THEN 1 ELSE 0 END) DESC
I'm using (PDO :: FETCH_ASSOC) to get the data. Some columns respond in less than 1 second and others in more than 20 seconds.
Any idea what could be the problem and how to solve it?
Your query contains following that will slow it down:
many joins
many subselects
select without where
functions like COUNT, isnull, datediff, sum.(some of these may cancel an index)
case when
order by
group by
Depending on your indexes, on how the tables are joined, and on how big are the tables, this will eventually get very slower.
Try using 'explain' command, and simplify the query if possible.
explain output
a good video about explain
PDO is not at fault; the query is complex. And there may be missing indexes.
Turn this into a LEFT JOIN (because IN (SELECT...) optimizes poorly.
AND ID_REDACAO NOT IN ( SELECT ID_REDACAO FROM CORRECAO)
Upgrade to 5.6; it has some improvements.
You have two JOIN ( SELECT ... ). Before 5.6 that would be optimized terribly. Move one of them out into a temp table, to which you add a suitable index.
In one of the subqueries, GROUP BY CORRECAO.ID_REDACAO seems to be unnecessary.
These indexes (or PRIMARY KEYs) are needed:
CORRECAO: (ID_REDACAO)
REDACAO: (ID_REDACAO), (ID_PROFESSOR)
ALUNO: (ID_ALUNO)
TEMA: (ID_TEMA)
COMENTARIO: (ID_REDACAO, ID_COMENTARIO) ("compound index")
If those suggestions do not help enough, come back with SHOW CREATE TABLE for each table.

SQL order by issue - cannot order by column specified

I am not an expert in SQL therefore need your help.
I have this advanced query written by someone else:
SELECT cms_product_bid.*,
cms_products.*,
cms_products_images.pd_image,
cms_products_images.pd_image_label,
IF (cms_product_bid.pd_bid = 1,
IF(cms_product_bid.pd_bid_start_date <= CURDATE(),
IF(cms_product_bid.pd_bid_end_date >= CURDATE(),1,0)
,0)
,1) AS bid_product_check
FROM cms_products
LEFT JOIN cms_products_images
ON (cms_products.pd_id = cms_products_images.pd_id)
INNER JOIN cms_product_bid
ON (cms_product_bid.product_id = cms_products.pd_id)
INNER JOIN cms_home_product_slider
ON (cms_products.pd_id = cms_home_product_slider.pd_id)
WHERE cms_products.pd_status=1
AND cms_products.pd_visibility=1
AND cms_home_product_slider.cat_id='$featured_cat_id'
GROUP BY cms_products.pd_id
HAVING(bid_product_check =1)
ORDER BY cms_products.pd_sort ASC
All I am trying to do is to sort by cms_products.pd_sort but it isn't sorting by that column even though column is there in table with values such as 1, 2 and so on.
Can anyone point out the issue here ?
my guess is that your column is of type VARCHAR or string, you can simply CAST it
SELECT...
FROM...
WHERE...
ORDER BY CAST(cms_products.pd_sort AS SIGNED) ASC
it may be a bug... but try to list out the columns instead of using the .* shortcuts... then see if that column will be recognized and sorted correctly.
btw - using .* is not a best practice for many reasons.

Invalid SQL Syntax error

Few weeks ago my code was all fine, but recently I've been unable to load my test database on my web app (built using CakePhp).
This is the error :
Warning (512): SQL Error: 1064: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near ') LEFT JOIN blogs AS Blog ON
(Song.blog_id = Blog.id) WHERE 1 = 1' at line 1
[CORE/cake/libs/model/datasources/dbo_source.php, line 684]
SELECT COUNT(*) AS `count`
FROM `songs` AS `Song`
LEFT JOIN libraries AS `Library` ON (`Song`.`id` = `Library`.`song_id` AND `Library`.`user_id` =)
LEFT JOIN `blogs` AS `Blog` ON (`Song`.`blog_id` = `Blog`.`id`)
WHERE 1 = 1
Warning (512): SQL Error: 1064: You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near ') WHERE 1 = 1 ORDER BY Song.postdate
desc LIMIT 100' at line 1
[CORE/cake/libs/model/datasources/dbo_source.php, line 684]
SELECT *, Blog.id
FROM `songs` AS `Song`
LEFT JOIN libraries AS `Library` ON (`Song`.`id` = `Library`.`song_id` AND `Library`.`user_id` =)
WHERE 1 = 1
ORDER BY `Song`.`postdate` DESC
LIMIT 100
You don't have a condition for the Library.userid column:
Library.user_id =)
You need to specify a value on the right side of the equals sign!
your error is here:
Library.user_id =)
maybe you want to change it to:
Library.user_id ='$user_id')
It appears that some value for user_id is not being passed into the query:
`Library`.`user_id` =)
//-----------------^^^^
Likely, there is some PHP variable missing here, or a PDO parameter is not being correctly bound.
SELECT COUNT(*) AS `count` FROM `songs` AS `Song`
left JOIN libraries AS `Library`
ON (`Song`.`id` = `Library`.`song_id` AND `Library`.`user_id` =) <-- HERE's something missing
LEFT JOIN `blogs` AS `Blog` ON (`Song`.`blog_id` = `Blog`.`id`) WHERE 1 = 1
SELECT *,
Blog.id FROM `songs` AS `Song` left JOIN libraries AS `Library`
ON (`Song`.`id` = `Library`.`song_id` AND `Library`.`user_id` =) <-- HERE's something missing
WHERE 1 = 1 ORDER BY `Song`.`postdate` desc LIMIT 100
Since this is a CakePHP question, my answer will approach from the paradigms of the framework. However, given your SQL output, you are either NOT using CakePHP or you are doing something that is definitely a no-no.
Please post the Find statement that you are using the build this SQL statement and your Library's hasMany relationship to Blog. Unless you are using the query method, I think you would get a PHP error before you could build this type of SQL statement using the CakePHP find conventions. If you are using the query method to find your data, then don't. This is a simple query that can be done with CakePHP find easily.
Your library model should like like:
public $hasMany => array('Blog');
And the find:
$this->Library->find(
'all',
array(
'conditions' => array(
'1' => 1 //Not sure why you have Where 1=1
)
'order' => "Library.name DESC",
'limit' => 100
)
);

Categories