9

I have developed a website named vishwasthali.org in PHP and have hosted it in IIS7 on a third-party server. Everything else works fine, except for the code for database access that I have written using MySQLi API. The code works fine on the localhost, but not on the server. Using phpinfo() on the server and my machine, I found that:

(1) On my machine (running in Windows 7 32-bit, Apache hosting environment), the PHP version is 5.3.8, and mysqli version is 5.0.8-dev, and

(2) On the server (running on Windows Server 2008, IIS7 hosting environment), the PHP version is 5.2.14 and mysqli version is 5.0.51a.


Here are the screen shots of phpinfo() on the server: enter image description here enter image description here


Here is the sample of the mysqli code.

session_start();
include_once 'code_files/conn.php';
$stmt = $mysqli->prepare('SELECT * FROM adminusers WHERE Username = ? AND `Password` = ?');
$stmt->bind_param('ss', $_POST['Username'], $_POST['Password']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0)
    ........
else
    ........

Using echo get_class($mysqli); does display mysqli. That means, the code does detect mysqli. But the statement $stmt is not prepared. Even the $mysqli->error; does not display any error, might be because of PHP being running on IIS.

Now I have two options, either change the code for database access to classic mysql API, or to upgrade the PHP and MySQLi versions on the server by downloading latest PHP binaries from the php.net. I won't like to go with the first option (that definitely works), hope you know why. The latter is not in my hands. Can't think anything more than these.

I suppose replacing the php_mysqli.dll on might upgrade the mysqli version, not sure. But what about PHP version?

What can I do in this case. Will an upgrade of PHP and MySQLi versions on the server solve my problem? If YES, can it affect other PHP websites running on the server?

Thanks in advance.

3
  • Can you elaborate on what is happening on the server? How is the code not working? Are you receiving an error message? Also on a side note, you should make sure to clean your input before passing it into bind_param to prevent SQL injection. Passing data directly from $_POST is a very risky proposition.
    – nageeb
    Jun 3, 2012 at 17:12
  • +1 for question clarity and wording effort
    – Justin T.
    Jun 3, 2012 at 17:13
  • @nageeb: Thanks for suggestion. When I use the statement echo ($stmt) ? "Statement Prepared" : "No Statement";, on localhost, it prints 'Statement Prepared', where as on server, it prints 'No Statement'. Simply put, the executing stops as the line where I am preparing the statement and a blank page is displayed (since the above code is in a separate file).
    – Kumar Kush
    Jun 4, 2012 at 5:39

3 Answers 3

8

All right. Here's the solution. Different PHP versions can be enabled and running side-by-side in IIS7. Here's the procedure that I have successfully tested on my machine. I am only briefing the process, considering that you are familiar with the IIS and FastCGI. If not, see the link: http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis'

  1. Download any different versions of non-thread safe zip packages with binaries of PHP from http://www.php.net/downloads.php.
  2. Extract them to separate folders in your hard-drive like C:\PHP5.2.14, C:\PHP5.4.3, etc.
  3. Open the php.ini files in all the folders and add/uncomment and change following settings in all of them.

    session.save_path = "C:\Windows\Temp"
    expose_php = Off
    ;; Choose any path here, but mostly preferred is this
    open_basedir = "C:\inetpub\wwwroot"
    extension_dir = "ext"
    fastcgi.impersonate = 1
    cgi.fix_pathinfo=1
    cgi.force_redirect = 0
    ;; You can chose different time zones for different web-sites
    date.timezone = "you time zone"
    ;; Enable any extensions that you want to per website, e.g.
    extension=php_mysql.dll
    extension=php_mysqli.dll

  4. Open IIS Manager. Add as many Handler Mappings as the number of PHP versions you want to install. Simply change the path of Executable in the Edit Module Mapping dialog box. Name the Mappings as convenient to identify them easily, like PHP 5.2.14 via FastCGI and PHP 5.4.3 via FastCGI.

  5. In the IIS section in IIS Manager, open FastCGI Settings. Here, check whether there are as many entries as the number of PHP versions you want to run. If not, click Add Applications from the right-pane. Select a php-cgi.exe from the folder of the required PHP version in the Full Path field in the 'Add FastCGI Application' dialog box. Click OK.

  6. Now, copy your website folder to C:\inetpub\wwwroot or whatever path you choose in open_basedir setting in php.ini. In the IIS Manager, expant Sites->Default Web Site from the left pane. Select the website that you want to target to a specific version of PHP.

  7. Open Handler Mapping, with a website selected from the left pane. You will find many mappings here. Look only for those entries that have been created by you (those which have path as *.php). Among them, retain the only one that you need for the particular web-site selected and Remove the others. This WILL NOT delete the mappings from you machine, but only from the web-site selected. You website will now run only on the PHP version whose Mapping you have retained. Do the same for any websites that you want to target to a specific PHP version. You can test it by running phpinfo() in the websites.

After you add new websites to wwwroot, they won't have any handler attached. Add a new handler targetting the desired PHP version. You are done.

This may not be the best procedure. But it worked for me. For verification, see this link:
http://learn.iis.net/page.aspx/246/using-fastcgi-to-host-php-applications-on-iis/#PHP_Versioning'

Hope it helps, in case someone needs this.

Regards

3

There are a few things quite wrong here!

a) The difference between your development environment and your live site is too large! At least use the same web server and the same PHP 5.x version!

b) NEVER publicly make your phpinfo() accessible, it is a very large security risk! A hacker can get loads of great information from it.

c) Even when using prepared statements you should sanitize your user data before you use it in a query!

I don't think your problem has anything to do with the different mysqli versions. Maybe you should first make sure you use the same web server and php version in both environments and then ask again with more code, (e.g. how you make the connection) if it still doesn't work.

Yes, updating PHP can affect other projects on the server but that just means you need to update these projects, which you should do anyways!

And no, downloading PHP from php.net is not the right way in your case. You should use tested and approved versions provided by the respective repositories.

4
  • Thanks for important tips. In reference to your statement "Maybe you should first make sure ....", should it really matter? After all, the syntax of the code is NOT going to change. Secondly, if updating PHP can affect other projects, can't I update PHP in my project by using some DLL's and referring them in .htaccess?
    – Kumar Kush
    Jun 4, 2012 at 5:35
  • It's not just about syntax, it's about configuration, extensions, etc. I can only repeat it one more time. It is a very bad idea!
    – markus
    Jun 4, 2012 at 5:51
  • Well, I suppose I found solution to the problem. Separate PHP handlers can be created for separate PHP versions in IIS, that can be used to host different websites. See this link 'learn.iis.net/page.aspx/246/…'. I have just installed PHP 5.4.3 in IIS on my local machine, which is newer than PHP version of my Apache installation. And my website works on this. Now I will download a lower version of the PHP and test what is written in the web-page. If it works, I will recommend same to the server owner.
    – Kumar Kush
    Jun 4, 2012 at 8:16
  • And the mysqli version in the new setup is 5.0.10.
    – Kumar Kush
    Jun 4, 2012 at 8:25
0

How to run multiple versions of PHP in IIS

Prerequisites

In this article, I assume that you have already installed IIS and, at the very least, have one PHP installation on your machine, with all the necessary configurations in place to support one of the installed PHP versions.

If not, follow the instructions How to Host PHP on Windows With IIS

But keep in mind that the Windows Platform Installer is retired on December 31, 2022 and you need to download from PHP official website and modify the php.ini.

I consider that you have two PHP C:\PHP7.4 and C:\PHP8.2

Configure IIS and add PHP versions

Open IIS manager

  • In the left pane, navigate to your server node
  • Under the IIS section, open FastCGI Settings
  • Click Add Applicaion... and select the PHP for the both version

Add both PHP

Configure the sites

In the left panel of IIS, click on the site and add two sites, each pointed to a different directory (web site root) for two different versions of PHP.

enter image description here

For each site click bindings and give different port number (ex: 740 and 820)

For each site click Handler Mappings then Add Module Mapping...

  • Request path: *.php
  • Module: FastCgiModule
  • Executable: Browse to the correct PHP version (e.g., C:\PHP7.4\php-cgi.exe)
  • Enter a name
  • Request Restriction > File or Folder

Test

On each root create the index.php file contains:

<?php
phpinfo();

Open the browser:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.