Why is PDO working without dbname? - php

I am connecting to my database using this command:
$resource = new PDO('odbc:driver=FreeTDS;Server=127.0.0.1;Port=8090;UID=Reporting;PWD=readonly;');
There is no dbname specified, and yet, it still connects to a database. The problem is, it is connecting to the wrong database. I tried including a section dbname=DATABASENAME;, but this was entirely ignored. How do I tell PDO to connect to a different database?

Use DATABASE instead of DBNAME, i think this is the problem:
$resource = new PDO('odbc:driver=FreeTDS;Server=127.0.0.1;Port=8090;DATABASE=DATABASENAME;UID=Reporting;PWD=readonly;');

did you try to do the Standard operation? like....
new PDO("odbc:Driver={SQL Server};Server=127.0.0.1;Database=test;",'sa','password');
and which database you are using? ms sql server or some else?

Related

Getting Data from Vertica with PHP with wrong Encoding

I'm trying to get data from Vertica DB using php odbc_connect, I have a problem with Russsian text, so for example instead of getting this text Уютная I got an \x1A\x1A\x1A\x1A\x1A\x1A
This is my DNS connection string:
$dsn = "Driver=Vertica;Server={$host};Port={$port};Database={$database};";
$this->connection = odbc_connect($dsn, $username, $password);
And this my /etc/vertica.ini file (app runs in debian Jessie):
[Driver]
DriverManagerEncoding=UTF-16
ODBCInstLib = /usr/lib/x86_64-linux-gnu/libodbcinst.so.1
ErrorMessagesPath=/opt/vertica
LogLevel=4
LogPath=/tmp
I'm using Vertica ODBC driver 7.2.2-0
Any idea how I can fix it?
Thank you !
Temporary solution I found is to wrap the field with URI_PERCENT_ENCODE function (vertica sql function) and in php you do a urldecode.
stackoverflow

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'

connecting to a database using HostMonster

I'm using HostMonster as my web host and I'm trying connect to a database I created using MySQL inside of HostMonster. In order to call that database in my website do I need to use PHP? Or is there a way to create a javascript OnClick function that can call the database. I'm not using ASP.Net so it's not quite as simple as I would like it. Just curious if the best solution is PHP, if so I guess I should go learn it.
what are you planning to do with the database, other than just 'calling it'? You will need some language like PHP to connect to the DB to retrieve, insert, update or delete data in the DB.
here is a code for connection MySQL from PHP using MYSQLI extension
<?php
$dba_host='localhost';
$dba_name='root';
$dba_pass='';
$dba_db='sn';
$con=mysqli_connect($dba_host,$dba_name,$dba_pass,$dba_db) or die('Connection Refused !');
$stmt=mysqli_prepare($con,"SELECT UID FROM Main");
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $value);
while(mysqli_stmt_fetch($stmt))
$result[] = $value;
mysqli_stmt_close($stmt);
mysqli_close($con);
?>
Your javascript onClick function is running on the client side (in the browser) and the database is running on the server-side. You will need a server-side language to get the information from the database and send it to the browser.
You do not HAVE to use PHP to connect to a MYSQL database. Also, you can't connect to your database using only client-side javascript (ie. an onClick() function). You need to use a server side language, PHP is one choice.
To connect to a MYSQL database on hostmonster using PHP you will need to know your credentials that use to log into phpMyAdmin from your cpanel. Once you have made the connection you can then select the MYSQL database that you created. Once the database is selected you can query it using the "mysql_query" function in PHP. The following code does all of that and stores the results of the MYSQL query in a PHP variable called $result.
<?php
$con = mysql_connect("www.yourdomain.com","phpMyAdmin_username","phpMyAdmin_password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mysql_database_name", $con);
$query = "SELECT * FROM TableName"
$result = mysql_query($query);
?>
Now you've got the results of the query inside the PHP variable $result and you can use it anyway you like.
If you put this in your 'public_html' folder and named it 'index.php' or 'index.html' this would automatically be run when someone went to www.yourdomain.com.
You can find a great tutorial series on PHP here http://thenewboston.org/list.php?cat=11.

Best way to access sqlite database in read mode using php

Couple questions. Whats the best way to access a live .db file using sqlite to just read data?
Secondly
$url = "data.db";
try
{
$db = new PDO('sqlite:"$url"');
...
How can I correct this syntax so I can set the database url as a variable? If I must use a different method other than PDO because of question 1 please set it out with a variable for database url.
Thanks
Shamelessly taken from Opening SQLite3 as READONLY with PDO?:
Instead of PDO, try the SQLite3 library:
$db = new SQLite3($url, SQLITE3_OPEN_READONLY);
For more details, refer to PHP's doc:
http://php.net/manual/en/sqlite3.open.php

how to access firebird from Mysql?

dear all..
i have a DB which built use Mysql.
for this case, i want to access another server PC which have DB in firebird. i want to take some data inside that. Both of DB always connected, because i must get data every time. This is a scheduling data, so i must always connected to firebird from mysql.
But i have no experience to make connection between Mysql and firebird.
can you tell me how to do that?
what is the best way that must i choose, do some migration or convert or may make sync.?
any advance will be appreciate.thanks.
could I call the firebird 1st(use ibase_connect) then insert the data to mysql? i dont know how to insert data to Mysql DB after i get from firebird DB.
At this MySQL server can only connect to other MySQL servers. Not sure about Firebird, but if it's similar, then you need to do all you data handling in external application.
Take a look at MySQL Migration Toolkit. According to this post firebird is supported.
Cheers!
I suggest migrating firebird data to MySQL, this would help you avoid some pain in the ass :)
Unfortunately I have a project which has to be synced with some external firebird db...
may be use php can work.
<HTML>
<HEAD>
<TITLE>PHP + Firebird / Interbase test (connection)</TITLE>
</HEAD>
<BODY>
<H3>FB Connect test.</H3>
<?php
// DB definition of account
define("DBNAME","xx.xxx.xx.xxx:D:\DATABASE\OCS DATA.FDB"); // data bsse name
define("DBUSER","SYSDBA"); // user name
define("DBPASS","masterkey"); // password
// DB connection
$dbh = ibase_connect(DBNAME,DBUSER,DBPASS);
if ($dbh == FALSE) {
echo 'could not connect to DB<BR>';
} else {
echo 'success to connect to DB<BR>';
// DB dis connection
ibase_close($dbh);
}
?>
</BODY>
</HTML>

Categories