MSSQL connection using PHP - php

Iam so fresh with PHP so this problem so big to me. I dont know why and where my mistake is. Based on my research to connect to MSSQL database by using this code:
<?php
$run = mssql_connect('dev-svr05','sa','P#55w0rd', 'orlig_sm_dev');
if ($run)
{
echo "Connection OK";
}
else
{
echo "Connection Failed";
}
?>
But when i run this code i got this error message:
PHP Fatal error: Call to undefined function mssql_connect() in C:\Inetpub\wwwroot\phpscript\save_mssql.php on line 5
I using the same code to connect to MYSQL and its success but not with MSSQL. Can anybody please tell me why this is happen? Thank You

I would advice you download the sql server binaries from windows and use PDO.
try {
//In some occasions you only need to define IP/Hostname and you can forgo the \SQLEXPRESS part
$db = new PDO( "sqlsrv:Server=HOSTNAME\SQLEXPRESS;Database=DATABASENAME","USERNAME","PASSWORD");
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// set this to your primary database
$db->query("USE DATABASENAME");
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server".$e );
}

Fourth parameter is expecting a true/false. Refer the below link and try:
http://php.net/manual/en/function.mssql-connect.php

Try "sqlsrv_connect" instead of "mssql_connect"

Related

how to access sqlite3 database from cpanel using php

hi friends ,
class MyDB extends SQLite3
{
function __construct()
{
$this->open('/var/cpanel/eximstats_db.sqlite3');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
i have using eximstats db from server. while on updating my server the eximstats db get got under the SQLite3. i am new one to SQLite3 even though I have tried many more times access that db using the above php code but never i got result . please help me to improve this coding.
Is this code is correct . while running this i got "Fatal error: Uncaught exception 'Exception' with message 'Unable to open database: unable to open database file' "
thank you
You can simply use PHP PDO interface to access your SQLite3 database.
$db = new PDO('sqlite:/var/cpanel/eximstats_db.sqlite3')
PDO_SQLITE DSN
To access a database on disk, append the absolute path to the DSN prefix.
Just make sure that you have the PDO Driver for SQLite 3.x installed on your system.
Now to select a table just do:
$result = $db->query('SELECT * FROM tablename');
foreach( $result as $row ) {
print_r( $row );
}

PDO Sqlite error 5: database locked

I've been trying to use a sqlite database (php with PDO), but have been running into a problem. Generally the commands work, and everything is fine (including storing files), but for some reason when I run these two commands (which have been simplified), I get the error
SQLSTATE[HY000]: General error: 5 database is locked
I've tried for a while, but have been unable to fix whatever is wrong. The code is below.
Things I've done:
Tried to put sleep(2) between commands
Found out that commenting either of the commands out will cause the error not to happen (which doesn't really help, as both commands must run)
Note that (unlike other problems I saw while looking at similar questions) the database operates correctly in other cases.
$db = new MyDB();
$STH = $db->catchMistakes('SELECT PASSWORD FROM USERS WHERE USERNAME = ?', "test");
$STH->fetchColumn();
$db->catchMistakes("UPDATE ISSUES SET NAME = ? WHERE NUM = ?", ["test", "1"]);
And here's the code for MyDB
public function catchMistakes($cmd, $params = []) {
if (!is_array($params)) {
$params = [$params];
}
try {
$DBH = new PDO("sqlite:" . DB);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$DBH->beginTransaction();
$query = $DBH->prepare($cmd);
$toReturn = $query->execute($params);
$DBH->commit();
return $query;
}
catch(PDOException $e) {
$DBH->rollback();
$error = $e->getMessage();
exit;
}
}
Sorry if there's a simple fix, I'm pretty new at this. Any help would be greatly appreciated.
You can use closeCursor() method on a PDOStatement object to free the connection to the database so the statement can be executed. You can refer to the PHP manual.

Connecting to Oracle database from PHP

I have an Oracle database that I am trying to connect to.
For some reason when I try the following code:
<?php
include "header.php";
// simply attempt to connect to the database
/* If you are connecting to the Oracle database, the credentials are as follows:
* Username: ********
* Password: ********
* Hostname: **********
* Port: 1521
* Service name: ***********
*/
$oracleConnect = true;
if ($oracleConnect)
{
echo 'Attempting connection...<br>';
$connection = null;
try
{
$connection = oci_connect('user',
'pass',
'user#//hostname:1521/dbname');
}
catch (Exception $e)
{
echo $e->getMessage();
}
if (!$connection)
{
echo '<p>Something is wrong.</p>';
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// if the connection has been established
else
{
// tell the user and close it (this is a test)
echo 'Connection established!!';
oci_close($connection);
}
}
else
{
$connection = new mysqli('host', 'user', 'password', 'database');
echo ($connection) ? 'Database connection successful!' : 'Could not connect.';
}
include "footer.php";
?>
When I try the above code, I get the "Attempting connection..." to print, but nothing else. It is supposed to print something else regardless. What could possibly be going wrong?
I think the problem is the user# part of your connection string. I dont think thats necessary as oci_connect has a user name parameter. I could be wrong, ive never used oracle from php before, but the docs on oci connections would also seem to indicate that:
To use the Easy Connect naming method, PHP must be linked with Oracle 10g or greater Client libraries. The Easy Connect string for Oracle 10g is of the form: [//]host_name[:port][/service_name]. From Oracle 11g, the syntax is: [//]host_name[:port][/service_name][:server_type][/instance_name]. Service names can be found by running the Oracle utility lsnrctl status on the database server machine.
Also oci_connect does not throw an exception as far as i can tell so your try/catch is useless unless you planned on throwing your own when it returns false.

error on mysqli::connect_errno

I'm trying to start working with mysqli and OOP, but it isn't working out that well. I can't get my mysqli methods working starting with a simple connect_errno method.
Can anyone help me out?
The connection to the database seems to be made, since I tried it with an other Password (access denied). With the correct Password I got the following error:
Fatal error: Call to undefined method mysqli::connect_errno() in
C:\xampp2\htdocs\badeendenrace\db\db.php on line 7
<?php
$dbhost='localhost';
$dbuser='Tjerk';
$dbdata='badeendenrace';
$dbpw='123test';
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if ($db->connect_errno())
{
echo "Connection failed: ".$db->connect_error()."\n";
exit();
}
?>
I'm using the latest version of Xampp (Reïnstalled it today) with PHP version 5.5.6
As Rocket Hazmat said, connect_errno isn't a function. The same to connect_error. So just take away the round brackets after both in your code.
if ($db->connect_errno)
{
echo "Connection failed: ".$db->connect_error."\n";
exit();
}
It seems that you have confused the object oriented function with the procedural. You mention OOP so I assume you want the OOP version.
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if ($db->connect_errno) //no parenthesis
{
echo "Connection failed: ".$db->connect_error."\n"; //no parenthesis
exit();
}
For procedural
$db = new mysqli($dbhost,$dbuser,$dbpw,$dbdata);
if (!$db)
{
echo "Connection failed: ".mysqli_connect_error(); //parenthesis
exit();
}

PHP SQLite3 error?

How do I know if there's an error if I did $db = new SQLite3("somedb.db"); in PHP? Right now the $db doesn't really give me any sort of error?
I can check for file existance, but I'm unsure if there could be any other errors when I open a connection.
You should enable exceptions and instantiate in a try-catch block.
It is not obvious from the documentation but if you use the constructor to open the database it will throw an exception on error.
Further if you set the flag SQLITE3_OPEN_READWRITE in the second argument then it will also throw an exception when the database does not exist (rather than creating it).
class Database extends SQLite3
{
function __construct($dbName)
{
$this->enableExceptions(true);
try
{
parent::__construct($dbName, SQLITE3_OPEN_READWRITE );
}
catch(Exception $ex) { die( $ex->getMessage() ); }
}
Try:
echo $db->lastErrorMsg();

Categories