PDA

View Full Version : PHP and SQL



AllSystemGo
February 21st, 2007, 10:48 AM
I have a SQL DB on a windows server that I would need to access. But correct me if I'm wrong PHP doesn't work with SQL only MySql... So is there any way I could access to data on my SQL DB??

Cheers

bwh2
February 21st, 2007, 11:46 AM
i assume you mean SQL Server, which PHP works just fine with.

AllSystemGo
February 21st, 2007, 12:20 PM
Ok so you mean I am able to connect to a SQL Server DB with PHP...

Then how am I suppose to do it. I keep getting an error
Lost connection to MySQL server during query when I do the



$con = mysql_connect("server addr");


Thanks

JoshuaJonah
February 21st, 2007, 12:23 PM
ummm... that connection string says "mySQL". You should have a look at www.php.net they have alot of info on connection strings including this easily-findable little nugget: http://www.php.net/manual/en/function.mssql-connect.php

bwh2
February 21st, 2007, 12:24 PM
well, you can't use mysql_* functions on a sql server db. i recommend using MDB2 from PEAR (http://pear.php.net/manual/en/package.database.mdb2.php)

JoshuaJonah
February 21st, 2007, 12:26 PM
yeah thats probably a better idea if your familiar with pear packages

AllSystemGo
February 21st, 2007, 12:40 PM
Thank you very much for your help guys. I really appreciate.

I'll post you my finding.

Cheers

AllSystemGo
February 21st, 2007, 07:57 PM
Ok I installed your MDB2 and now I get that error



<html>
<body>
<?php
require_once 'MDB2.php';

$hostname = "10.128.0.10";
$con =& MDB2::connect("mssql://test:test@10.128.0.10/CCM0304");

if (PEAR::isError($con))
{
die('Could not connect: ' . $con->getMessage());
}
?>
</body>
</html>


The error I get is Could not connect: MDB2 Error: not found

This is what I was doing in C# to access my DB:



"Network Library=DBMSSOCN;Data Source=10.128.0.10;database=CCM0304;Integrated Security=TRUE"


What am I doing wrong??

bwh2
February 21st, 2007, 08:03 PM
hmmm. doesn't seem to be installed correctly. try using DB. DB comes as a default install with many PHP installs.

AllSystemGo
February 21st, 2007, 08:36 PM
OK I installed MDB2 the right way now I think. And I didn't install the pear.php module.

So what I did, I installed the package pear.php with synaptic, BTW I'm using Ubuntu, then I installed MDB2



pear install MDB2


I got that message -> "install ok: channel://pear.php.net/MDB2-2.3.0"

So I guess I am now setup properly but I have the same message... What did I miss out on??

I didn't install any mssql driver cause it doesn't seem to work properly!!

Thanks for your help.

Cheers

borrob
February 22nd, 2007, 04:47 AM
i've found this part for the code to connect
look at the site:

http://peardoc.m-takagi.org/en/package.database.mdb2.intro-connect.html

<?php
require_once 'MDB2.php';

$dsn = array(
'phptype' => 'pgsql',
'username' => 'someuser',
'password' => 'apasswd',
'hostspec' => 'localhost',
'database' => 'thedb',
);

$options = array(
'debug' => 2,
'portability' => MDB2_PORTABILITY_ALL,
);

// uses MDB2::factory() to create the instance
// and also attempts to connect to the host
$mdb2 =& MDB2::connect($dsn, $options);
if (PEAR::isError($mdb2)) {
die($mdb2->getMessage());
}
?>

AllSystemGo
February 22nd, 2007, 09:18 AM
Thank you for your help. I think I'm getting closer. I now have that message


Warning: MDB2::include_once(MDB2/Driver/mssql.php) [function.MDB2-include-once]: failed to open stream: No such file or directory in /var/www/apache2-default/intranet/MDB2.php on line 330
Warning: MDB2::include_once() [function.include]: Failed opening 'MDB2/Driver/mssql.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/apache2-default/intranet/MDB2.php on line 330
MDB2 Error: not found


I think that I'm missing the Driver for MSSQL

AllSystemGo
February 22nd, 2007, 04:14 PM
Here is my latest update.



<html>
<body>
<?php
require_once 'MDB2.php';
require_once 'mssql.php';

$engine = 'mssql';
$user = 'administrator';
$pwd = 'fer45yu89';
$host = '10.128.0.10';
$database = 'CCM0304';
$dsn = array(
'phptype' => 'mssql',
'username' => 'administrator',
'password' => 'fer45yu89',
'hostspec' => '10.128.0.10',
'database' => 'CCM0304',
);
$options = array(
'debug' => 2,
'portability' => MDB2_PORTABILITY_ALL,
);
echo 'test1';
// uses MDB2::factory() to create the instance
// and also attempts to connect to the host
$mdb2 =& MDB2::connect($dsn, $options);
//$mdb2 =& MDB2::connect('mssql://administrator:fer45yu89@10.128.0.10/CCM0304');
$mdb2->setFetchMode(MDB2_FETCHMODE_ASSOC);
echo 'test2';
if (MDB2::isError($mdb2))
{
die($mdb2->getMessage());
}
$res = $mdb2->queryAll("SELECT * FROM Pilot");
if (PEAR::isError($res))
{
die($res-getMessage());
}
echo $res->numRows();
while (($row = $res->fetchRow(MDB2_)))
{
echo $row['pkid'] . "\n";
}
$mdb2->disconnect();
?>
</body>
</html>


with that now all I get is a blank page with test1 on it. It's like it doesn't even get to the test2. I don't know if it geting stuck in the connect function but it seems like it..

You guys see anything wrong??

Cheers

kortexvfd
February 22nd, 2007, 04:29 PM
Have you checked http://us3.php.net/manual/en/ref.mssql.php

AllSystemGo
February 26th, 2007, 06:14 PM
Ok so I guess it's because I didn't install my package right cause right now it's working fine.

Here is the perfect Tutorial to do just that, hope it helps as much as it helped me!

http://panthar.org/2006/06/15/php-with-mssql-on-ubuntu-606

And thank you to everyone who help me. I'll come back with more questions!!!

Cheers!