What does Apache need to support both mysqli and PDO? - php

I'm considering changing some PHP code to use PDO for database access instead of mysqli (because the PDO syntax makes more sense to me and is database-agnostic). To do that, I'd need both methods to work while I'm making the changeover.
My problem is this: so far, either one or the other method will crash Apache.
Right now I'm using XAMPP in Windows XP, and PHP Version 5.2.8. Mysqli works fine, and so does this:
$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database';
$sql = "SELECT * FROM `employee`";
But this line makes Apache crash:
$dbc->query($sql);
I don't want to redo my entire Apache or XAMPP installation, but I'd like for PDO to work. So I tried updating libmysql.dll from here, as oddvibes recommended here. That made my simple PDO query work, but then mysqli queries crashed Apache.
(I also tried the suggestion after that one, to update php_pdo_mysql.dll and php_pdo.dll, to no effect.)
Test Case
I created this test script to compare PDO vs mysqli. With the old copy of libmysql.dll, it crashes if $use_pdo is true and doesn't if it's false. With the new copy of libmysql.dll, it's the opposite.
if ($use_pdo){
$dbc = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
echo 'Connected to database<br />';
$sql = "SELECT * FROM `employee`";
$dbc->query($sql);
foreach ($dbc->query($sql) as $row){
echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
}
}
else {
$dbc = #mysqli_connect($hostname, $username, $password, $dbname) OR die('Could not connect to MySQL: ' . mysqli_connect_error());
$sql = "SELECT * FROM `employee`";
$result = #mysqli_query($dbc, $sql) or die(mysqli_error($dbc));
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo $row['firstname'] . ' ' . $row['lastname'] . "<br>\n";
}
}
What does Apache need in order to support both methods of database query?

Not a direct answer, but perhaps it can help you in your search for it:
The legacy mysql extension (The function prefixed with mysql_) is a thin wrapper over libmysqlclient. The new mysqli extension is essentially the same, but it implements some functionality that were introduced in later versions of libmysqlclient. PDO also uses libmysqlclient, but doesn't map it as directly as the other extensions do. This all amounts to 3 different php-extensions that all refer to the same native library. If some of them make assumptions about the version of the library, it might cause them to clash.
I would suggest that you install the newest version of libmysqlclient.dll that you can find and try to disable the legacy mysql extension (if you haven't already).
If you have code that uses mysql extension, you can have mysqli bind to those functions and it should work the same.
Also, make sure that you don't have the new mysqlnd driver installed for some reason. mysqlnd is an alternative implementation of libmysqlclient and it really just makes everything even more complicated.
Yes, it's a big mess.

It seems like all you need is to uncomment the correct extension directives in php.ini, like extension=php_mysqli.dll and extension=php_pdo_mysql.dll.
I never confirmed this with XAMPP, because I got frustrated with it and decided to install Apache, PHP and MySQL individually. In the process, I learned to do the above steps, along with a lot of other things. So I haven't confirmed that this will work with XAMPP, but I don't see why not, unless it doesn't come with the right drivers.
The two drivers (.dll files) in the example directives above are the ones I'm using. I also set extension_dir = "c:/php/ext" in php.ini - you should make sure that path points to the directory where your php extensions are. (Note that one of the nice things about installing these components separately is that I can put PHP in c:\php instead of having it buried several levels deep in c:\xampp.)
If anybody wants to follow along with installing your server components separately, I documented the whole process, with screenshots, on my blog.

same as Why does this pdo::mysql code crash on windows?
but dont have the solution for the moment
Crash happens only on "SELECT * FROM xxx" query() and prepare/execute

The unique solution i found :
Install LibMySQL.DLL FROM 5.0.51a package (working with last 5.1.44 MySQL Version)
http://www.netfulvpc.fr/files/libmysql_dll.zip

Ok, i have a very bad but working solution :p)
Open ext\php_pdo_mysql.dll with an Hex Editor
Search for "83 C3 50" offset 0x000024d5 (in php 5.2.12 vc6)
Replace by 83 C3 "54"
The problem is a bad structure size... (struct pdo_column_data)
I tried to fix that in libmysql.dll but that crash php_mysqli ;)

Related

Thrift + Cassandra + PHP + Windows?

Connecting to Cassandra with PHP is really a pain. The documentations at Apache and DataStax are extremely poorly written - for Windows users.
I have Thrift installed (I believe!) via Chocolatey. But I am still not able to compile php code for Cassandra using thrift.
If you look at this link ,
now we can compile php code for Cassandra using thrift I used command: d:\cassandra\trift\thrift.exe --gen php
d:\cassandra\interface\cassandra.thrift
So what is cassandra.thrift and where does it come from?? WHAT should I put inside it??
If I follow the instruction exactly, I get this error,
Could not open input file: d:\cassandra\interface\cassandra.thrift
So what is going on?
How do I make this work?
I have tried to install DataStax PHP Driver for Apache Cassandra and that documentation even worst.
Why PHP modules do not come with Cassandra like it does for MongoDB? Most of the independent drivers I found are outdated, not supported anymore or abandoned.
EDIT:
From the README,
Install the PHP extension
Installing with pecl
The PHP driver is not published to the official PECL repository yes.
You can still install it using pecl by specifying the provided
package.xml file path as the argument to pecl install command.
Install the 2.0 version of the C/C++ driver
not published to the official PECL repository yes - is it yes or yet?
Obtaining Build Dependencies
CMake
Git
ActiveState Perl
Python v2.7.x
I have downloaded and installed. Then, what? In Building the Driver,
A batch script has been created to detect installed versions of Visual...
What? Where does A batch script suddenly come from??
Then,
First you will need to open a “Command Prompt” (or Windows SDK Command
Prompt) to execute the batch script.
Usage: VC_BUILD.BAT [OPTION...]
--DEBUG Enable debug build
--RELEASE Enable release build (default)
--DISABLE-CLEAN Disable clean build
....
What are these bunch of '--' for?
To build 32-bit shared library:
VC_BUILD.BAT --X86 To build 64-bit shared library:
VC_BUILD.BAT --X64
Where does .BAT come from? What should I put inside it? Where should I run it from??
After all, what are those Build Dependencies for? How do I use them??
Just hope that someone can write a proper guide then the guide above - it is frightening! (if you compare the guides in MongoDB, it is far better and professional)
EDIT 2:
First error when I run the .bat from my desktop,
I have git installed already but I still have this error,
After fixing git issue above, I have a new one - it just frozen there, nothing happens,
The IDL file cassandra.thrift is usually part of the cassandra package, but you can find it following the link above. The link points to trunk, you may want another version.
After you downloaded the right version of that file or better found it in your downloaded Cassandra package in the interface folder, generate the code as outlined in the documentation you have. The rest should be easy.
Why PHP modules do not come with Cassandra like it does for MongoDB? Most of the independent drivers I found are outdated, not supported anymore or abandoned.
I'm not really sure about that, but my guess is that the fact CQL is promoted heavily for a while now instead of using the raw Thrift API - the latter is a complex task, while CQL is more easy to use - is one of the key factors why it is this way. It more or less eliminates the need for another wrapper.
PS: Just to be sure:
thrift.exe --gen php d:\cassandra\interface\cassandra.thrift
could not open input file cassandra.thrift
Of course you point to the right drives, folders and files, do you?
Forget about Thrift and the 'beta', I found a better solution. It very straight forward and extremely easy!
Example codes,
require_once 'lib/Cassandra/Cassandra.php';
$cassandra = new Cassandra();
$s_server_host = '127.0.0.1'; // Localhost
$i_server_port = 9042;
$s_server_username = 'admin'; // We don't use username
$s_server_password = 'password'; // We don't use password
$s_server_keyspace = 'demo'; // We don't have created it yet
$cassandra->connect($s_server_host, $s_server_username, $s_server_password, $s_server_keyspace, $i_server_port);
// Tests if the connection was successful:
if ($cassandra) {
// Select:
// Queries a table.
$cql = "SELECT * FROM users;";
// Launch the query.
$results = $cassandra->query($cql);
// Update:
// Prepares a statement.
$stmt = $cassandra->prepare('UPDATE users SET first_name = ?, last_name = ? where id = ?');
// Executes a prepared statement.
$values = array('first_name' => 'Fred', 'last_name' => 'Smith', 'id' => '1');
$result = $cassandra->execute($stmt, $values);
// Insert:
// Prepares a statement.
$stmt = $cassandra->prepare('INSERT INTO users (id, first_name, last_name)
VALUES (:id, :first_name, :last_name)');
// Executes a prepared statement.
$values = array('first_name' => 'John', 'last_name' => 'Robinson', 'id' => '4');
$result = $cassandra->execute($stmt, $values);
// Delete:
// Prepares a statement.
$stmt = $cassandra->prepare('DELETE FROM users WHERE id = :id');
// Executes a prepared statement.
$values = array('id' => '4');
$result = $cassandra->execute($stmt, $values);
// Closes the connection.
$cassandra->close();
}
Update:
The datastax PHP driver is now GA and binaries are available for download (no need to build it yourself):
https://github.com/datastax/php-driver
Regarding the DataStax PHP driver, the instructions are being improved per your feedback as I type.
Because this Driver is in Beta, we do not yet have pre compiled binaries that you can simply download. They will be available once the driver is GA. For now you will have to build them yourself.
The process for building the binaries is very straight forward. 1) Install the dependencies 2) run vc_build.bat.
You can find the vc_build.bat here (just right click save as from your browser):
https://raw.githubusercontent.com/datastax/php-driver/master/ext/vc_build.bat

php loop through results without using the native driver

I need to rewrite my php code so that it does not use the native driver php5-mysqlnd.
On my development machine my code works fine because I have php5-mysqlnd installed, but as soon as I installed php5-mysqlnd on my VPS host then apache failed and wouldn't start. And so I needed to have everything wiped and a fresh server install. It's something to do with Plesk, I don't care about those details I know I can't use php5-mysqlnd so I just need my php to work without it.
So I don't care that everyone says it is the wrong technology and I don't care if people say it's an old technology.
The code below works on my development machine and I need to rewrite it without the native driver, how do I do that?
$db = new mysqli($_SESSION["DB_HOST"], $_SESSION["DB_USERNAME"], $_SESSION["DB_PASSWORD"], $_SESSION["DB_DATABASE_NAME"]) or die('Cannot connect');
$stmt = $db->prepare("call get_languages( ? )");
$stmt->bind_param('i', $iLanguageId );
$stmt->execute();
if ($result = $stmt->get_result()) {
while ($row = $result->fetch_assoc())
{ echo "<p>" . $row['language'] . "</p>"; }
$result->free_result();
}
else
{ echo '<h2>Error: No languages listed.</h2>'; }
You could uninstall php5-mysqlnd switch back to libmysqlclient if that's what you want.
Another option is to use the PDO driver and rewrite your code to use PDO instead.

Using MSSQL with PHP on CentOS

I have installed and configured FreeTDS on my CentOS environment to enable my PHP code to get connect to some external MSSQL server. I have my entire application running on written using PDO which is running perfectly on windows. Just wanted to know if my same PDO code can be used even in CentOS or else I have to convert entire code from PDO to mssql_query form.
Like in PDO I have :
$statement_keyword = $obj->conn->prepare($keywordquery);
$statement_keyword->execute();
$rows_keyword = $statement_keyword->fetchAll(PDO::FETCH_ASSOC);
In MSSQL using FreeTDS we have to write
$result = mssql_query($keywordquery);
while ($Row = mssql_fetch_assoc($result)) {
$iw[$i++]=(string)$Row['FullName'];
.......
}
Means I have to run thru entire loop to get the record array, similarly there may be more changes that I need to make like in stored procedures execution ...
Can anyone guide me in using PDO for MSSQL on CentOS, or an easy way other round.
Thanks
After looking for net and doing some research and experimental work I realised it is very easy to use the PDO code for windows to PDO equivalent without must changes, the only change I need to make was in the manner the connection is establised
Using PDO with MSSQL In windows
$this->conn = new PDO("sqlsrv:server=".$this->dbServer.";Database={$this->dbName}",$this->userName,$this->userPassword);
Using PDO with MSSQL on CentOS
$this->conn = new PDO("dblib:host=$this->dbServer;dbname={$this->dbName}",$this->userName,$this->userPassword);
Make sure you have configured FreeTDS, ODBC , etc on your CentOS Stack

How to configure wampserver for spatialite

I am in windows 7. I installed WAMPServer. Now, I can not load spatialite libraries. It is showing warning..
Warning: SQLite3::loadExtension() [sqlite3.loadextension]: Not
supported in multithreaded Web servers
Here is my total configuration procedure, what I have done...
From this link I got spatialite lib. I copied "libspatialite-1.dll" and paste it to "D:\wamp\bin\php\php5.3.8\ext" which contains php extention dlls.
http://www.gaia-gis.it/spatialite-2.3.1/libspatialite-win-x86-2.3.1.zip
Then I edited php.ini file. I changed the following configuration.
.
sqlite3.extension_dir = C:\libspatialite-win-x86-2.3.1\bin
.
extension=libspatialite-1.dll
.
enable_dl = On
And lastly, I copy and paste all the libraries from my downloaded libspatialite-win-x86-2.3.1 to my project folder(libspatialite.a, libspatialite.dll.a, libspatialite.la) in my php code i write the script as follows...
<?php
$db = new SQLite3('sixcommunes.sqlite');
$db->loadExtension('libspatialite.a');
$rs = $db->query('SELECT spatialite_version()');
while($row = $rs->fetchArray()){
print "<h3>SQLite version: $row[0]</h3>";
}
?>
I do not know what I have done wrong or how to solve this problem?
Haven't seen any answer, so giving my own. I have used spatialite in C#, I think for using Spatialite Extensions you don't have to load libspatialite like a usual PHP extension, instead something similar to this should be done. (this is c# code, just trying to give you an idea)
SQLiteCommand sqliteCommand = new SQLiteCommand(String.Format("SELECT
load_extension('{0}');", "libspatialite-2.dll"), connection);
sqliteCommand.ExecuteNonQuery();
You have to execute Select load_extension("libspatialite-2.dll") on sqlite to use Spatialite.
Hope this helps

Connecting to MySQL with PHP

I have MySQL running such that I can open a client command line and log on and make databases, tables, etc.
I wanted to do some AJAX. Doing AJAX with ASP, SQL Server, etc is not advisable since both places where I am hosting my websites do not use the Microsoft products. So I am forced to do my development with PHP and MySQL.
I just about have everything set up. I have set up IIS so that I can go to my localhost and I can test out web pages. I have PHP installed so that I can pull up the PHP setting pages.
The problem occurs when I try to bring up the MySQL database in PHP. I use this very simple PHP command:
<?php
$con = mysql_connect('localhost', 'testuser', 'testpassword');
?>
When I try to connect to the mysql database through PHP, I get this error:
Click here
I figure the problem must be in the settings that are in the php.ini. But it seems that my php.ini is set up for MySQL although it is not mentioned in the output when I query the phpinfo page with this code
Here is the result from this:
Click here
It looks that your php is missing mysql module
in php.ini make sure that you have correct extensions path eg.:
extension_dir = "C:\php\ext"
and make sure you have commented out:
extension=php_mysql.dll
extension=php_mysqli.dll
Which version of PHP are you running and are you sure you have MySQL installed and running on localhost with a username of "testuser" and a password of "testpassword" (and have you reloaded the privilege tables after creating those users)?
Have you got IIS configured to log errors into a file - does that provide any more information?
Create a new PHP file that contains the following:
<?php
phpinfo();
?>
This will helpy you to discover if MySQL has been compiled in properly etc. Can you access the MySQL server from the command line; using something similar to:
mysql -u testuser -ptestpassword testdatabase
If you get a prompt from MySQL, the service is running and we can better help from there.
Hope that helps a little!
Are you selecting the database? After your connect statement, you need to use:
mysql_select_db($databaseName, $conn);
Documentation here: http://php.net/mysql_select_db
What I would do is to download the complete package in one go (Server, MySQL and PHP). There are tons of good resources like WAMP or MAMP (for MAC users). Once you download the package the installation is just easy and you don't have to set anything as it does it automatically.
They also have phpMyAdmin where you can manage your database and its users and privileges.
Once you got your server running you can test that code that seems fine for me. Try running something like this:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected';
mysql_close($link);
?>
Cheers

Categories