title: How does one build the mysql extension for PHP 5.6?
body: <p>I am working to install PHP 5.6 and 7.0 side-by-side to facilitate migrating from PHP 5 to 7.</p>
  
  <p>I am using Ondřej Surý's Ubuntu PPA, <a href="https://launchpad.net/~ondrej/+archive/ubuntu/php" rel="nofollow noreferrer">https://launchpad.net/~ondrej/+archive/ubuntu/php</a> , which provides co-installable versions of PHP 5.6 and 7.0.</p>
  
  <p>Both PHP versions work beautifully side-by-side, but there's one snag: I'm unable to build the <code>mysql</code> extension for PHP 5.6 (even though I'm able to build it for 7.0).</p>
  
  <p>(I'm well aware that the <code>mysql</code> extension is obsolete and that it should be abandoned in favor of a more modern alternative. Yet, it is unreasonable for me to expect customers whose legacy applications depend on it to refactor their code while PHP 5.6 is still supported. So, I'm left with little choice in the matter...)</p>
  
  <p>For anyone not familiar with this co-installable PPA, the version number is appended to the binaries, so the paths to the various PHP executables are as follows:</p>
  
  <pre><code># ls -lah /usr/bin | grep "php"
  -rwxr-xr-x  1 root   root     13K Dec 31 03:24 dh_php
  lrwxrwxrwx  1 root   root      21 Feb 23 09:51 php -&gt; /etc/alternatives/php
  -rwxr-xr-x  1 root   root    3.6M Feb 23 06:37 php5.6
  -rwxr-xr-x  1 root   root    3.5M Feb 23 06:58 php7.0
  lrwxrwxrwx  1 root   root      25 Feb 23 10:06 php-cgi -&gt; /etc/alternatives/php-cgi
  -rwxr-xr-x  1 root   root    3.6M Feb 23 06:37 php-cgi5.6
  -rwxr-xr-x  1 root   root    3.4M Feb 23 06:58 php-cgi7.0
  lrwxrwxrwx  1 root   root      28 Feb 23 10:06 php-config -&gt; /etc/alternatives/php-config
  -rwxr-xr-x  1 root   root    4.1K Feb 23 06:37 php-config5.6
  -rwxr-xr-x  1 root   root    4.2K Feb 23 06:58 php-config7.0
  lrwxrwxrwx  1 root   root      24 Feb 23 10:06 phpize -&gt; /etc/alternatives/phpize
  -rwxr-xr-x  1 root   root    4.7K Feb 23 06:37 phpize5.6
  -rwxr-xr-x  1 root   root    4.6K Feb 23 06:58 phpize7.0
  </code></pre>
  
  <p>Here's what I've tried:</p>
  
  <pre><code># cd ~
  
  # git clone https://git.php.net/repository/pecl/database/mysql.git
  
  # cd mysql
  
  # phpize5.6
  Configuring for:
  PHP Api Version:         20131106
  Zend Module Api No:      20131226
  Zend Extension Api No:   220131226
  
  # ./configure --with-php-config=/usr/bin/php-config5.6
  checking for grep that handles long lines and -e... /bin/grep
  checking for egrep... /bin/grep -E
  checking for a sed that does not truncate output... /bin/sed
  checking for cc... cc
  checking whether the C compiler works... yes
  checking for C compiler default output file name... a.out
  checking for suffix of executables...
  checking whether we are cross compiling... no
  checking for suffix of object files... o
  checking whether we are using the GNU C compiler... yes
  checking whether cc accepts -g... yes
  checking for cc option to accept ISO C89... none needed
  checking how to run the C preprocessor... cc -E
  checking for icc... no
  checking for suncc... no
  checking whether cc understands -c and -o together... yes
  checking for system library directory... lib
  checking if compiler supports -R... no
  checking if compiler supports -Wl,-rpath,... yes
  checking build system type... x86_64-unknown-linux-gnu
  checking host system type... x86_64-unknown-linux-gnu
  checking target system type... x86_64-unknown-linux-gnu
  checking for PHP prefix... /usr
  checking for PHP includes... -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib
  checking for PHP extension directory... /usr/lib/php/20131226
  checking for PHP installed headers prefix... /usr/include/php/20131226
  checking if debug is enabled... no
  checking if zts is enabled... no
  checking for re2c... no
  configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
  checking for gawk... gawk
  checking for MySQL support... yes, shared
  checking for specified location of the MySQL UNIX socket... no
  checking for the location of libz... no
  checking for MySQL UNIX socket location... /var/run/mysqld/mysqld.sock
  checking how to print strings... printf
  checking for a sed that does not truncate output... (cached) /bin/sed
  checking for fgrep... /bin/grep -F
  checking for ld used by cc... /usr/bin/ld
  checking if the linker (/usr/bin/ld) is GNU ld... yes
  checking for BSD- or MS-compatible name lister (nm)... /usr/bin/nm -B
  checking the name lister (/usr/bin/nm -B) interface... BSD nm
  checking whether ln -s works... yes
  checking the maximum length of command line arguments... 1572864
  checking whether the shell understands some XSI constructs... yes
  checking whether the shell understands "+="... yes
  checking how to convert x86_64-unknown-linux-gnu file names to x86_64-unknown-linux-gnu format... func_convert_file_noop
  checking how to convert x86_64-unknown-linux-gnu file names to toolchain format... func_convert_file_noop
  checking for /usr/bin/ld option to reload object files... -r
  checking for objdump... objdump
  checking how to recognize dependent libraries... pass_all
  checking for dlltool... no
  checking how to associate runtime and link libraries... printf %s\n
  checking for ar... ar
  checking for archiver @FILE support... @
  checking for strip... strip
  checking for ranlib... ranlib
  checking for gawk... (cached) gawk
  checking command to parse /usr/bin/nm -B output from cc object... ok
  checking for sysroot... no
  checking for mt... mt
  checking if mt is a manifest tool... no
  checking for ANSI C header files... yes
  checking for sys/types.h... yes
  checking for sys/stat.h... yes
  checking for stdlib.h... yes
  checking for string.h... yes
  checking for memory.h... yes
  checking for strings.h... yes
  checking for inttypes.h... yes
  checking for stdint.h... yes
  checking for unistd.h... yes
  checking for dlfcn.h... yes
  checking for objdir... .libs
  checking if cc supports -fno-rtti -fno-exceptions... no
  checking for cc option to produce PIC... -fPIC -DPIC
  checking if cc PIC flag -fPIC -DPIC works... yes
  checking if cc static flag -static works... yes
  checking if cc supports -c -o file.o... yes
  checking if cc supports -c -o file.o... (cached) yes
  checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
  checking whether -lc should be explicitly linked in... no
  checking dynamic linker characteristics... GNU/Linux ld.so
  checking how to hardcode library paths into programs... immediate
  checking whether stripping libraries is possible... yes
  checking if libtool supports shared libraries... yes
  checking whether to build shared libraries... yes
  checking whether to build static libraries... no
  configure: creating ./config.status
  config.status: creating config.h
  config.status: executing libtool commands
  </code></pre>
  
  <p>So far, so good.</p>
  
  <p>The problem occurs during <code>make</code>. I've included only a portion of the output because I'm certain that the same issues appear repeatedly.</p>
  
  <pre><code># make
  /bin/sh /root/mysql/libtool --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/mysql -DPHP_ATOM_INC -I/root/mysql/include -I/root/mysql/main -I/root/mysql -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib  -DHAVE_CONFIG_H  -g -O2   -c /root/mysql/php_mysql.c -o php_mysql.lo
  libtool: compile:  cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -I. -I/root/mysql -DPHP_ATOM_INC -I/root/mysql/include -I/root/mysql/main -I/root/mysql -I/usr/include/php/20131226 -I/usr/include/php/20131226/main -I/usr/include/php/20131226/TSRM -I/usr/include/php/20131226/Zend -I/usr/include/php/20131226/ext -I/usr/include/php/20131226/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/mysql/php_mysql.c  -fPIC -DPIC -o .libs/php_mysql.o
  In file included from /root/mysql/php_mysql.c:63:0:
  /root/mysql/php_mysql_structs.h:114:2: error: unknown type name 'zend_resource'
    zend_resource *default_link;
    ^
  /root/mysql/php_mysql_structs.h:115:2: error: unknown type name 'zend_long'
    zend_long num_links,num_persistent;
    ^
  /root/mysql/php_mysql_structs.h:116:2: error: unknown type name 'zend_long'
    zend_long max_links,max_persistent;
    ^
  /root/mysql/php_mysql_structs.h:117:2: error: unknown type name 'zend_long'
    zend_long allow_persistent;
    ^
  /root/mysql/php_mysql_structs.h:118:2: error: unknown type name 'zend_long'
    zend_long default_port;
    ^
  /root/mysql/php_mysql_structs.h:122:2: error: unknown type name 'zend_long'
    zend_long connect_errno;
    ^
  /root/mysql/php_mysql_structs.h:123:2: error: unknown type name 'zend_long'
    zend_long connect_timeout;
    ^
  /root/mysql/php_mysql_structs.h:124:2: error: unknown type name 'zend_long'
    zend_long result_allocated;
    ^
  /root/mysql/php_mysql_structs.h:125:2: error: unknown type name 'zend_long'
    zend_long trace_mode;
    ^
  /root/mysql/php_mysql_structs.h:126:2: error: unknown type name 'zend_long'
    zend_long allow_local_infile;
    ^
  /root/mysql/php_mysql.c:119:2: error: unknown type name 'zend_resource'
    zend_resource *active_result_res;
    ^
  /root/mysql/php_mysql.c:414:32: error: unknown type name 'zend_resource'
   static void _free_mysql_result(zend_resource *rsrc)
                                  ^
  /root/mysql/php_mysql.c:425:40: error: unknown type name 'zend_resource'
   static void php_mysql_set_default_link(zend_resource *link)
                                          ^
  /root/mysql/php_mysql.c: In function 'php_mysql_select_db':
  /root/mysql/php_mysql.c:372:57: error: request for member 'ptr' in something not a structure or union
      _mysql_result = (MYSQL_RES *)mysql-&gt;active_result_res-&gt;ptr; \
  </code></pre>
  
  <p>It seems that the <code>mysql</code> extension's source code is no longer compatible with the PHP 5.6 API.</p>
  
  <p>Given that the <code>mysql</code> extension compiles perfectly fine under PHP 7.0 on the same server, one must wonder if the extension source was updated to work with 7.0.</p>
  
  <p>I attempted to find earlier versions of the <code>mysql</code> extension's code, but <a href="https://pecl.php.net/package/mysql" rel="nofollow noreferrer">https://pecl.php.net/package/mysql</a> refers users only to <a href="http://git.php.net/?p=pecl/database/mysql.git;a=summary" rel="nofollow noreferrer">http://git.php.net/?p=pecl/database/mysql.git;a=summary</a> , which appears not to have any meaningful version history, such as an older branch that is designed to work with PHP &lt; 7.0.</p>
  
  <p>What else might I try? Maybe I am overlooking something silly...</p>
  
  <p>Thank you for any suggestions!</p>
  
  <h2>UPDATE: Solution</h2>
  
  <p>Per the Accepted Answer, I was fetching PHP 7 source when I needed PHP 5.6 source.</p>
  
  <p>This was the solution (be sure to adjust your own versions, paths, and service commands as necessary):</p>
  
  <pre><code># git clone --depth=1 -b PHP-5.6 https://git.php.net/repository/php-src.git
  # cd ./php-src/ext/mysql
  # phpize5.6
  #./configure --with-php-config=/usr/bin/php-config5.6
  # make
  # make install
  # printf "; configuration for php mysql module\n; priority=20\nextension=mysql.so" &gt; /etc/php/mods-available/mysql.ini
  # ln -s /etc/php/mods-available/mysql.ini /etc/php/5.6/fpm/conf.d/20-mysql.ini
  # service php5.6-fpm reload
  </code></pre>