Different SQL result with same query in PHP and MySQL - php

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%';

Related

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.

How do I get database information after running pg_connect?

I have some code that runs a function and within it executes pg_connect.
$db = pg_connect("$dsn");
Is it possible to retrieve the database information from the $db variable, specifically the database name? If I run a var_dump on $db I get:
resource(18) of type (pgsql link)
Another approach is to use the PHP function that was specifically developed for this purpose:
$db_name = pg_dbname($db);
http://php.net/manual/en/function.pg-dbname.php
This saves you the SELECT.
You can simply run a command on the database you've connected to that asks for its name; as documented in the Postgres manual, the relevant query would be:
SELECT current_database() as database_name;
(I will assume you know how to run an SQL query using your $db variable, so won't bother with PHP samples.)

Detecting type of database connection in drupal

How can I detect what type of database I'm connecting to in drupal (using php code)? I am trying to write a module which exposes some database functionality which only works on postgres or sql server. Currently, I'm doing it by trying to detect the database version, since the syntax appears to be different for each database but it doesn't seem very robust. Is there a php function which will report this?
You should use the global variable: $databases and check the driver value.
global $databases;
dpm($databases);
https://drupal.stackexchange.com/questions/48882/how-to-get-database-credentials
I don't fully understand what you're trying at the moment but
db_driver()
returns he currently connected database in Drupal as a string (e.g. "pgsql").
This may be helpfull to you. Use db_set_active() API to set your database before executing the query.
This will help you to keep away from errors
<?php
//set your configurations
$db_url['default'] = 'mysql://drupal:drupal#localhost/drupal';
$db_url['mydb'] = 'mysql://user:pwd#localhost/anotherdb';
$db_url['db3'] = 'mysql://user:pwd#localhost/yetanotherdb';
//set the active database and then process your queries in this case you can always knows which database is connected now.
db_set_active('mydb');
db_query('SELECT * FROM table_in_anotherdb');
Setting multiple databases
<?php
// ... header of the settings.php file
$db_url = array (
"default" => "mysql://user:pass#host/db",
"second" => "pgsql://user:pass#host/db"
);
db_set_active('second');
Ref: https://drupal.org/node/18429

JOIN not working with PHP + ODBC + Microsoft Access

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.

How do I detect database name using MySQL PDO

I am rather new to the PDO library, so I apologize for my inexperience. I am writing a class that uses the PDO library to build and execute queries and return the results, no matter what they are.
Within the class, I detect whether there is an open connection to a database, and if it is the same as the one being configured, it uses this one instead. This is really easy to do using the MsSQL library as the PDO::getAttribute() function returns 'CurrentDatabase' and 'SQLServerName', so I can just apply a condition like so:
if(!empty($this->PDO)){
// Get the current connection information
$current_connection = $this->PDO->getAttribute(PDO::ATTR_SERVER_INFO);
// Return if the connection is the same
if($this->connection_parameters['hostname']==$current_connection['SQLServerName']&&$this->connection_parameters['database']==$current_connection['CurrentDatabase']){
return;
}
}
However, when it comes to MySQL, the data returned from PDO::getAttribute is completely different and I cannot seem to get the database name from the current connection.
Does any body know a function or method to get the currently connected database of a MySQL connection using the PDO library in PHP?
I order to connect to both MySQL and MsSQL, you must have 2 connections. However, changing the database on a live connection is very simple.
The following simply checks if a PDO instance already exists and whether or not it is using the required database. If so then it continues with this connection, if not it changes the database.
// Test if the PDO object already exists
if(!empty($this->PDO)){
// If connection is the same then select the database
if($this->connection_engine==$this->PDO->getAttribute(PDO::ATTR_DRIVER_NAME)){
// Get the current database in use
$database = $this->PDO->query("SELECT {$this->select_db_function}");
$database = $database->fetchAll(PDO::FETCH_NUM);
$database = $database[0][0];
// If the current database matches the new database then return
if($database==$this->connection_parameters['database']){
return;
}
}
}
I see no point in looking for the opened connection and - especially - in checking for the current database.
Why can't you just open the connection, select the database for it and then use this connection all the time throughout your class - just like everyone does?
See comments on the MySQL manual page for 'USE database'

Categories