Jump to content

[SOLVED] Converting PHP MySql PDO script into standard PHP Mysql script


anilvaghela

Recommended Posts

Hi Guys n Girls... ;D

 

It's Friday and I am currently having a big issue.

 

My department have finally managed to get PHP installed on a linux server woohoo!! But they are a bit stingy on what they have open and configured on the php.ini files! We have MYSQL and only PHP enabled!

 

Now the problem that I have is I have been working on a project on my own dev server which has many extenstions open on PHP.

 

I have a 'PDO' scripts which wont work as the driver is missing and my department said they wont open this extenstion and i need to work around this.

 

I need help on getting the following two scripts converted into standard php and mysql scripts.

 

The first is the 'class conn' function which connects to the db:

 

 

class conn{

/*** Declare instance ***/

private static $instance = NULL;

/**

* the constructor is set to private so

* so nobody can create a new instance using new

*/

private function __construct() {

/*** maybe set the db name here later

***/

}

/**

* Return DB instance or create intitial connection

* @return object (PDO)

* @access public

*/

public static function getInstance() {

if (!self::$instance)

    {

    self::$instance = new PDO("mysql:host=HOST;dbname=DB", 'USER', 'PASSWORD');

    self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }

return self::$instance;

}

/**

* Like the constructor, we make __clone private

* so nobody can clone the instance

*/

private function __clone(){

}

} /*** end of class ***/

 

 

 

OK thats the connection now the following statements are based in the process php file which uses the connection to run mysql scripts here is a line from there which i also need to know what i can convert it too...

 

 

conn::getInstance()->beginTransaction();

$stmt = conn::getInstance()->prepare("SELECT @myorder1 := order_id FROM ref_db WHERE

name_id=:name_idd__");

$stmt->bindParam(':name_idd__', $name_idd__);

$stmt->execute();

 

 

 

 

That will hopefully need to change or may stay the same but the PDO function i need serious help on converting it to standard php scripting...

 

PLEASE HELP!!

 

Thank You!!

 

Anil Vaghela

 

[email protected]

www.heavyvibesonline.com

class conn{
/*** Declare instance ***/
private static $instance = NULL;
/**
* the constructor is set to private so
* so nobody can create a new instance using new
*/
private function __construct() {
/*** maybe set the db name here later
***/
}
/**
* Return DB instance or create intitial connection
* @return object (PDO)
* @access public
*/
public static function getInstance() {
if (!self::$instance)
    {
    self::$instance = mysql_connect('HOST','USER','PASSWORD');
    mysql_select_db('DB',self::$instance);
    }
    return self::$instance;
}
/**
* Like the constructor, we make __clone private
* so nobody can clone the instance
*/
private function __clone(){
}
} /*** end of class ***/

 

$conn = conn::getInstance();
$sql = sprintf("SELECT @myorder1 := order_id FROM ref_db WHERE name_id = '%s'",mysql_real_escape_string($name_idd__));
mysql_query($sql,$conn) or die(mysql_error($conn));

 

I have a 'PDO' scripts which wont work as the driver is missing and my department said they wont open this extenstion and i need to work around this.

 

You can indeed rewrite all of it using mysql functions. However you might want to concider an alternative, what about using a written PDO class?

http://www.google.com/webhp?complete=1&hl=en#complete=1&hl=en&q=pdo+php4&aq=f&oq=&aqi=g-s1&fp=5TZlSg8c0wI

Just one more thing about the above equation that did work for the 'conn:getinstance' query....

 

how would i bind the following.....

 

$stmt = conn::getInstance()->prepare("INSERT INTO ref_db(order_id, name_id, name_ch, parent_id, level, agg, pseudo, ps_id, replevel, class, audit, tdate, user_id, checked_id, assign_id) VALUES(@myorder1 +1,  :name_id, :move_node, :parent_id, :level, :agg, :pseudo, :ps_id, :replevel, :class, :audit, NOW(), :user_id, :checked_id, :assign_id)");

$stmt->bindParam(':name_id', $name_id);   

$stmt->bindParam(':move_node', $move_node);

$stmt->bindParam(':parent_id', $parent_id);

$stmt->bindParam(':level', $level);

$stmt->bindParam(':agg', $agg);

$stmt->bindParam(':pseudo', $pseudo);

$stmt->bindParam(':ps_id', $ps_id);

$stmt->bindParam(':replevel', $replevel);

$stmt->bindParam(':class', $class);

$stmt->bindParam(':audit', $audit);

$stmt->bindParam(':user_id', $user_id);

$stmt->bindParam(':checked_id', $checked_id);

$stmt->bindParam(':assign_id', $assign_id);

    $stmt->execute();

by using the same process as above, but repeated for all those vars. i'm not gonna type it all out, but i usually use sprintf() cus i think it's cleaner. so take your SQL, and put a %s in single quotes wherever you want a value, and then add the variable as an argument to sprintf()

$sql = sprintf("INSERT INTO ref_db(order_id, name_id, name_ch,....) VALUES (@myorder1 +1, '%s', '%s',...)",
  mysql_real_escape_string($name_id),
  mysql_real_escape_string($move_node),
  ....);
mysql_query($sql) or die(mysql_error());

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.