Access SQL, append query breaks when using ODBC/PHP - php

I'm designing a web interface for my clients database (A .mdb MS Access file). I'm using an ODBC driver to connect to it and the odbc_ functions provided by PHP.
My problem is access's 'append' queries. From what I gather, it's just inserting more rows, but something is breaking the query from executing:
INSERT INTO test ( TITLE, [LEVEL], UNITID, TITLEM, COHORTPLUSOPTIONS )
SELECT \"OPTION ONLY\" AS Expr, Units.LEVEL, UnitOptionNumbers.ID, Units.TITLE,
UnitOptionNumbers.OPTIONCOHORT
FROM UnitOptionNumbers INNER JOIN Units ON UnitOptionNumbers.ID = Units.ID WHERE
(((UnitOptionNumbers.NOAWARD)=Yes));
The most helpful error message I can get is:
[ODBC Microsoft Access Driver] Too few parameters. Expected 1.
Which isn't helpful at all. I'm confident with mySQL, but I just cannot pinpoint the problem here. Please can you help me find the reason the query wont execute, or help me figure out a work around.
Thanks for your time.

I don't have enough reputation to comment but perhaps it could be a problem with the fact that your table "test" has two fields with the same name ("TITLE")

According to Microsoft:
"This error occurs only with Microsoft Access when one of the column names specified in a select statement does not exist in the table being queried."
The solution therefore is to change
SELECT \"OPTION ONLY\" AS Expr
to
SELECT 'OPTION ONLY'
It seems the original code attempted to fill the first field with a default text value I.e "OPTION ONLY". "OPTION ONLY" was being read as a column name it seems.

Related

Mysqli Query Result Different When Called from PHP than When Called from Mysql Shell

I have searched for answers on this, but can't find anything online.
I am having trouble with a query which gives the correct result set when run from the mysql shell on the computer, but a different one when run from mysqli in the PHP script.
The query is:
SELECT q.pos, q.event_ID, e.name FROM (SELECT #row_number:=CASE WHEN #event_ID=e_ID THEN #row_number+1 ELSE 1 END AS pos, #event_ID:=e_ID as event_ID, u_ID from event_queue ORDER BY e_ID, dateadded) AS q INNER JOIN event AS e ON q.event_ID = ID WHERE q.u_ID=11;
If this is called from mysqli then I get the incorrect position value (q.pos) of 1, if run from the mysql shell then I get the correct value of 4. I think it is because it is using a variable in the mysql code, but I can't find anything online which describes this issue.
Can someone point me in the right direction?
Thanks.
The solution that worked for me was just to remove the semi colon from the end of the first SQL query.
I had:
mysqli_query($dbcon,"SET #row_number=0;");
I changed it to:
mysqli_query($dbcon,"SET #row_number=0");
and it worked. I also needed to set the #event_ID variable in another mysqli query.
Thanks.

Invalid Oracle table name: User, using Laravel

I am trying to migrate one of my Laravel project to Oracle Database (11g). I installed this package to communicate with the database. Everything seems to working If I write queries, but the automatic query builder makes invalid queries. Like:
The registration uses this validation to post:
$validator = Validator::make(Input::all(),array(
'createInputEmail' => 'required|email|unique:User,email|max:254',
'createInputDisplayName' => 'max:24|min:4|alpha_num',
'createInputPassword1' => 'required|max:60',
'createInputPassword2' => 'required|same:createInputPassword1'
));
The problem here is the unique check. I get this error:
oci_execute(): ORA-00903: invalid table name (SQL: select count(*) as aggregate from User where email = asd#asd.sd)
If I copy this query to SQL Developer to test and apply quotes to the table name it works.
So a correct query:
select count(*) as aggregate from "User" where email = asd#asd.sd;
//dont worry about the unquoted email address
I can't put quotes to the rules when I am defining the validator, because it gets escaped.
I tried:
'required|email|unique:\'User\',email|max:254',
'required|email|unique:\"User\",email|max:254',
'required|email|unique:"User",email|max:254',
'required|email|unique:""User"",email|max:254', <-- the first 2 got escaped, last 2 didn't
'required|email|unique:'User',email|max:254',
That's an interesting case. For most Oracle Developer there's a standard to make all table name plurals (users vs user). That would help you to avoid this issue.
Basically, your problem is not just the quotes, you are using an Oracle Reserverd Words: User. More information about Oracle Reserverd Words here: http://docs.oracle.com/cd/B19306_01/em.102/b40103/app_oracle_reserved_words.htm
If you are the responsible to design the database do the following ASAP:
Avoid using Oracle Reserved Words as table name.
Keep table name as plural, to avoid that particular case.
If the database cannot be altered, you will have to keep looking for a workaround to this issue.
I have a workaround in mind that you need to test it, edit your model User.php and change the table name to include the quotes.
protected $name = '"User"';
I have tested and the quotes appeared in the generated SQL query but it fails to execute because I'm using MySQL and all table name are wrapped with `.

Stumped on why MySQL won't accept my query?

Okay, so I'm currently using mysqli_real_escape_string to escape my SQL queries before sending them to MySQL via PHP. Yet, for some reason my queries aren't processing, and when I outputted the MySQL query and pasted it in to PHPMyAdmin, it gave the following 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 ind={A$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQg' at line 1
Now, the following is my query:
INSERT INTO `db`.table(`colheader`) VALUES ('{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}') WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'
Now, I know that the string assigned to 'ind' has some issues, but I tried putting a slash before every period and every dollar sign and it still doesn't work. I tried putting the whole thing in double quotes, even brackets. Nothing. Could anyone point out what I'm clearly missing? I've looked at the documentation and can't seem to find anything. Thank you in advance!!
WHERE serves to filter which records will be affected or retrieved by your query, and INSERT servers to append a whole new record to a table.
An INSERT can never affect existing records, therefore its nonsense to have a WHERE clause. INSERT does not support WHERE.
If you are trying to edit the value of a field on an existing record, use UPDATE instead.
Take a look at the MySQL Reference Manual for details about its usage.
if your trying to make an update to the specified index use
UPDATE `db`.table SET `colheader` = '{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}' WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'

PDO column doesn't exist when it does?

I have recently uploaded my site to my hosting provider and I am getting a very odd error.
I have imported the exact same database as I was using on my local machine to the web host and it is telling me:
Warning: PDOStatement::execute(): SQLSTATE[42S22]: Column not found:
1054 Unknown column 'toppers_types.urlPath' in 'field list' in
/home/users/xxxxx/html/caketopper.co.uk/public_html/models/GalleryModel.php
on line 32
The weird thing is, is that the column does exist and I am 100% I am connecting to the correct database.
Here is the SQL concerned:
SELECT toppers.name, toppers.urlName, md.description, toppers_images.thumbSrc,
toppers_types.urlPath
FROM toppers_images, toppers_types, toppers
LEFT JOIN meta_descriptions AS md ON md.topperId = toppers.id
WHERE toppers_images.topperId = toppers.id
AND toppers_types.id = toppers.typeId
AND isPrimary = 1
If I take that SQL and put it into phpmyadmin on the webhost, the query runs as expected and I get results.
I am executing the query using PDO:
$r = $this->db->prepare($SQL) ;
$r->execute($PDOParams) ;
return $r->fetchAll(PDO::FETCH_OBJ) ;
Has anyone come across such an anomaly like this before? and if so how do I fix it?
There are plenty of the questions of this kind on Stackoverflow and almost all of them ended the same way:
- Oh, I forgot to update the actual database (rename the field, save the proper file etc.)
There is also possible issue with letter case (Unix version is case sensitive while Windows one is not).
Anyway - just test everything.
Run this query using PDO and see the results.
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'toppers_types'
if field is absent - check server credentials and such
if present - copy-paste it's name to the query

mysql template in php

I come across this sql code in a php software.
What does the #section_filter mean?
Is that a valid mysql syntax? or just a templating system?
$filterid = ac_sql_select_one("
SELECT
id
FROM
#section_filter
WHERE
userid = '$ary[userid]'
AND
sectionid = 'article'
AND
conds = '$conds_esc'
");
Thanks
It is a valid sql syntax but the problem i suspect is that hash # character creates comments in sql queries hence this query might not execute.
Another possiblity is that the program that you saw this query in should be able to dynamically replace the #section_filter with some table name before it gets to mysql engine and then the query should run fine. This is the highest possibility in my view.
The # symbol is one way to express a comment in MySQL. So, this isn't a valid SQL statement as written, since the line:
#section_filter
would be completely ignored.

Categories