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.
Related
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.
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.
UPDATED
My latest code as follows:
$file ="sqlite:C:\New folder\\test.db";
echo $file;
$handle = new PDO($file) or die("Could not open database");
Error
sqlite:C:\New folder est.db
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php:20 Stack trace: #0 C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php(20): PDO->__construct('sqlite:C:\New f...') #1 {main} thrown in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 20
I'm trying to use PDO driver however the error keep prompting me saying cant find the driver when it's in my extension php folder. I'm at my wits now.
I'm using SQLITE version 3 and PHP version 5.4.3.
I already placed the database file inside my r/w folder but it Fatal error: Call to undefined function sqlite_open(). I already researched on the web for alot of sources but the my directory path still returns some strange error (Sorry if i'm too noob, first time learning).
My code as follows:
$db ="C:\New folder\test.db";
Test to print my directory path of the database.
echo $db;
$handle = sqlite_open($db); //or die("Could not open database");
Debug result
C:\New folder est.db
Fatal error: Call to undefined function sqlite_open() in C:\inetpub\wwwroot\NetBeans\PHP&SQLITE.php on line 18 which is "$handle = sqlite_open($db); //or die("Could not open database");"
Kindly advise where did I do wrong? The path I specified was correct but when I print out, it shows wrong path.
Thanks
one problem you have is that you are escaping (inadvertently) some of your path characters.
As your var_dump shows.
C:\New folder est.db
change your $db variable definition to
$db ="C:\New folder\\test.db";
or just put path in single quotes so PHP won't try and parse the string
$db ='C:\New folder\test.db';
and believe it or not in most cases unix paths will work:
$db ="C:/New folder/test.db";
as for the remainder of your problem, you need to make sure you have sqlite 2 enabled. View your php info and check to see if you a section like:
SQLite
SQLite support enabled
PECL Module version 2.0-dev $Id: sqlite.c 306939 2011-01-01 02:19:59Z felipe $
SQLite Library 2.8.17
SQLite Encoding iso8859
if not, in your php.ini make sure the line
extension=php_sqlite.dll
is uncommented and present (and the .dll exists in your extension folder).
However most people will recommend that you use the PDO driver for sqlite (and for any other database) as it will provide a more consistent paradigm across the different databases (you can use the same code with most databases).
[edit]
I found some comments in the php manual that may shed some light on your issues.
It seems that the directory that contains your sqlite database must be
writeable by the web server. Making just the file writeable won't
work.
and
Don't forget "extension=php_pdo_sqlite.dll" has to be enabled in
php.ini (if you use xampp is will be disabled by default) .
I'm trying to use the dbase library in php5.3 to open a .dbf file. I've got the dbase.so library installed and active on my php5 build and I'm executing the following code:
$db = dbase_open('CMX.dbf', 0);
if( $db ){
echo 'success';
dbase_close($db);
}
Where CMX.dbf is a Visual FoxPro9 data table and is located in the same directory as the executing script with read/write/execute permissions enabled.
The following is an exert from /var/log/apache2/error.log:
PHP Warning: dbase_open(): unable to open database CMX.dbf in /var/www/test.php on
line 28
As this error/warning is not very descriptive, I'm having issues tracking down the root cause. Can anyone help with this?
Not positive about PHP, nor Apache, but typically, when trying to connect to database files (or Foxpro), the typical approach would be to have a CONNECTION to a PATH, then perform a query against the name of the table....
Try this (in foxpro):
use cmx.dbf
copy to cmx_php.dbf type fox2x
I was having a similar problem where some dbs would open and others would not. This allowed me to access the db with php/dbase.so
I found the info here in the comments section.
Try this:
$db_path = "CMX.dbf";
$db = dbase_open($db_path, 0) // 0=ReadOnly, 1=WriteOnly 2=ReadWrite
or die("Error! Could not open dbase database file '$db_path'.");
if( $db ){
echo 'success';
dbase_close($db);
}
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”