Cannot connect to Snowflake Database [PHP] - php

The structure of the table I'm trying to reach is as such:
Database: INTERNAL_STUFF
Schema: INTERNAL_TEST
Table: TEST_TABLE_SIMPLE
I create a PDO as such:
$dbh = new PDO("snowflake:account=$this->account", $this->user, $this->password);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
If I use this query:
$result = $dbh->query("SELECT * FROM INTERNAL_STUFF.INTERNAL_TEST.TEST_TABLE_SIMPLE");
I end up getting this response - 'Schema 'INTERNAL_STUFF.INTERNAL_TEST' does not exist or not authorized.'. So it appears to be treating the database and the schema as just the schema.
If I use the same query but drop the database from the front:
$result = $dbh->query("SELECT * FROM INTERNAL_TEST.TEST_TABLE_SIMPLE");
I end up getting this response - 'SQLSTATE[22000]: Data exception: 90105 Cannot perform SELECT. This session does not have a current database. Call 'USE DATABASE', or use a qualified name.'
What am I doing wrong here? My user has access to the correct role to view the table, and that exact query (the longer of the two) works just fine in a Snowflake Worksheet.

You may also set the default namespace(database & Schema) for your user using Alter User statement.
Details: https://docs.snowflake.com/en/sql-reference/sql/alter-user.html#usage-notes

Can you execute query USE DATABASE INTERNAL_STUFF and next query USE SCHEMA INTERNAL_TEST before you execute your main Sql query.

Related

Trying to get my current PHP programs to access the DB created by MyPhpAdmin

I have a DB that I created using MYSQL workbench. I was later given access to MyphpAdmin. I was not able to connect to the DB created by Workbench while using MyPhpAdmin.
I then created the same DB using MyPhpAdmin, but now my code, (PHP) cannot locate the DB created by MyPhpAdmin.
I would rather use the DB created by MyPhpAdmin, so what do I need to do to connect to the MyPhpAdmin DB. They are both located on the same server and I am using XAMPP/Apache
Thanks.
Don "Ho"
You can use PDO for that :
$pdo = new PDO('mysql:host=localhost;dbname=mydb;port=3306', 'user', 'pass');
and then use that to make requests :
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ? AND status=?');
$stmt->execute([$email, $status]);
$user = $stmt->fetch();
for fetching multiple rows :
$stmt = $pdo->prepare("SELECT * FROM table WHERE name LIKE ?");
$stmt->execute([$search]);
$data = $stmt->fetchAll();
and if there is no parameter you can just use query :
$stmt = $pdo->query('SELECT name FROM users');
check out the documentation for pdo, there is a lot more functions, like rowCount and so on

PHP PDO MYSQL/MARIADB How to query a view? (view not found error)

I have a view in a MariaDB and a simple PDO query to select all data.
Working with regular tables, there are no issues at all. The only issue I have is that as soon as I use a VIEW, I get the error that the view does not exist.
[826]::sqlstate[42s02]: base table or view not found: 1146 table 'kafexxxx_diner.reservationsxmonth' doesn't exist
::select reservationsxmonth.property,reservationsxmonth.period,reservationsxmonth.total from reservationsxmonth limit 0, 10000
It doesn't look like a rights issue as the statement performs fine in phpMyAdmin.
The question is, can I use PDO to access views, and how to do it? Does the PDO connection require certain options?
$pdo='mysql:host=127.0.0.1;dbname=kafexxxx_diner;port=3306;charset=utf8';
$this->connection = new PDO($pdo, $this->userid, $this->password, $options);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
This works without a problem, so you should check for typos or check in your database, if the view was created
<?php
$stmt = $pdo->query("SELECT * FROM view_name");
while ($row = $stmt->fetch()) {
echo $row['name']."<br />\n";
}
See example

Laravel - Where Condition on Multiple Database

I'm using laravel with multiple DB connection. Oracle and PostgreSQL. I can do query from both of instance.
But the problem is I have to look inside my Oracle DB WHERE NOT EXISTS on my postgreSQL.
This is my current Query :
DB::connection('ora')->table('ora_purch')
->whereExists(function($query)
{
$query->select(DB::connection('pgs')->raw(1))
->from('pg_purch')
->whereRaw('ora_purch.id = pg_purch.id');
})
->get();
From this query, I get error "database pg_purch is not exists". It's just like laravel reads pg_purch as an oracle instance.
I also do simulation with the same query and the same instance (multiple database on postgreSQL only), the query is fine and produces correct data.
Is it possible to makes the laravel reads pg_purch refer to the connection?
Or maybe i've missing something?
Please advise, thank you.
Your code generates query like this:
SELECT * FROM ora_purch WHERE EXISTS (SELECT 1 FROM pg_purch WHERE ora_purch.id = pg_purch.id);
And make it with "ora" connection, ignoring "pgs" connection.
You can't use multiple connections inside one query.
Try to split them:
$ids = DB::connection('pgs')->from('pg_purch')->pluck('id');
$result = DB::connection('ora')->from('ora_purch')->whereIn('id', $ids)->get();

Selecting MySQL views using $wpdb class

I am building a Wordpress site and it is connected to a MySQL database. I am using the wordpress class wpdb (https://codex.wordpress.org/Class_Reference/wpdb) to interact with the database. With that class, I am able to query TABLES of my database, but not Views.
I need to be able to select Views of my database. Is this something that is not allowed with wpdb, or is my code just wrong? Is there a way to query views the same way I can query tables using wpdb?
I have tried using the query function, as well as treating a view the same way I treat a table, but it does not work. It returns empty.
Query method:
$test = $mydb->query(
$mydb->prepare(
"
SELECT name FROM $mydb->$view_name
WHERE id = 1"
)
);
echo $test; //returns empty; should return a name
Table method:
$test = $mydb->get_var(
"select name from $view_name WHERE id = 1"
);
echo $test; //returns empty; should return a name
Any suggestions? Am I able to connect to my database using something other than $wpdb (does Wordpress allow that?).
name seems to be a reserved word in MySQL (reference). Try surrounding it with back-ticks (`) like this:
SELECT `name` FROM ...
You should be seeing some errors, do you keep an eye on the logs? Also, when you have doubts in your queries, you can simply copy the raw query and execute it into phpMyAdmin or whatever tool you are using to access your database manually

PHP IBaseQuery not updating immediately?

I connect the normal way:
$dbh = ibase_connect($host, $username, $password) OR die("could not connect");
Then I run a query:
ibase_query($dbh, 'ALTER TABLE USERS ADD OLDUSERPASS VARCHAR(32) COLLATE NONE') or die(ibase_errmsg());
Directly after this I run:
ibase_query($dbh, 'UPDATE USERS SET OLDUSERPASS = USERPASS') or die(ibase_errmsg());
It complains:
Column unknown OLDUSERPASS At line 1
But when I look in the DB, the column has been created. So, for some reason that split second after run ALTER, the query is not actually committed to the server.
Any ideas why?
Try
ibase_commit($dbh) after alter statement
In Firebird, DDL is under transaction control and you are not allowed to use newly created objects (tables, columns, etc) within the same transaction. So you will first need to commit before executing a query that uses that object.

Categories