How can I use php's built in http server + mysql? - php

I'm trying to connect to mysql server using the following code:
$connection = mysql_connect("localhost", "username", "password");
if(!$connection)
{
die('Could not connect: '. mysql_error());
}
And I'm getting "Call to undefined function mysql_connect()". Why is that? isn't this function built in in php?
I'm using Ubuntu 12.04 + php 5.4.11 + php's built in http server.

First do not use mysql_* functions as those functions are deprecated
and second you need to install php5-mysql
sudo apt-get install php5-mysql
and then you can use mysqli and also PDO (needs to be installed too).

You must enable the extension.
Be aware that this API is deprecated.

Related

Fatal error display when run php code to connect to database

I got this problem when I run php code to connect with database in mysql.. Can anybody help me? I'm stuck.
Error:
fatal error call to undefined function mysql_connect() in php
Php code:
?php
mysql_connect("localhost","root","") or die("Could not connect to database");
mysql_select_db("dadadsdb") or die("Could not select database");
?>
mysql_* functions are deprecated in newer PHP versions and in PHP 7 they are just gone. If you are using PHP 5.x you could install these modules by sudo apt-get install php-mysql (sometimes it is sudo apt-get install php5-mysql but I recommend you to use mysqli or PDO.
Use mysqli instead of mysql.
$con = mysqli_query("host","username","password","database");
Mysql has been deprecated in PHP 5.5 for various reason and it can be replaced with MySQLi OOP(Object Oriented Programming) or the normal procedural style.
You can also use PDO that is more secure than MySQL.

SSH2 Connect Issues

I am using Laravel framework with homestead. (Latest Version) and i am trying to use ssh2_connect. I have installed the extension
sudo apt install php-ssh2
and i have tested that its working
php -m |grep ssh2
i am seeing
ssh2
i am assuming that this is correct but when i try and make a connection i get the following error.
Call to undefined function App\Http\Controllers\ssh2_connect()
This is the code i am executing
$connection = ssh2_connect($request->sever_ip, 2222);
if (!$connection) die('Connection failed');
Would someone point me in the right direction?
Because you are in the namespace of the Controllers you might need to use:
$connection = \ssh2_connect($request->sever_ip, 2222);
if (!$connection) die('Connection failed');

wamp php5.3.5 mssql_connect() Fatal error

I am using WampServer Version 2.2, php5.4.3 , Apeache2.2.22 I could not use mssql_connect(), "mssql_connect() Fatal error: Call to undefined function mssql_connect()" I went through googling but still not found solution.
It is always better to use PDO which supports ms sqlserver and other databases, you can use the syntax
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
or you can refer the documentation https://secure.php.net/manual/en/pdo.connections.php
You don't have the MS SQL Drivers installed. You can check this with phpinfo();
On Linux you need mssql.so or sybase.so With debian its apt-get install php5-sybase
For windows take a look here
Drivers need to be configured for PHP to find the function
mssql_...
You could also look at PDO DB classes as they can connect to any DBS, you need the drivers installed tho.

Connect to SQL Server from CakePHP 3 on Ubuntu 12.04 LTS

My setup was working on Windows but I recently switched to Ubuntu 12.04 LTS and now it won't connect. When I load a page where I need to talk to SQL Server, I get this error:
Database driver Cake\Database\Driver\Sqlserver cannot be used due to a
missing PHP extension or unmet dependency
It is obvious that CakePHP can't find the SQL Server PDO driver.
I found many old tutorials to help me but I took the most recent (I want absolutely to be able to use PDO with my CakePHP website). This is the tutorial I followed.
Using the terminal, I can access the database with this command
sqlcmd -S my.sql.server.com -U username
What do I need to do to connect to this sql server database from my ubuntu install with CakePHP 3.x?
Update (October 17th 2016):
Microsoft recently released a preview version (on their msphpsql repo) of pdo_sqlsrv and sqlsrv for Linux. It worked successfully for me on a Ubuntu 16.04.1 LTS (Xenial Xerus) virtual machine running php7.
Original answer:
As #AD7six said:
CakePHP's sql server driver is for using PDO_SQLSRV; If you need to use ODBC, there isn't one in the core.
PDO_SQLSRV is working if you are on Windows. CakePHP has a driver that support it natively.
If you want to talk to a SQL Server Database from Linux, you have to use the ODBC driver. You can install the driver using this tutorial which helped me a lot. After that, you will be able to connect to your database using PDO (not CakePHP):
try {
$query = new PDO(
"odbc:Driver={SQL Server};Database=[Database name]; Server=[Hosame]",
"[Username]",
"[Password]");
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
Thanks everybody for your help. It could be great to find/code a CakePHP 3 database driver for odbc.
The new version of CakePHP supports PDO-sqlsrv which is new the MS driver that supports PHP 7 and higher.
http://php.net/manual/en/ref.pdo-sqlsrv.php
It also allows you to connect to Azure. Example:
//Establishes the connection
$conn = new PDO( "sqlsrv:server=$serverName ; Database = $database", $uid, $pwd);

PHP application connect with external firebird database

I have an Ubuntu server (LAMP)
Now I want to connect with an external firebird database. How can I do that?
Can I install a firebird driver? How can I install a firebird driver?
Do I have to modify my php.ini file?
I do not use PDO
#apt-get install php5-interbase
You do not need to install firebird on local machine
PHP sample code
$dbh = ibase_pconnect("remote_server_ip:db_file", "user_name", "user_password") or die('die message');
$q = ibase_query($dbh, "select * from some_table");
while ($r = ibase_fetch_object($q)) {
$some_value = $r->SOME_FIELD;
echo $some_value;
}

Categories