JOIN not working with PHP + ODBC + Microsoft Access - php

I am trying to write a PHP script that plugs into an existing Access Database. If I would be starting from scratch, I would have used MySQL for the job, but because there is an existing MS Access application I am stuck with the database as it is.
As of right now, I am trying to get the following PHP Code to work.
$conn=odbc_connect('buju','','');
if (!$conn)
{
exit("Connection Failed: " . $conn);
}
$sql="SELECT *
FROM Teilnehmer
INNER JOIN TeilnWerte ON Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
WHERE Teilnehmer.Klasse = '$_POST[klasse]'";
$rs=odbc_exec($conn,$sql);
echo "\nErrorCode:\n".odbc_error($conn);
echo "\nErrorMessage:\n".odbc_errormsg($conn);
I am pretty sure, that the problem is in the SQL Query, since it all works fine if I only do
SELECT * FROM Teilnehmer WHERE Klasse = '$_POST[klasse]'
without trying to join the second table.
I am using odbc and the Microsoft Access Driver. The Error Code that I get is 07001. The Error Message is
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
I have also tried
SELECT *
FROM Teilnehmer, TeilnWerte
WHERE Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
AND Teilnehmer.Klasse = '$_POST[klasse]'
which did not work either.
Is there anything that I am doing wrong? Are there certain SQL commands that don't work

Since echo $sql gives you ...
SELECT *
FROM
Teilnehmer
INNER JOIN TeilnWerte
ON Teilnehmer.LfdNr = TeilnWerte.Teilnehmer
WHERE Teilnehmer.Klasse = '06A'
Test that same statement as a new query in Access. Create a query in the query designer, switch to SQL View, paste in the statement and see what happens when you run it.
Most often the cause of "Too few parameters" is a misspelled item (object name, function or SQL keyword). Since the db engine can't find that item, it assumes the item is a parameter. Access will pop up a parameter dialog asking you to supply a parameter value, and that dialog also contains the parameter's name. So it tells you which is the misspelled item.

Related

PHP/OCI - Unable to get results from Oracle procedure within a temporary table

I am attempting to do two things using the PHP OCI Oracle functions:
Run a package procedure within an Oracle database.
Once the package has ran, query a temporary table to get the results of the procedure's operation.
I am able to do this successfully using the SQL Developer software provided from Oracle. My query is extremely basic and can been seen below:
BEGIN
PKG_KTY_SEARCH.PR_PRICE_LIST();
END;
/
SELECT * FROM kty_web.KTY_PROD_PRICE_TEMP;
This code above works perfectly and I get a full table of results in SQL Developer.
I am attempting to do the same thing above in PHP using OCI. My code can be seen below:
<?php
// Load up the system.
require('../../system/init.php');
global $config;
$oracleDb = oci_new_connect($config['oracleDb']['username'], $config['oracleDb']['password'], $config['oracleDb']['connectionString']);
$firstStid = oci_parse($oracleDb, "BEGIN PKG_KTY_SEARCH.PR_PRICE_LIST(); END;");
oci_execute($firstStid);
$secondStid = oci_parse($oracleDb, "SELECT * FROM kty_web.KTY_PROD_PRICE_TEMP");
oci_execute($secondStid);
oci_fetch_all($secondStid, $result);
echo json_encode($result);
echo "<br />Import complete!";
?>
This however returns no errors, and an empty result set. I can't figure out why. Anybody seeing anything obvious here that I'm missing?
Result set returned from PHP
{"PRODUCT_ID":[],"CUST_ROLE":[],"MIN_QTY":[],"MAX_QTY":[],"PRICE":[]}
My connection string is as follows:
$config['oracleDb']['connectionString'] = "(DESCRIPTION=(ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = " . $config['oracleDb']['host'] . ")(PORT = " . $config['oracleDb']['port'] . ")))(CONNECT_DATA=(SID=" . $config['oracleDb']['sid'] . ")))";
I am using PHP7.1.22, and Oracle 11g database. I am able to query normal tables and get results without problems within PHP and get a full result set.
Is the temporary table defined as on commit delete rows or as on commit preserve rows?
By default, oci_execute will implicitly issue a commit after every successful call. Assuming your temporary table is defined as on commit delete rows, that will delete the rows before the subsequent query. You can change that behavior by passing an optional second parameter
oci_execute($firstStid, OCI_DEFAULT);
Assuming you do this, however, you'll want to do an explicit oci_commit in order to close the transaction you've opened.

mssql_connect will not connect different DB on same SQL Server

$conn_161 = "192.168.0.161"; //local serwer adress
$user_161 = "ME";
$pass_161 = "what_is_the_password?";
$connect_161 = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'DUKENUKEN3D' , $connect_161 );
//as requested - 1st DB connection and 1st query
$q_duke = "select * from DUKE /*DB1*/";
$r_duke = mssql_query($q_wartownik,$connect_161); //result
$connect_different_db = mssql_connect($conn_161,$user_161,$pass_161);
mssql_select_db ( 'BIGMAN' , $connect_different_db );
//second db and query
$q_bigman = "select * from BIGPEOPLE /*DB2*/";
$r_bigman = mssql_query($q_bigman,$connect_different_db ); //result
Error:
Warning: mssql_query() [function.mssql-query]: message: Invalid object name 'DUKE'.
Yes I know mssql_select_db is old, but I need to use it in this project. As you see I try to connect to same server but select 2 different DB at the same time, and run queries in the same php page (to connect data from 2 different DB). It seems it is not valiable? I even tried to run mssql_select_db just before doing the query to second DB, and then changing it back to first DB.
I understand this is limitation of the library (I will run all queries from LAST selected DB).
Is there a workaroud? Because all I got is to create page inside invisible iframe and there run php page with different db connection - far from good solution.
I would expect that this will work the same as it would if you were running this in a SQL environment directly (e.g. you can try it in SSMS or from the command line).
You can specify the database name when you reference the table in the query: e.g.
select * from db1.dbo.DUKE
This is standard SQL Server behaviour whenever you want to refer to an object which is outside the context of the current database.

Different SQL result with same query in PHP and MySQL

After a quick search, I haven't find solution of my problem so I post a new one.
So I have to create view in a MySQL database from a Web interface (using PHP).
I use a PEAR framework for the connection with MySQL(5.0.26)
I have the SQL request :
CREATE VIEW test AS SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');
When I execute this request directly on mon local MySQL Database, I obtain a result with 470 lines.
However, when I execute this request in my PHP code, I have a different result (I have 386 line), and I don't know why !
$values['request'] = "SELECT cswc.code_de_la_copie, cswc.service_de_reference, cswc.libelle_de_la_copie, cswc.direction_de_securite_logique
FROM pressi_copiesServiceWithCibles cswc LEFT OUTER JOIN pressi_servicesReferenceWithCibles srwc ON cswc.service_de_reference = srwc.code_du_service
WHERE cswc.cible is null
AND (srwc.cible LIKE '%£DOMAIN£%' OR srwc.cible LIKE '%$DOMAIN$%');";
$baseView = "test";
$sqlView = 'CREATE VIEW '.$baseView.' AS '.$values['request'];
$res =& $this->mdb2->query($sqlView);
if (PEAR::isError($res)) {
return false;
}
Moreover, I have already create 6 views before this one without any problem (same result in PHP and in MySQL)
Thank you for your help
Note that your connection to the database also has a CHARSET and a COLLATION for any string values you include in your query. Although your query looks the same to you in both situations, it must not from the MySQL servers point of view.
Maybe the client CHARSET (and/or COLLATION) differ when you connect via PHP from when you connect via MySQL console.
See the MySQL manual for more information on the client charset and collation.
For comparison, you can use this query:
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

Slow query on remote database

I have:
A linux server with PHP 5.3.2 and Apache 2.0
A windows Server with SQL SERVER
I connect my linux server with database with unixODBC 2.3.1 and FREETDS 0.9.1
The connection is okay but the queries are slow.
Here is my code that generate the image below:
$this->adodb->LogSQL(true); // turn on logging
$query = "select c.name, t.name, c.length from syscolumns c join systypes t on t.xusertype = c.xusertype
join sysobjects o on o.id=c.id where o.name = 'CONDOMINIO'";
$res = $this->adodb->Execute($query);
$this->adodb->LogSQL(false); // turn off logging
$perf = NewPerfMonitor($this->adodb);
echo $perf->SuspiciousSQL();
echo $perf->ExpensiveSQL();
Image http://www.vigoonline.net/slow.png
As you can see, the first query has an average time of 4.68 seconds, which is way too slow.
If I execute the same query like this:
$this->adodb->_query($query);
Then the time to execute the query takes less than a second what is great. Has anyone else experienced the same thing?
The query called with "Execute" is used for the own class for bringing the "MetaColumns" information from the table
The table "CONDOMINIO" only has 21 rows.
If the server database is in the same machine with the application script, then the response is fast!
I resolved this myself.
Here is what I did:
Go to the file /adodb/drivers/adodb-odbc.inc.php and to the class ADODB_odbc
Here try to find:
var $curmode = SQL_CUR_USE_DRIVER
and change it to:
var $curmode = SQL_CUR_USE_IF_NEEDED;
This will make the connection use SQL_CUR_USE_ODBC when needed and "Voila" DB access speed to SQL server is really fast!

Can't access the SQLite database with MAMP and PHP

I have been learning how to program websites lately and the time has come for me to add a database. I have in fact already successfully created a MySQL database and interacted with it with PHP.
My problem is I can't seem to access a SQLite database file with it. I am using MAMP to host locally for now. Here is a snippet of the code I am using to access the db and find and print out a value stored on it.
<?php
$dbhandle = sqlite_open('/Applications/MAMP/db/sqlite/Users');
if ($dbhandle == false) die ('Unable to open database');
$dbquery = "SELECT * FROM usernames WHERE username=trevor";
$dbresult = sqlite_query($dbhandle, $dbquery);
echo sqlite_fetch_single($dbresult);
sqlite_close($dbhandle);
?>
As you have access to the database (your code doesn't die), I'd say there's got to be an error later ;-)
Looking at your SQL query, I see this :
SELECT * FROM usernames WHERE username=trevor
Are you sure you don't need to put quotes arround that string ?
Like this :
SELECT * FROM usernames WHERE username='trevor'
Also, notice that sqlite_fetch_single will only fetch the first row of your data -- which means you might need to use sqlite_fetch_array or sqlite_fetch_object if you want to access all the fields of your resulset.

Categories