I have a project which has been developed in php 4 now it has to be upgraded to php 7.
The database is connected via odbc connection. When i try to connect to the database and run the index file nothing seems to pop up.
What i have done is i have converted my database into mysql and tried to connect to the database and it is connecting .
How do i change the complete odbc to mysql?
Please find below the odbc dataconnection file and let me know how to i connect it with mysqli.
<?php
//##################### ODBC CONNECT #######################
class DBConnectionManager{
var $DB;
function DBConnectionManager(){
$this->DB=$this->DBConnect();
}
function DBConnect()
{
//connection to database
// $dbconnect = odbc_connect(DB_SOURCE_NAME,DB_USERNAME,DB_PASSWORD)
$dbconnect = new mysqli("localhost",DB_USERNAME,DB_PASSWORD);
or die("Unable to Connect to SQL SERVER on ".DB_SOURCE_NAME);
return $dbconnect;
}
function DBexecute($query='')
{
$result = odbc_exec($this->DB,$query);
return $result;
}
function DBRows($query='')
{
$result=$this->DBexecute($query);
$num_row = 0;
while ($row = odbc_fetch_array($result))
{
$num_row++;
}
odbc_free_result($result);
return $num_row;
}
function mysqlFetchArray($arry){
return odbc_fetch_array($arry);
}
function closeConnection(){
if(isset($this->DB)){
odbc_close($this->DB);
unset($this->DB);
}
}
}
?>
What are the other changes i have to make?
Thanks in advance,
Renu
Related
I followed the steps at this website to create a background service using Android Studio that sends GPS data to a PHP script, that then posts to a MySQL database.
However, I am wanting to post to a Postgres database. I tried several things, but I have not had any success. These are the two PHP scripts from the website tutorial:
config.php script:
<?php
class DB{
private $dbHost="localhost";
private $dbUsername="USER";
private $dbPassword="PASS";
private $dbName="locationdb";
public $db_con;
public function __construct(){
if(!isset($this->db)){
try{
$pdo=new PDO("mysql:host=".$this->dbHost.";dbname=".$this->dbName, $this->dbUsername, $this->dbPassword);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db_con=$pdo;
}catch(PDOEXCEPTION $e){
die("Failed to connect with mysql :".$e->getMessage());
}
}
}
public function addGPSData($lat, $lon)
{
$sql="INSERT into tblgpslocation(latval, lonval) VALUES (:lat, :lon)";
$stmt=$this->db_con->prepare($sql);
$stmt->bindParam(":lat", $lat);
$stmt->bindParam(":lon", $lon);
$stmt->execute();
$newId=$this->db_con->lastInsertId();
return $newId;
}
}
?>
Script 2:
<?php
include "config.php";
$db= new DB();
if(isset($_POST['gpsdata']) && $_POST['gpsdata']=='true'){
$result=$db->addGPSData($_POST['latitude'], $_POST['longitude']);
if($result>0){
echo "OK";
}else{
echo "error";
}
}
?>
What changes do I need to make in order to make this connect to my postgresql database and post the incoming data?
I'm a learning programmer and I'm trying to learn PHP.
Now the point is that I use alot of db connections in my new project. This are alot of different DB's an require different connection.
Does anybody a way where I can create a function what makes me use my code like this? Getdata($User, $beerbrand, $sales); and use these variables in my code? I use the same way to get the data out of the database everytime. But it's alot of code, where I think it should be possible to make that a little easier.
The way I use now:
mysql_connect($host, $user, $password) or die ("cannot connect");
mysql_select_db("$db_name");
$stuff = mysql_query("SELECT * FROM beers ORDER BY ID");
while($frontstuff = mysql_fetch_array($stuff)){
$us = $frontstuff['Beer'];
}
If you think this is a stupid question, please be gentle and explain it to me in a easy way.
Best regards
<?php
class Database {
private $connection; //Database Connection Link
private $userName; //Database server User Name
private $password; //Database server Password
private $database; //Database Name
private $hostname; //Name of database server
public function __construct() {
$this->hostname=your servername
$this->userName=yourusername
$this->password=yourpasswrod
$this->database=your bb name
try {
$this->connectlink = mysql_connect($this->hostname,$this->userName,$this->password);
if(!($this->connectlink)) {
throw new Exception("Error Connecting to the Database".mysql_error(),"101");
}
if(!mysql_select_db($this->database, $this->connectlink)) {
throw new Exception("Error Connecting to the Database1".mysql_error(),"101");
}
}catch(Exception $e){
//print_r($dbConfig);
echo $e->getMessage();
}
}
public function __destruct() {
#mysql_close($this->connectlink);
}
}
you can crete a database file like this for specifying connnection
and you can use this file in processing your query in another pages.Eg
<?php
include_once("database.php");
function __construct(){
$this->db = new Database;
}
public function getstuf($parameter){
$stuff = mysql_query("SELECT * FROM beers ORDER BY ID");
while($frontstuff = mysql_fetch_array($stuff)){
$us = $frontstuff['Beer'];
}
return stuff;
}
}
I have created php and Mysql site.Its Working Locally Xampp well. Finally I moved My site to server..i have Used Mysql Workbench. connected client database in Mysql Workbench in My system.when I Run the code its showing Error:
Validating Information....error with query: SELECT ID,fname,lname,email,userlevel,user_group,orgID FROM students WHERE username='test' AND password='123456' No connection could be made because the target machine actively refused it.
<?php
class db
{
var $persistent=0;
var $query;
var $user="abaceu";
var $pass ="******";
var $host="*********";
var $mydb="mysql_24172_abacu";
var $rlink;
function connect()
{
if($this->persistent==1)
{
$this->rlink = mysql_connect($this->host,$this->user,$this->pass);
}
else
{
$this->rlink = mysql_connect($this->host,$this->user,$this->pass);
}
mysql_select_db ($this->mydb);
}
function query($SQL)
{
$this->query=mysql_query($SQL)or die( "error with query: $SQL ".mysql_error() );
}
function getRows()
{
$this->moreRows=mysql_fetch_array($this->query);
if($this->moreRows<1)
{
#mysql_close($this->rlink);
}
return $this->moreRows;
}
function row($column)
{
return $this->moreRows[$column];
}
function close()
{
#mysql_close($this->rlink);
}
}
/*
$db = new db;
$db->connect();
$db->query("SELECT * FROM x_groups");
//$db->query("INSERT INTO x_groups (group_ID,name) VALUES ('mm10','mystery tester 10')");
while($db->getRows())
//while(odbc_fetch_into($rlink, $row));
{
echo $db->row("name")."---".$db->row("group_ID")."<BR>";
}
*/
?>
any Idea about issues? its worked xampp Local side.But Not Working Host server side ?
I have a PHP/MySQL website and it is hosted on Shared hosting. This website is not a huge traffic site. But I got an error (Too Many Connections Error) frequently. The hosting provider said that 25 MySQL connections at a time
I am using two functions for database connections, initDB() and closeDB()
function initDB()
{
$connection = mysql_connect($DatabaseURL,$DatabaseUName,$DatabasePWord);
if($connection)
{
$db = mysql_select_db($DatabaseName,$connection);
}
return $connection;
}
function closeDB($connection)
{
mysql_close($connection);
}
gettings data from database
$connection = initDB();
//executing MySQL query
closeDB($connection);
This is working fine, but sometime got Too Many Connections Error.
In my website, initDB() and closeDB() functions are called 76 times.
I am sure the opened connections are closed after execution
If any problem in the above code
How can i solve the Too Many Connections Error
You can use fast-hack of your code using static.
function initDB()
{
static $connection;
if (!$connection) //establish connection only once
{
$connection = mysql_connect($DatabaseURL,$DatabaseUName,$DatabasePWord);
if($connection)
{
$db = mysql_select_db($DatabaseName,$connection);
}
}
return $connection;
}
function closeDB($connection)
{
//no need to close.
}
I have just changed the connection driver (extension) from mssql_connect to work with sqlsrv_connect. Unfortunately things do not seem to work as I wanted. I'd appreciate if someone can tell me what’s wrong and how can I fix it.
Code excerpt:
//a oracle DB moudle with a generic functions such as
//db_connect() db_query()
/// turn on verbose error reporting (15) to see all warnings and errors
error_reporting(15);
//generic DB operations
// Global record . for using in
$curr_rec = NULL;
function dbok($res)
{
if($res==false){
echo "DB error: "."FIXME need error mesg"."\n";
exit;
}
return $res;
}
function db_now_expr()
{
return "getdate()";
}
function db_is_connected($dbh)
{
return $dbh>0;
}
function ensure_connected($dbh)
{
if(!db_is_connected($dbh)) die("DB is not connected, operation failed");
//echo "DEBUG: hdb=$dbh";
}
//connect to given database
//return handler to DB
function dbo_logon($dbserver,$dbname)
{
$tsql = ??????? ====>>> what should i put here????
$dbserver = "computername\SQLEXPRESS";
$dbname = MY_Database_name;
$connectionOptions = array("Database"=>"BULL");
$dbh=dbok(sqlsrv_connect($dbserver, $connectionOptions));
dbok(sqlsrv_query($dbname,$dbh));
$stmt = sqlsrv_query( $connectionOptions, $tsql );
return $dbh;
}
//close DB
function dbo_logoff($dbh)
{
if(!db_is_connected($dbh)) echo("DB disconnect error");
sqlsrv_close($dbh);
}
Here is the original code using mssql_connect:
//a oracle DB module with a generic function such as
//db_connect() db_query()
/// turn on verbose error reporting (15) to see all warnings and errors
error_reporting(15);
//generic DB operations
// Global record . for using in
$curr_rec = NULL;
function dbok($res)
{
if($res==false){
echo "DB error: "."FIXME need error mesg"."\n";
exit;
}
return $res;
}
function db_now_expr()
{
return "getdate()";
}
function db_is_connected($dbh)
{
return $dbh>0;
}
function ensure_connected($dbh)
{
if(!db_is_connected($dbh)) die("DB is not connected, operation failed");
//echo "DEBUG: hdb=$dbh";
}
//connect to given database
//return handler to DB
function dbo_logon($dbserver,$dbuser,$dbpass,$dbname)
{
// $dbh=dbok(mssql_pconnect($dbserver,$dbuser,$dbpass));
$dbh=dbok(mssql_connect($dbserver,$dbuser,$dbpass));
//error_log("connect to [$dbname]".date("His")." \r\n",3,"/tmpbull.log"); //DBEUG DELME
dbok(mssql_select_db($dbname,$dbh));
//dbo_exec($dbh,"alter session set NLS_DATE_FORMAT='dd-mm-yyyy //hh24:mi:ss'");
return $dbh;
}
//close DB
function dbo_logoff($dbh)
{
if(!db_is_connected($dbh)) echo("DB disconnect error");
mssql_close($dbh);
}
Note that I had to change the authentication method from SQL authentication to Windows authentication because sqlsrv_connect uses Windows authentication instead of SQL authentication. Is that right?
I think these two links will help to understand Difference between mssql and sqlsrv
http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx
http://blogs.msdn.com/b/brian_swan/archive/2010/03/10/mssql-vs-sqlsrv-what-s-the-difference-part-2.aspx
Though in short, to quote one of the articles:
The sqlsrv driver is built, maintained, and supported by Microsoft`
and
The mssql driver is a community-built driver.
I’m not sure how recently this driver was updated or maintained as an official PHP extension, but as of the release of PHP 5.3, it is no longer available with PECL. A quick internet search turns up a few places to download the mssql driver, but none of them that I’ve found indicate that the driver is being actively maintained.
Source: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx
As for your code sample, see below:
$dbserver = "computername\SQLEXPRESS";
$dbname = "BULL";
$connetion = dbo_logon($dbserver,$dbname);
function dbo_logon($dbserver,$dbname) {
$connectionOptions = array("Database"=>$dbname);
$dbh=sqlsrv_connect($dbserver, $connectionOptions);
if(!$dbh){
die("Error in Database connection");
return false;
}
return $dbh;
}
I think this will work for the connection.please note the code is not tested.
"The sqlsrv driver is built, maintained, and supported by Microsoft`" well right now Sep 2014, there is no official release to support php 5.6 last official release is from april 2012. MS style...