Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

[SOLVED] PHP 7 and mssql_connect

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
4 posts • Page 1 of 1
Author
Message
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

[SOLVED] PHP 7 and mssql_connect

  • Quote

Post by Vieri » Sat Oct 14, 2017 8:29 pm

Hi,

I used connect to an SQL server with the PHP functions mssql_*.

For some reason, they got removed, and now I need to update my PHP code somehow.

I've vaguely heard of sqlsrv, but it seems to require an ODBC driver which is not yet available for Linux.

This is my PHP version:

Code: Select all

dev-lang/php-7.0.19::gentoo was built with the following:
USE="acl apache2 berkdb bzip2 calendar cli crypt ctype curl embed fileinfo filter ftp gd gdbm hash iconv ipv6 json kerberos ldap mssql mysql mysqli nls odbc opcache phar posix postgres readline session simplexml soap sockets ssl tokenizer unicode xml xmlreader xmlrpc xmlwriter zip zlib -bcmath -cdb -cgi -cjk -coverage -debug -enchant -exif (-firebird) -flatfile -fpm -gmp -imap -inifile -intl -iodbc -ldap-sasl -libedit (-libressl) -mhash -oci8-instant-client -pcntl -pdo -phpdbg -qdbm -recode (-selinux) -sharedmem -snmp -spell -sqlite -systemd -sysvipc -threads -tidy -truetype -wddx -webp -xpm -xslt" ABI_X86="(64)"
Can anyone please let me know if there's an easy way to connect to an MS SQL server from a Gentoo PHP script like it was before with mssql_*?

Maybe there's a missing Gentoo ebuild for http://pecl.php.net/package/sqlsrv?

Thanks,

Vieri
Last edited by Vieri on Mon Oct 16, 2017 11:38 am, edited 1 time in total.
Top
kitsunenokenja
Tux's lil' helper
Tux's lil' helper
Posts: 132
Joined: Tue Jan 20, 2004 7:39 pm

  • Quote

Post by kitsunenokenja » Sun Oct 15, 2017 1:04 am

The old-fashioned procedural functions for database calls were deprecated and by the release of version 7, were stripped out entirely as a result. You need to migrate to using PDO class. Also, years ago there used to be some sort of driver included to connect to MSSQL servers but not anymore. You will need to acquire the Linux PHP7 driver separately from the vendor as well as enable the appropriate use flags when compiling PHP to use it.
ProtonMail - Free encrypted e-mail from Switzerland
Let's Encrypt - Free SSL cert organisation
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Mon Oct 16, 2017 10:13 am

That's a bummer.
OSS is always changing, and that's good. However, backward compatibility is not it's strength.

I was wondering if I could find an easier way out by using freetds and unixodbc (then use odbc_* functions in PHP).

FreeTDS seems OK:

Code: Select all

# TDSVER=7.1 tsql -S sqlserver -U my_user -P my_pass -D my_db
locale is "C"
locale charset is "ANSI_X3.4-1968"
using default charset "ISO-8859-1"
Setting my_db as default database in login packet
1>  select  ID from pro_Prof where ( Active = 1 and User = 'me' )
2> go
ID
X111000K
(1 row affected)
1> quit
unixODBC:

Code: Select all

# cat /etc/unixODBC/odbcinst.ini
[FreeTDS]
Description=Freetds
Driver=/usr/lib64/libtdsodbc.so
UsageCount=1
Threading=2

Code: Select all

# cat /etc/unixODBC/odbc.ini
[sqlserver]
Driver = FreeTDS
Description = MS SQL server
Trace = No
Server = sqlserver
Port = 1433
Database =  my_db
TDS_Version = 7.1

Code: Select all

# odbcinst -i -d -f /etc/unixODBC/odbcinst.ini
odbcinst: Driver installed. Usage count increased to 2.
    Target directory is /etc/unixODBC

Code: Select all

# odbcinst -q -d
[FreeTDS]

Code: Select all

# isql -v sqlserver my_user my_pass
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>
I can run SQL queries from the isql prompt.

However, my PHP script doesn't seem to work:

Code: Select all

# cat test.php
<?php
$con = odbc_connect("sqlserver", "my_user", "my_pass");
if ($con === false) die("ERROR: cannot connect to database.");

$sql= "select  ID from pro_Prof where ( Active = 1 and User = 'me' )";
$rs = odbc_exec ($con, $sql);
if ($rs === false) die("ERROR: cannot query database.");
$numrows = odbc_num_rows($rs);

echo "numrows = ".$numrows."\n";
$row = odbc_fetch_array($rs);
if (!$row) die("ERROR: cannot fetch table data.");
echo "row: ".$row[0]."\n";
odbc_free_result($rs);
odbc_close($con);
?>
The output I see on a browser is:

Code: Select all

numrows = -1
row: 
Nothing in the logs:

Code: Select all

# tail -n 200 /var/log/apache2/php_error_log
I don't understand why apache/PHP can connect to the SQL server (apache user can obviously find the DSN), but it doesn't retreive the expected records.

I'm just one step from getting all of this to work...
How can I test this further?
Top
Vieri
l33t
l33t
Posts: 935
Joined: Sun Dec 18, 2005 12:26 pm

  • Quote

Post by Vieri » Mon Oct 16, 2017 11:38 am

Hmm... Never mind. My PHP code was wrong. I was accessing the array by integer, not by name, but the array is associative.
So using

Code: Select all

$row['ID']
works fine.

Still, odbc_num_rows always returns -1, no matter what so I guess I can't count on the result of that call.
Top
Post Reply

4 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic