PDO Construct error - PHP - php

I have created a wine shop in PHP that accesses a MySQL database and pulls the wines data onto a webpage. However, after getting it all working nicely I have now come across a PDO Construct error and do not know why.
The error is as follows:
Warning: PDO::__construct() [pdo.--construct]: [2002] A connection
attempt failed because the connected party did not (trying to connect
via HOSTNAME) in C:\webdev\shop\selectfromwines.php on line 5
It seems to relate to the following piece of code:
$database = new PDO('mysql:host=HOSTNAME; dbname=co525', 'co525', 'co525');
Any ideas where I might be going wrong?
P

is HOSTNAME real host name? If not - use correct address. If you have local db, try 127.0.0.1 or localhost

Open your php.ini and set the following:
pdo_mysql.default_socket=”/opt/lampp/var/mysql/mysql.sock”

Related

Issue connecting to PHP with Ajax chat

Using Ajax integrated chat with PHPBB3. It is supposed to use the mysql connections that the site uses. The database tables have been created. When accessing, I continue to get the error
'>[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/di/container_builder.php on line 291: file_put_contents([ROOT]/chat/../cache/container_[ROOT]/chatslashdotdotslash.php): failed to open stream: Invalid argument Error-Report: No connection could be made because the target machine actively refused it. Error-Code: 2002'
Edit: I have not had any trouble connecting to the database with the web application itself. I've read that the hosts file may need to be edited. I made sure that the socket server option for AJAX chat is set to false. I'm not sure what this error is actually trying to say here or what the actual connection error is. Can someone point me in the right direction?
The php class container_builder is where the error falls. Here is the function being called at that point:
protected function dump_container($container_filename)
{
$dumper = new PhpDumper($this->container);
$cached_container_dump = $dumper->dump(array(
'class' => 'phpbb_cache_container',
'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
));
file_put_contents($container_filename, $cached_container_dump);
I cleared out the particular cache file, which was then recreated, but the error does not go away. I would think there is no problem with connectivity to the database by the application. Cache is stored on the server not the database.

Connect to MS Access with PHP not working

I am trying to connect to a MS Access Database with PHP. It is working perfectly when I am creating a System DSN but how do I make the connection work, when I want to copy and use the PHP files plus Database on a different computer? (Without creating another System DSN on that computer as well)
At the moment I am trying it this way:
$conn = odbc_connect("odbc:DRIVER={Microosoft Access Driver (*.mdb)}; DBQ=$odbc_name; Uid=$Uid; Pwd=$Pwd;");
And I am getting this error:
Warning: odbc_connect() expects at least 3 parameters, 1 given in C:\wamp\www\PartB\db_connection.php on line 14
The file is correctly found with this line of code:
$odbc_name = $_SERVER["DOCUMENT_ROOT"] . "PartB\db.mdb";
So where is my problem? Why is this way not working, but System DSN is? Any ideas?
Ok I found a answer to this myself.
$conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$odbc_name";
The above code makes the connection work without System DSN.
Now I just need to reconfigure my query statements and all good.

I am getting the error that database is unknown

Where is mistake? I try to connect to database from php script. I am getting the error that database is unknown. Database for sure exists, i can see this from PhpMyAdmin.
I use XAMPP v3.2.1 with mysqlnd 5.0.11-dev and php v5.5.9.
Script file debug1.php in folder c:\xampp\htdocs:
$conn=mysqli_connect( 'localhost', 'root', '', $vdbname ) or die( "cannot connect to server".mysqli_error() );
When i run this file from browser windows using url: "localhost/debug1.php" i am getting error message:
Warning: mysqli_connect(): (HY000/1049): Unknown database 'gintare_calendar' in C:\xampp\htdocs\debug1DBconn.php on line 5
You may want to look for whitespace in database name. From phpMyAdmin go to database Operation sections and see if white space has been added in front or after database name.
This means the database you want to connect to is not called "gintare_calendar".
Open your MySQL administration package (phpmyadmin) and verify the actual name of the database you want to connect to.
I am sorry, i forget that i reinstalled XAMPP. It is true. There is no database. I have to create the database, import previous tables and than everything works again.

PDO connect to SQLite server on different server

I have a very simple SQLite database that I need to read/write to from a different server.
Say the database is stored here : http://www.abc.com/data/data.sqlite
And I'm using PHP to access it from http://www.xyz.com
So my first attempt was the following:
$dbpath = "http://www.abc.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
No good, I get the following:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [14] unable to open database file'.........PDO->__construct('sqlite:http://w...') #1 {main} thrown
If try and copy the database onto the same server I'm accessing from:
$dbpath = "http://www.xyz.com/data/data.sqlite";
$dbconn = "sqlite:$dbpath";
$db = new PDO($dbconn)
I get the same message.
It's only when I give it a relative path on the same server:
$dbpath = "../data/data.sqlite";
That it actually works.
I know the database URLs and database itself are correct.
So is there a limitation to accessing cross-servers? Anyone know out to solve this issue?
Thanks a lot.
There are no such thing like 'SQLite server'. It exists only in a form of a file.
But there are no files in the HTTP protocol, but URIs only.
So, this is essential incompatibility.
To be able to do remote calls you have 3 choices
Only as a joke: download the file locally before each SELECT query and upload it back after updates
Establish some proxy script, to recieve a query and to return json.
Get yourself a real database server.
Or just change the project architecture to eliminate the need of remote access.

PHP - MYSQL - test database server

I'm learning PHP MYSQL and trying to create a database.
I'm following this tutorial
http://www.raywenderlich.com/2941/how-to-write-a-simple-phpmysql-web-service-for-an-ios-app
So far I'm testing to see if the server has access to MySQL. When I use the following code.
?php
class RedeemAPI {
private $db;
// Constructor - open DB connection
function __construct() {
$this->db = new mysqli('localhost', 'username', 'password', 'promos');
$this->db->autocommit(FALSE);
}
// Destructor - close DB connection
function __destruct() {
$this->db->close();
}
// Main method to redeem a code
function redeem() {
// Print all codes in database
$stmt = $this->db->prepare('SELECT id, code, unlock_code, uses_remaining FROM rw_promo_code');
$stmt->execute();
$stmt->bind_result($id, $code, $unlock_code, $uses_remaining);
while ($stmt->fetch()) {
echo "$code has $uses_remaining uses remaining!";
}
$stmt->close();
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api->redeem();
?
I get the following error:
Warning: mysqli::mysqli() [mysqli.mysqli]: [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Users/user/Sites/promos/index.php on line 8
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2002): No such file or directory in /Users/user/Sites/promos/index.php on line 8
Warning: mysqli::autocommit() [mysqli.autocommit]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 9
Warning: mysqli::prepare() [mysqli.prepare]: Couldn't fetch mysqli in /Users/user/Sites/promos/index.php on line 20
Fatal error: Call to a member function execute() on a non-object in /Users/user/Sites/promos/index.php on line 21
Did I perhaps forget to enable something?
It might explain why I cannot connect via my IP address to sequel pro and only to 127.0.0.1?
Apache servers use '127.0.0.1' instead of 'localhost'
There are two settings you have to check for this to work (assuming you have MySQL server installed of course):
Check the value of mysql.default_socket in your PHP configuration.
Check the value of socket in your MySQL configuration file under the [mysqld] heading.
Those values have to be identical; if they're not, change one to match the other and restart respective service.
This seems to be a common issue, as googling for it yields quite a few results. I experienced this on my two linux boxes as well (never under Windows though) and at some point I resolved to just use 127.0.0.1 on all dev servers. Basically, localhost makes the connection to the MySQL server use a socket, but your configuration doesn't point to the socket file correctly.
Here are a couple of resources that you might find useful:
http://westsworld.dk/blog/2011/03/problems-connecting-to-unixvarmysqlmysql-sock/ - basically what #Jack suggests, but more in depth
Can't connect to MySQL on Mac -- missing mysql.sock file - here's how to locate your socket file (for some reason, my php.ini had the path wrong, and I assume your case could be similar).
I hope this helps.

Categories