Query with database link server with pdo in php - php

I'm facing a problem right now, i explain :
I have my SQL query :
SELECT COUNT(*) AS nb, TO_CHAR(myDate,'yyyy-mm-dd') AS dateF, T1.IDCar, T2.IDMotor
FROM TDB2#MYLINKSERVER,T1
JOIN T2 ON T1.ID = T2.ID
WHERE TDB2.IDCar = T1.IDCar
Which use a TDB2#MYLINKSERVER , to connect 2 differents databases from Oracle and use a table from and other database.
In fact, when i launch the query in sql developper, my query returns some datas.
So in php, i have
$sql = $pdo->prepare('myquerybefore')
$sql->execute(array($annee));
$res = $sql->fetchAll(PDO::FETCH_ASSOC);
And when i try to vardump my $res, it returns array(0) { }
So i think that PDO don't use the #LINKSERVER to fetch results.
Any help would be appreciated
Sleakerz,

I found a way to make my query work :
I create a synonym with
CREATE PUBLIC SYNONYM LINKDATABASE
FOR TABLE1#THELINKDATABASE;
Then i have in my query: FROM LINKDATABASE which is my synonym !

Related

Execute multiple queries in PHP

I'm trying to join two views which I create in a query.
The query works fine in MySQL but when I execute it with PHP it looks like it's not actually executing because I can't visualize the two views in the db.
The query I'm executing is the following:
$query1 = "CREATE OR REPLACE VIEW new_tbl AS SELECT
AnagTav.Id,
AnagTav.Riga,
AnagTav.Colonna,
AnagTav.Costo,
AnagTav.Nome,
AnagTav.NumeroPersone,
PrenPic.Data AS Occupato,
PrenPic.IdUtente
FROM `AnagraficaTavoli` AS AnagTav
LEFT JOIN PrenotazioniPicNic AS PrenPic ON PrenPic.IdTavolo = AnagTav.Id
WHERE (PrenPic.Data >= '".$dateFrom."' AND PrenPic.Data <= '".$dateTo."')
OR PrenPic.Data IS NULL
GROUP BY CASE
WHEN AnagTav.Nome != '' THEN AnagTav.Nome
ELSE AnagTav.Id
END
ORDER BY AnagTav.Riga ASC, AnagTav.Colonna;
CREATE OR REPLACE VIEW new_tbl2 AS SELECT
AnagraficaTavoli.*,
Occupato,
IdUtente
FROM AnagraficaTavoli
LEFT JOIN new_tbl ON (AnagraficaTavoli.Id = new_tbl.Id)
WHERE new_tbl.Id IS NULL;";
$result=$conn->query($query1);
After creating the 2 views I execute a second query:
if($result){
$query2 = "SELECT * FROM new_tbl
UNION
SELECT * FROM new_tbl2
ORDER BY Riga ASC, Colonna;";
$resultList=$conn->query($query2);
while($rowList=$resultList->fetch_assoc())
{
//I do stuff here
}
}
$conn is my connection to the db
I tried printing the query and executing it on MySQL, but it works fine.
I don't know why I'm getting this error and I don't know how to solve it.
This might be non obvious, but if you keep your script connected, you can access you temporary tables in separate queries. What you should do is to slice your query into three different operations, especially the "select" being a separate one. Only having single select statement in a query, php will be able to properly fetch resultset.

Running two SQL queries in PHP where the first statement creates a temporary table to be used by the second one

I want to run this long SQL query in PHP.
CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty
FROM warehouse
WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise)
GROUP BY warehouse.merchandise_id
);
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty
FROM `merchandise` INNER JOIN info
ON merchandise.id = merchandise_count.merchandise_id;
Here's a quick explanation of what it does: First it creates a temporary table to store some selected data, then it uses the temporary table to INNER JOIN it with data in a permanent table.
I have already tried '$statement1;$statement2;' in PHP, but it gives syntax and access violation error but the given query works flawlessly in phpmyadmin.
I checked other similar posts like this and they suggest to use '$statement1;$statement2;' but it doesn't work for me. My server is running PHP 7. I'm using PHP PDO to connect to my database. Any help is appreciated.
I ran the following and it did work.
$stmt = $pdo->query("
CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty
FROM warehouse
WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise)
GROUP BY warehouse.merchandise_id
);
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty
FROM `merchandise` INNER JOIN info
ON merchandise.id = info.merchandise_id;
");
// skip to next rowset, because it's a fatal error to fetch from a statement that has no result
$stmt->nextRowset();
do {
$rowset = $stmt->fetchAll(PDO::FETCH_NUM);
if ($rowset) {
print_r($rowset);
}
} while ($stmt->nextRowset());
Notice I had to fix merchandise_count.merchandise_id to info.merchandise_id in your query, because you have no table reference to merchandise_count.
However, I would recommend you do not bother with multi-query. There's no benefit from concatenating multiple SQL statements in a single call. It's also not supported to use prepared statements when using multi-query, or to define stored routines like procedures, functions, or triggers.
Instead, execute the statements one at a time. Use exec() if the statement has no result set and doesn't need to be prepared statements.
$pdo->exec("
CREATE TEMPORARY TABLE IF NOT EXISTS info AS (
SELECT warehouse.merchandise_id, SUM(warehouse.prod_quantity) AS qty
FROM warehouse
WHERE warehouse.merchandise_id IN (SELECT merchandise.id FROM merchandise)
GROUP BY warehouse.merchandise_id
)");
$stmt = $pdo->query("
SELECT LPAD(`id`,8,'0'), prod_title, prod_lcode, prod_price, qty
FROM `merchandise` INNER JOIN info
ON merchandise.id = info.merchandise_id
");
$rowset = $stmt->fetchAll(PDO::FETCH_NUM);
if ($rowset) {
print_r($rowset);
}
As long as you use the same $pdo connection, you can reference temporary tables in subsequent queries.

Running multiple MySQL queries with user defined variable through PHP

I'm trying to run a combination of queries from PHP. Something like this:
SELECT #rownum:=0; INSERT INTO tbl1 (`myId`, `rank`) (SELECT myId, #rownum:=#rownum+1 FROM `tbl2` WHERE 1 ORDER BY rank DESC)
Because of the user-defined var (#rownum) I have to run these queries "together". I've tried doing it either with mysqli or PDO, but both seemed to block this.
The mysqli usage I tried was:
public function multiQuery($query) {
if (#parent::multi_query($query)) {
$i = 0;
do {
$i++;
} while (#parent::next_result());
}
if (!$result) {
printf("MySQLi error:<br><b>%s</b><br>%s <br>", $this->error, $query);
}
return $result;
}
And in PDO I just expected it to work with the query() func.
How can I overcome this?
This might work better:
(set tbl1.rank to autoincrement)
TRUNCATE tbl1;
INSERT INTO tbl1 (myId) SELECT myId FROM tbl2 ORDER BY rank DESC;
It's two separate commands but you won't have to worry about the variable issue. It doesn't answer the question "how to run two different commands in one string" or "how to use variables in a query with PHP" but if you're just trying to re-rank things, that should work.
Truncate will empty tbl1 and reset the autoincrement value back to 1

Object Oriented PHP Query (dabl ORM)

I'm trying to use DABL ORM and its object oriented query building with a join however the results are only coming back from the first table I specify, can anybody advise what I'm doing wrong.
https://manifestwebdesign.com/redmine/projects/dabl/wiki/Object_Oriented_Query_Building
$stu = new Students;
$q = new Query;
$q->addColumn(Students::ADNO);
$q->join(Students::SEN, SenStatus::ID);
$q->addColumn(SenStatus::STATUS);
$students = $stu->doSelect($q);
Results when var_dump'd only show columns from students table.
There is no issue with the database structure as the normal sql query:
SELECT adno, status FROM students LEFT JOIN sen_status ON students.adno = sen_status.id
Works fine. Any thoughts?
have you tried all the syntax alternatives?
like
$students = Students::doSelect($q) ?

MYSQL: Help with a left join? Might be better as two queries?

Im not to versed in mysql JOINS, but I think that is what is required for what I am trying to do.
With the help of SO, I got this excellent piece of SQL for calculating a count of items in my database (by categories):
SELECT SUM(`tid` IS NULL) AS `total_null`,
SUM(`tid` = 0) AS `total_zero`,
COUNT(DISTINCT `tid`) AS `other`
FROM `mark_list`
WHERE `user_id` = $userid
Now what I need the query to do is check another table: mark_options to see if the value groupthem = 1. If groupthem = 1 then the above query should be used. If groupthem = 0 then I would like to use the following query:
SELECT tid,
COUNT(*) AS other
FROM mark_list
WHERE userid = $userid
Is it better to run 2 queries, the first one to check if groupthem = 1 or 0, then have PHP decide which final query to run, or to use an SQL JOIN (or other method) to do the same function in a single query?
Thanks!!
Plz send teh reps. Kthx bai.
SELECT SUM(`mark_list`.`tid` IS NULL) AS `total_null`,
SUM(`mark_list`.`tid` = 0) AS `total_zero`,
COUNT(DISTINCT `mark_list`.`tid`) AS `other`,
COUNT(`mark_list`.`tid`) AS `ungrouped`,
`mark_options`.`groupthem`
FROM `mark_list`, `mark_options`
WHERE `mark_list`.`user_id` = $userid
GROUP BY `mark_options`.`groupthem`

Categories