"use" command failing with syntax error in PostgreSQL (converting from MySQL) - php

So, I am trying to convert a MySQL-based database to a PostgreSQL database, and I'm getting some less-than-desirable results. My code begins with this:
create database bookmarks;
use bookmarks;
But I get an error message that reads:
ERROR: syntax error at or near "use"
LINE 2: use bookmarks;
^
I am honestly at a loss in regards to what I need to do to fix this. Any help you can offer me would be appreciated. Thanks.

In PostgreSQL, you connect to the particular database you want to use when you initiate a connection to the server. How you need to do this depends entirely on what language / environment you're using.
If you're doing this by hand using the psql command-line tool, though, then USE mydatabase; in MySQL is equivalent to \c mydatabase; in PostgreSQL.
Edit: In PHP, if you want to create a new database and then connect to it, you'd do the following:
Connect to your server using an existing database - template1 or postgres are common choices for this. Execute CREATE DATABASE bookmarks; using this connection.
Create a new connection to the database you just created, using new PDO("pgsql:host=localhost;port=5432;dbname=bookmarks;user=myuser;password=mypassword"); (replace the DSN parameters with your particular values as needed, of course).
Now you're connected to the new database, and any queries you execute will be executed against bookmarks. No need to execute USE bookmarks; or anything else.

Related

How to force the creation of a NEW DATABASE CONNECTION using PHP's PDO to connect to MySQL

I use PHP to connect to MySQL.
At last, I switched to the PDO interface from the deprecated mysql_! I love it, and it works great, but one lingering issue...
MY QUESTION: Does a new PDO (using the same credentials) always create a new connection to the database?
If that's the case, then I'm all set!
I am aware of the option of "Persistent connections" in PDO; I do NOT use that option.
With the old mysql_connect() function, I could FORCE a new database connection with the new_link flag. Am I correct in my understanding that with a new PDO, one ALWAYS gets a new database connection? (Unless requesting a "persistent connection" - which, once again, I do NOT do.)
If I understand correctly, PDO is the opposite of mysql_connect in that (assuming identical database credentials) PDO always gives a new connection, unless specified otherwise (i.e. unless requesting a "persistent connection") - WHEREAS by default mysql_connect would give the same old connection, unless you forced a new one.
Side note: as to WHY I want to force a new connection, it's part of my implementation of a more robust SQL query execution mechanism. I discovered over the years that, when a PHP script is used to serve large files, occasionally a new SQL query gets a lost database connection error ("the database has gone away"); in those cases, my remedy - which worked perfectly for years - has been the following algorithm:
1) try to run the SQL query
2) in case of error, force a new database connection [critical step!] and then re-run the SQL query. It if fails a 2nd time, give up and issue an error/log; but, in most cases, the problem goes away and the 2nd attempt works :)
I'm trying to replicate that robust function with PDO... I found excellent guides on the mysql -> PDO switching (such as http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ), but I'm still hazy whether instantiating a new PDO object implies a new database connection in cases where an earlier PDO object was created with the same credentials.
Thanks!!
Yes it's a new instances of the connection using the credentials given. You can see this with MySQL SHOW FULL PROCESSLIST via MySQL command-line, as it creates each new connection and query, just have to be fast about it or run some slow queries.
On a side note; running the insert again is a bit dirty workaround to an unoptimized process with the database design and mysql config, I recommend revisiting the items in question and find a better method to the process. Best

mysql w/ mysqli or PDO fail to find stored procedure when schema name includes '.' characters

First, I have read and googled on this issue extensively using various combinations of the the search terms php pdo or mysql database name escape. Sadly, I have not found anyone dealing with this same issue.
I have a mysql database with a schema named www.company.com
Generally this is not an issue, as it can easily be escaped using the back tick character.
However, when using mysqli or PDO to call a stored procedure in that schema, it always returns:
PROCEDURE www.company.com.usp_stored_procedure_name does not exist
Obviously, this is a simple mater of putting ` characters around the www.company.com however I cannot find away to do this using either mysqli or PDO. I have also failed to find reference to this issue in the documentation.
I should also point out that this procedure runs just fine from mysql command line client or from MySQL Workbench as in those cases I can include the back ticks or a use statement. Furthermore, I've already verified that this is not related to database user permissions.
However, every time I try to use the stored procedure through mysqli or PDO I receive the error above.
As this procedure is responsible for ETL operations and returns multiple results sets, converting it to individual statements called from php would be very painful. I am desperately hoping that someone has dealt with this problem before.
The following are some of the methods tried:
Note: back-tick nesting seems to cause a mess w/ the forum, so I've used the '-' character in place of a `
I've tried using an escaped database name when calling to mysqli_connect...
$mysqli=mysqli_connect($host, $this->db->username, $this->db->password, '-www.company.com-');
...and received an error.
I've tried calling the stored procedure w/o a database name...
$mysqli->multi_query("CALL usp_stored_procedure_name(50, '2013-05-01')");
...and received the error referenced above.
I've tried calling the stored procedure w/ a database name...
$mysqli->multi_query("CALL -www.company.com-.usp_stored_procedure_name(50, '2013-05-01')");
...and received the error referenced above.

Use MySQL and PostgreSQL in the same PHP script

I'm trying to do some migrations from an old site to a new site. The old site uses MySQL and the new site uses PostgreSQL. My problem is I wrote a migration script in PHP that queries info from the old DB so that I can insert them into the new DB within that same script. The reason I need the script is I have to call other functions that do things and manipulate the data since the table columns aren't a one for one match so I can't just do a backup and restore type situation. I have a class for both DB's that I use.
The mysql queries work but postgres' don't. They get error messages saying pg_query(): 19 is not a valid PostgreSQL link resource in xxx
So is it possible to run them both in the same script? If I call the two scripts separately it works ok but I can't get the data from the old server to the new one.
I've looked everywhere and don't see many questions needing to use both DB's in one file.
Any help would be cool.
You are using the same variable for both resources and passing the mysql resource to the postgresql function

Confusion using MYSQLI

I just started using mysqli API for PHP. Apparently, every time an object of the class MYSQLI is instantiated, it can setup a connection to the database as it connects to the server unlike mysql_connect, which connects to the server first and then you are required to specify the database to query.
Now this is a good problem if the db exists, in my case, the db does not exist on the first ever connection to the server/execution of the problem, hence I must connect without specifying the database, which is fine, since the msyqli constructor does not make this database mandatory.
My challenge is essentially, how do I check if the database exists before attempting the first connection. The only way to really do this would be to establish a conection to the server and then use the result of the following query to gauge if the database exists:
SELECT COUNT(*) AS `exists` FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMATA.SCHEMA_NAME="dbname" ;
If this returns true, then the database exists, but now the challenge is how do I get the mysqli object to query this database rather than having to prefix the name of the database in the query.
Thanks much
USE databasename as a query alters the current working database. or you can of course use $mysqli->select_db('databasename');

How can I check a MySQL query syntax?

I'm working on my own database management system, developped in PHP, and I've chosen the same syntax as the MySQL queries for my own queries.
I'd like to know if there was a tool to check that a MySQL query is valid, without having to connect to a real MySQL database.
Does someone have a way to do it ? I've though about using some regular expressions but I'm not sure this is the easiest (and fastest) way to do this.
There's a few good PHP SQL parsers that break down the query into structured arrays.
You could run the code through the parser and see if it breaks to determine whether it's valid syntax.
http://code.google.com/p/php-sql-parser/
and
http://pear.php.net/package/SQL_Parser
are 2 I have used in the past.
set #s := 'your sql script';
prepare stm1 from #s;
a tool like that can't ensure tables or columns exist. the tool needs database schema for checking errors. I use heidisql and If I had more money ı would buy right to have more than one database on mysql server and I would execute the "create database" or copy database code and test on the second database which doesn't have any values, or prefereably, with values.

Categories