Link to home
Create AccountLog in
Avatar of Sith717
Sith717

asked on

Change the database connection type

Hello, I currently have a script that has a weird connection to the database, it currently looks like this:

<?php
session_start();
ob_start();
$hasDB = false;
$server = 'localhost';
$user = 't2n7gauf_sith717';
$pass = '123321';
$db = 't2n7gauf_sith717';
$mySQL = new mysqli($server,$user,$pass,$db);
if ($mySQL->connect_error)
{
    die('Connect Error (' . $mySQL->connect_errno . ') '. $mySQL->connect_error);
}
?>

Open in new window



I want it to instead look like this to match with another script im coding.

<?php 
 mysql_connect("localhost", "t2n7gauf_sith717", "123321") or die(mysql_error());
mysql_select_db("t2n7gauf_sith717") 
 or die(mysql_error());
?>

Open in new window


The thing is, if I use that connect code above it doesnt connect correctly, can someone change the variables or remove the things that need it to be set as $mySQL I guess.

Here is my code:

<?php

function checkUNEmail($uname,$email)
{
        global $mySQL;
        $userID = 'X';
        $error = array('status'=>false,'userID'=>0);
        if (isset($email) && trim($email) != '') {
                //email was entered
                if ($SQL = $mySQL->prepare("SELECT `id` FROM `admin` WHERE `email` = ? LIMIT 1"))
                {
                        $SQL->bind_param('s',trim($email));
                        $SQL->execute();
                        $SQL->store_result();
                        $numRows = $SQL->num_rows();
                        $SQL->bind_result($userID);
                        $SQL->fetch();
                        $SQL->close();
                        if ($numRows >= 1) return array('status'=>true,'userID'=>$userID);
                } else { return $error; }
        } elseif (isset($uname) && trim($uname) != '') {
                //username was entered
                if ($SQL = $mySQL->prepare("SELECT `id` FROM admin WHERE username = ? LIMIT 1"))
                {
                        $SQL->bind_param('s',trim($uname));
                        $SQL->execute();
                        $SQL->store_result();
                        $numRows = $SQL->num_rows();
                        $SQL->bind_result($userID);
                        $SQL->fetch();
                        $SQL->close();
                        if ($numRows >= 1) return array('status'=>true,'userID'=>$userID);
                } else { return $error; }
        } else {
                //nothing was entered;
                return $error;
        }
}

function getSecurityQuestion($userID)
{
        global $mySQL;
        $questions = array();
        $questions[0] = "What is your mother's maiden name?";
        $questions[1] = "What city were you born in?";
        $questions[2] = "What is your favorite color?";
        $questions[3] = "What year did you graduate from High School?";
        $questions[4] = "What was the name of your first boyfriend/girlfriend?";
        $questions[5] = "What is your favorite model of car?";
        if ($SQL = $mySQL->prepare("SELECT `secquestion` FROM `admin` WHERE `id` = ? LIMIT 1"))
        {
                $SQL->bind_param('i',$userID);
                $SQL->execute();
                $SQL->store_result();
                $SQL->bind_result($secQ);
                $SQL->fetch();
                $SQL->close();
                return $questions[$secQ];
        } else {
                return false;
        }
}

function checkSecAnswer($userID,$answer)
{
        global $mySQL;
        if ($SQL = $mySQL->prepare("SELECT `username` FROM `admin` WHERE `id` = ? AND LOWER(`secanswer`) = ? LIMIT 1"))
        {
                $answer = strtolower($answer);
                $SQL->bind_param('is',$userID,$answer);
                $SQL->execute();
                $SQL->store_result();
                $numRows = $SQL->num_rows();
                $SQL->close();
                if ($numRows >= 1) { return true; }
        } else {
                return false;
        }
}

function sendPasswordEmail($userID)
{
        global $mySQL;
        changePassword($userID);
        if ($SQL = $mySQL->prepare("SELECT `username`,`email`,`password` FROM `admin` WHERE `id` = ? LIMIT 1"))
        {
                $SQL->bind_param('i',$userID);
                $SQL->execute();
                $SQL->store_result();
                $SQL->bind_result($uname,$email,$password);
                $SQL->fetch();
                $SQL->close();
                $message = "Dear $uname,\r\n";
                $message .= "Here is your requested lost password for your account at our site:\r\n";
                $message .= "-----------------------\r\n";
                $message .= "$password\r\n";
                $message .= "-----------------------\r\n";
                $message .= "Our login page: <a href=\"login.php\">http://www.oursite.com/login.php</a>\r\n\r\n";
                $message .= "Thanks,\r\n";
                $message .= "-- Our site team";
                $headers .= "From: Our Site <webmaster@oursite.com> \n";
                $headers .= "To-Sender: \n";
                $headers .= "X-Mailer: PHP\n"; // mailer
                $headers .= "Reply-To: webmaster@oursite.com\n"; // Reply address
                $headers .= "Return-Path: webmaster@oursite.com\n"; //Return Path for errors
                $headers .= "Content-Type: text/html; charset=iso-8859-1"; //Enc-type
                $subject = "Your Lost Password";
                @mail($email,$subject,$message,$headers);
                return str_replace("\r\n","<br/ >",$message);
        }
}

function genRandomString($userID) {
    $length = 10;
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string = '';    
    for ($p = 0; $p < $length; $p++) {
        $string .= $characters[mt_rand(0, strlen($characters))];
    }
    return $string;
}

function changePassword($userID){
  global $mySQL;
  global $userID;
  $password = genRandomString($userID);
  $SQL = $mySQL->prepare('UPDATE `admin` SET `password`="'.$password.'" WHERE `id`="'.$userID.'"');
  $SQL->bind_param('s',$password);
  $SQL->bind_param('i',$userID);
  $SQL->execute();
  return $password;
}

?>

Open in new window


Thanks, I tried removing all the parts that require $mySQL part but the script stops working.

Thank you once again.

Im basically trying to simplify the connection to the database so ti works with another script.
Avatar of Frozenice
Frozenice
Flag of Philippines image

hi..


look for "Class  mysqli"

we'l change the connection from there...

your code doesnt work correctly
mysql_connect("localhost", "t2n7gauf_sith717", "123321") or die(mysql_error());
mysql_select_db("t2n7gauf_sith717")
 or die(mysql_error());

this is because, your functions is calling the $mySQL object and this object need to
be initialized or connect...

and what do you mean doesnt connect corretly?
as long as your user pass details and db name is correct it should be ok.

any error that you wanna tell us?



br


Avatar of Sith717
Sith717

ASKER

Yeah, Im trying to make the code work with that exact database connection by removing the mysqli parts.
ASKER CERTIFIED SOLUTION
Avatar of Vampireofdarkness
Vampireofdarkness
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
You might want to put all the data base initialization code into a common script that you include at the top of your PHP scripts.  I use a single statement like this to create my framework:

require_once('common.php');

Inside the common script, I have my classes and constants, something to start the session, retrieve the cookies and connect to the data base, like this code snippet.

But that said, you need to decide whether you want to use MySQL or MySQLI.  They can produce equivalent results, with no meaningful difference in performance.  MySQLi is a little more "advanced" and idiot-proof.  Choose one or the other because the functional syntax is different from the OOP syntax.
http://php.net/manual/en/ref.mysql.php
http://php.net/manual/en/class.mysqli.php
<?php // common.php
error_reporting(E_ALL);


// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://us2.php.net/manual/en/ref.mysql.php
// MAN PAGE: http://us2.php.net/manual/en/mysql.installation.php
// MAN PAGE: http://us.php.net/manual/en/function.mysql-error.php


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

Open in new window