Confusion using MYSQLI - php

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

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

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

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.

Update Access Database from PHP script?

I'm relatively new to PHP and databases. Currently I'm working with an existing Access database and am able to read and display the data just fine. However, I'm wanting to update a user record through an email verification link. I have the server send an email with a link of www.domain.com/verify.php?userID=#
I am able to read this GET variable just fine, but I'm completely lost as to how I update the record. Everything I search for is for updating MySQL databases whereas I'm using odbc.
Does anyone know how I could set this up?
something like this:
odbc_exec($conn, "UPDATE table SET field=value"); // $conn is your connection identifier
You need to use a ODBC database client library in PHP to connect to a ODBC datasource. For example you can use PDO with the driver ODBC and DB2 Functions (PDO_ODBC).

Two db connect files

I am using one db file to connect to the database. However, i need to show something else from another db database on this site. I can't use two db connect files. So, how do i go about display one websites content on another website with two different connections.
For one thing, you don't need separate database connections to the same server. Simply specify the database name before each table name in your SQL statements. For example:
SELECT foo, bar FROM db.table;
rather than:
SELECT foo, bar FROM table;
For another, you shouldn't be storing the database connection in a global for just the sort of reason you're running into. I suspect you're using the outdated mysql extension, which uses an implicit DB connection resource whenever one isn't explicit. Switch to PDO and create a simple connection manager class. One big advantage to PDO is it supports prepared statements, which can be safer and more performant than what the mysql extension provides. If you need a PDO tutorial, try "Writing MySQL Scripts with PHP and PDO"
If it is a separate db server you might need to look into the mysql federated engine which allows you to link to databases on separate servers as if they were on the same host.

Can I call DB:connect without affecting the currently active DB?

I'm trying to merge to php code bases which each use a different DB. Can I call DB::connect which changing which DB is considered active? Or can I save the active DB and restore it after calling connect?
Edit: The problem is one code base uses mysql_query() without providing $link_identifier, and I don't want to change all the calls, but I still want to be able to open a 2nd DB connection. Right now it works depending on the order in which I connect to the DBs, which is a pain.
DB::connect creates a connection resource. You can connect to many different databases/servers at the same time. The connection resource contains the database being used. Just store the returned connection reference in different variables and you will be fine.
Connecting to more than one database at the same time is actually very common. For example, connecting to a slave DB for SELECTs and master DB for INSERTs, UPDATEs and DELETEs.
When you create a new connection to the DB, a resource link is usually returned or stored as a member of the DB wrapper class or whatever. You MUST use this link (stored in a variable, for instance) each time you call query functions, etc. If you do not specify a link, the last opened link will be used instead which could be disastrous. If you are diligent about using the correct created link, however, then there will be no problem at all.

Categories