Attempting to make a prepared statement with ODBTP (Linux -> MSSQL) - sql-server-2008

I am attempting to connect to my SQL SERVER 2008 database using Ubuntu Linux and ODBTP to run a prepared statement and insert a new row into the database.
The query looks like the following
$Q = "INSERT INTO Table
([DocumentGuid]
,[ProjectId]
,[CreatedByContactId]
,[IdNumber]
,[Type])
VALUES
(:DocumentGuid
,:ProjectId
,:CreatedByContactId
,:IdNumber
,:Type )";
$statement = odbtp_prepare($Q , $database_connection);
// Attach the values to the procedure.
odbtp_attach_param($statement,":DocumentGuid",$DocumentGuid,ODB_CHAR);
odbtp_attach_param($statement,":ProjectId","33",ODB_INT);
odbtp_attach_param($statement,":CreatedByContactId","5",ODB_INT);
odbtp_attach_param($statement,":IdNumber","ID",ODB_CHAR);
odbtp_attach_param($statement,":Type","ABC",ODB_CHAR);
odbtp_execute($statement);
But I am getting the following error message in the log
"PHP Warning: [ODBTPERR][0]Not prepared procedure in (filename) on line 14"... Line 14 being the first odbtp_attach_param() line.
I have only ever made prepared statements using PDO and I am probably doing something wrong.
does anyone have an example prepared statement created using ODBTP they could show me?

Related

restore mysql backup file using php

I can get backup file for mysql database using php and every thing gone nice , but when I trying to restore this backup file I have mysqli error
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 'INSERT INTO radpostauth VALUES ('289','ah','ah','Access-Accept','2017-08-14 10:' at line 7
in other hand when I take the query which is writed into the file and past it in php my admin directly it executed without any error I am sure that the query is right
I have 3 empty line between every insert query when I run the file the first insert query is executed but the error is show when want to execute the second insert query
INSERT INTO radcheck VALUES ('1229','ah','Cleartext-Password',':=','ah','','','','','','','','');
INSERT INTO radpostauth VALUES ('289','ah','ah','Access-Accept','2017-08-14 10:04:22'), ('290','muh','asdfghjkl','Access-Accept','2017-08-14 10:05:30'), ('291','sda','sad','Access-Accept','2017-08-14 11:21:48'), ('292','sda','sad','Access-Accept','2017-08-14 11:55:40'), ('293','sda','sad','Access-Accept','2017-08-14 11:59:03'), ('294','sda','sad','Access-Accept','2017-08-14 12:52:58'), ('295','sda','sad','Access-Accept','2017-08-14 13:41:41'), ('296','sda','sad','Access-Accept','2017-08-14 14:50:40'), ('297','sda','sad','Access-Accept','2017-08-15 09:45:40'), ('298','ah','ah','Access-Accept','2017-08-16 12:52:09'), ('299','ah','ah','Access-Accept','2017-08-17 09:19:53');
why I have this wrong
Try using mysqli::multi_query() instead of mysqli::query(). It allows the execution of multiple queries at once.

is there a Syntax like MSSQL's "INSERT INTO table (fieldlist) OUTPUT INSERTED.id VALUES" in MySQL?

I like to get the id of the record that's been inserted by a statement as a result of that statement. I know about SELECT LAST_INSERT_ID(); but can not use it in my scenario because LAST_INSERT_ID() works on a per-connection-basis and I am currently using a convenient function in my framework to access the db - but that convenience implies that the connection only exists during execution of that statement - so I'm wondering if there is a syntax as in Microsoft SQL Server as shown in the title?
In my environment, I am building the command looks like this:
INSERT INTO Bookings (Status,YEarlyBird,YDays,YHotel,YBanquet,Name,Company,Address,Town,Region,Postcode,CountryCode,Second
Name,SecondEmail,RoomType,SpouseMealPlan,Notes,Conference_VAT,Accommodation_VAT,Conference_id,InvoiceAmount,Conferen
ceGross,Accomodation,SpouseGross,SpouseNet,TotalGross,TotalNet,ConferenceNet,Courses) VALUES (:<C1:,:<C1:,:<C6:,:<C
6:,:<C1:,:<C12:,:<C28:,:<C15:,:<C7:,:<C5:,:<C5:,:<C2:,:<C1:,:<C1:,:<C15:,:<C3:,:<C1:,:<F:,:<F:,:<I:,:<F:,:<F:,:<F:,:
<F:,:<F:,:<F:,:<F:,:<F:,:<C1:);SELECT LAST_INSERT_ID();
(the :<:-stuff are placeholders for Bind-variables)
If a ran the INSERT on its own, it works. If I append the SELECT LAST_INSERT_ID() as shown, I get:
[MySQL][ODBC 5.3(w) Driver][mysqld-5.7.18-log]You have an error in your SQL syntax; check the manual that corresponds to y
our MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID()' at line 1
P.S: using that convient way to access the DB is perfectly ok in my use-case because the DB is written once only - exactly with the statement i am working on.

Sql Server sp_send_dbmail linked mysql server error

execute msdb.dbo.sp_send_dbmail
etc etc
#query = 'select * from Mysql...MemberRef4 as oi where oi.MemberId = 133363 '
The above is a query that I plugged into an existing working send mail method. MySql is a linked MySql database. I CAN run this query without any errors, correctly, by selecting the query code and F5. I am running this code and other MySql code joins with SqlServer and other linked servers - without any errors, including views/procs/etc.
However, the sp_send_dbmail method fails with :
Msg 22050, Level 16, State 1, Line 0
Failed to initialize sqlcmd library with error number -2147467259.
Note: this is the first time I'm trying to put MySql queries in sp_send_dbmail
Any help please. I really don't want stored procs that create temp tables/etc :(

Mysql function return system result

In mysql, I'm trying to create a function that will return the result of a bash command. I've created a simplified version of what I'm trying to do to make it easier to understand.
I have a file called mycmd.sh, and inside it, I have this line:
echo $1 'test'
When in mysql, I execute the sh file it is working fine
system ~/mycmd.sh 'test'
But I would like to be able to call it while doing a select in mysql, something like this
select myFunc(username) from user;
The result will be that, every username will have a "test" after it.
The problem is when I try to create the mysql function
CREATE FUNCTION myFunc(s VARCHAR(500)) RETURNS VARCHAR(505)
RETURN SYSTEM ~/mycmd.sh s;
I always get the error: ERROR 1064 (42000): 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 '~/mycmd.sh s' at line 2
I tried putting it in quote, or in a variable, or in a prepared statement, but nothing seem to work...
Anyone have an idea how to do so?
Thank you
Unfortunately what you are trying to do isn't possible without using a UDF (User Defined Plugin). The system command you're using is part of the MySQL shell, not an actual MySQL function, which is why it doesn't work in the function you're trying to define.
The only other option I can think of is to write a procedure that imitated whatever your external command was going to do.

how to insert into more than one mysql table in single query?

I have this query which on executing in my sql command line client executes fine and get entry in both tables but executing it gives me error.
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the man
ual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO tm_belonging(B
ID,BELONGING_TYPE,BELONGING_TEXT)VALUES(LAST_INSERT_ID' at line 1
String sql="INSERT INTO tm_visitor(VISITOR_TEXT,COMPANY_TEXT,CONTACT,PERSON_TO_MEET,DEPARTMENT_TEXT,FLOOR)Values(?,?,?,?,?,?);"+"INSERT INTO tm_belonging(BID,BELONGING_TYPE,BELONGING_TEXT)VALUES(LAST_INSERT_ID(),?,?);";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setString(1,VisitorName.toUpperCase());
pst.setString(2,Company.toUpperCase());
pst.setString(3,Contact);
pst.setString(4,WhomeToMeet.toUpperCase());
pst.setString(5,Department);
pst.setString(6,DepartmentFloor);
pst.setString(7,BType);
pst.setString(8,Belonging);
pst.executeUpdate();
pst.close();
mmm try this:
String sql="INSERT INTO tm_visitor(VISITOR_TEXT,COMPANY_TEXT,CONTACT,PERSON_TO_MEET,DEPARTMENT_TEXT,FLOOR)Values(?,?,?,?,?,?);"+"set #lastid=LAST_INSERT_ID();"+"INSERT INTO tm_belonging(BID,BELONGING_TYPE,BELONGING_TEXT)VALUES(#lastid,?,?);";
if this dont work you must use one prepared statement for query
I dont think you can. You have to use two prepared statements for two insert queries. Normally i create store procedure if I have to deal with more than one table in one transaction and use callable statement.