SQL Server and tables? Invalid object - php

Firstly I'm a complete newbie when it comes to SQL Server. I have five SQL Server databases, in all of them except one I can easily do say...
SELECT TOP 10 * FROM dt_Organizers
However one of the new databases require me to do this:
SELECT TOP 10 * FROM dbuser.dt_Organizers
Note that all the tables in all databases have "dbuser" "dbamy" "dbyon" "dbmio" "dbcana" prefixes respectively, this never caused a problem before. Now if I use the query without doing dbuser.dt_Organizers it brings up:
Warning: mssql_query()
[function.mssql-query]: message:
Invalid object name 'dt_Organizers'.

I think you should have a look at
Understanding the Difference between
Owners and Schemas in SQL Server
SQL SERVER – Importance of Database
Schemas in SQL Server

Related

How can I fix this syntax error on my PHP MySQL query

I have been trying to make a query in MYSQL, I am doing 2 things in 1 query. My host has a Limit to queries so I need to limit the amount of queries I am doing. When I did the query I get
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 ', (UPDATE users SET last_online=NOW() WHERE
username='Welcome_234')' at line 1
I have put a semicolon ";" but it still doesn't work (This query worked in phpMyAdmin).
The code for the query is:
SELECT * FROM `users` WHERE username='Welcome_234',
UPDATE `users` SET last_online=NOW() WHERE username='Welcome_234'
I have tried many hours to figure out the problem and I still haven't found it.
Most PHP interfaces to run MySQL queries don't support multiple queries per call. You must run them one query per call.
Your host's limit on queries can't be so low that the difference between one and two queries will break it. I would guess the limit is in place to prevent clients from running excessive loops that run thousands of queries. If it is so low that you can't run two queries, then get a different host.

How to Connect to Sage 50 ODBC Database with PHP

I'm attempting to export data from Sage 50 database. I have managed to create a connection to the database however whenever I perform a query I have the following error message.
Warning: odbc_exec(): SQLColAttribute can't handle SQL_DESC_OCTET_LENGTH: [S1C00] Driver not capable in C:... on line 4
I have Attempted to change the cursor to SQL_CUR_USE_CODE as suggested on PHP.net
I am also sure it is connecting as when I enter a table name that doesn't exist then I am receiving this error.
Warning: odbc_exec(): SQL error: Table not found, SQL state S0002 in SQLExecDirect in C:... on line 4
$conn = odbc_connect("TestDB", "Manager", "", SQL_CUR_USE_ODBC);
$sql="SELECT * FROM STOCK";
$result=odbc_exec($conn,$sql);
Thanks in advance
I was having a similar issue with a c# application, but my solution may have some pertinence here and I hate to see someone go through the stress that I did. Here's what I did to fix my issue:
In the ODBC Data Source Administrator, select your Pervasive ODBC Client Interface
Under Data Options select a database name associated with the data source of your CrystalReports.udl file. Make sure the Dictionary location is the Data directory for your company.
Make sure your select statement queries a table that are allowed by the crystal report e.g. Address, Audittr, etc. since not all tables can be queried through the Crystal reports.
Hope that helps!

Migrating from MySQL to MS SQL

I have a PHP (CODEIGNITER) application which is generally deployed on Apache/MySQL combination. I recently deployed it on IIS8 and MS SQL 11.0.2100.60
I migrated the tables and data by using ODBC connection to migrate to Access database and then again another ODBC connection to migrate to MS SQL. I modified the configurations of my PHP application (PHP.ini, database.php, db_driver.php) to make sure it connects properly and works on IIS.
I am having problem with SQL Syntax now. When I try to run the application it does not give me database connection error (which it was giving earlier) but when I try to log in to the application (it has user authentication) - I get the following error:
Error Number: 42000
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near '`'.
SELECT * from ctbl_events WHERE 2017-01-11 <= startdate AND `enddate` >= 2017-01-11 and status=0 ORDER BY `ctbl_events`.`id`
Filename: C:\inetpub\wwwroot\GMS\system\database\DB_driver.php
Line Number: 330
does this mean problem is with ` symbol and if yes would I have to manually go and modify all the SQL queries in my application (would be a gigantic task) or is there any way to handle this.
As far as I know, SQL Server does not support using backticks to escape table or column names (you can use brackets instead). You can easily try this by running a simple query like
select * from `ctbl_events`
If that doesn't work, you will almost certainly have to update all the queries to replace the backticks with brackets.
Second reason the query is probably failing is the dates in your query must be enclosed in quotes, and match SQL Server's date format (this is configurable, so you may need to experiment a little).
So, the query you're trying to run should look a little like this:
SELECT * from ctbl_events
WHERE '2017-01-11' <= startdate
AND [enddate] >= '2017-01-11'
and status=0
ORDER BY [ctbl_events].[id]
Though in this case, you don't really need the brackets around table or column names - it's best to agree a standard for this and stick to it.

Access SQL, append query breaks when using ODBC/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.

SQL Syntax error, when executing several queries?

I am using a readymade script to backup my MySQL database using PHP. I store the resultant query in a variable.
If I echo the variable, and copy paste the output into the MySQL console, it works perfectly.
But when I run the same using 'mysql_query' (I know it is depreciated, kindly ignore that), I get the dreaded Syntax error.
Here's the echo output (first 2 lines) :
INSERT INTO assign
VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1','3.0','13','1','1','13','2','10.00','117.00','0','0');INSERT
INTO assign
VALUES('75086','rsam','CE0001/CZ0001/CPE183/CSC183','1','2','3.0','13','1','1','13','2','10.00','97.50','0','0');
And here's the exact 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 assign
VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1'' at line 1
If anyone can point out what I am obviously missing, I would be grateful!
As the documentation for mysql_query() says:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
You might be interested in mysql_multi_query():
Executes one or multiple queries which are concatenated by a semicolon.
While mysql_query is limited to a single statement, this situation can be avoided as multiple records can be inserted into the same table with only one statement:
INSERT INTO assign (...)
VALUES(...),
VALUES(...);
This will save on round-trip latency (over multiple mysql_query) which might matter.
See Inserting multiple rows in mysql

Categories