How to select database

Discussion in 'PHP' started by Avinash Rai, Jul 18, 2013.

  1. #1
    How to select database.when m callling insert.php it gives error as a Call to undefined function mysql_select_avinash().
    here is my code:
    <?php

    class createConnection
    {

    var $host="localhost";

    var $database="avinash";
    var $myconn;

    function connectToDatabase()
    {

    $conn= mysql_connect($this->host);

    if(!$conn)
    {
    die ("Cannot connect to the database");
    }
    else
    {

    $this->myconn = $conn;

    echo "Connection established";

    }

    return $this->myconn;

    }

    function selectDatabase()
    {
    mysql_select_avinash($this->database);

    if(mysql_error())
    {

    echo "Cannot find the database ".$this->database;

    }
    echo "Database selected..";
    }

    function closeConnection()
    {
    mysql_close($this->myconn);

    echo "Connection closed";
    }

    }//end of Class Create Connection

    ?>

    insert.php

    <?php

    include ('connection.php');
    $connection = new createConnection();
    $connection->connectToDatabase();
    $connection->selectDatabase();

    function getUserId()
    {
    $result=mysqli_query("select max(uid) from registration");
    while($row=mysqli_fetch_array($result))
    {
    $usid=$row["max(uid)"];

    }
    //echo $usid;
    return $usid++;

    }


    $uid=getUserId()+1;
    //$etype=$_POST[''];
    //echo $uid;
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];


    echo "<br>Your User id is::";
    echo $uid;

    $test2=mysql_query("insert into register values('$uid','$fname','$lname')");

    echo "<br />";
    echo "<U><b>Data Inserted Successfully<U><b><br><br>";

    $connection->closeConnection();
    ?>
     
    Last edited by a moderator: Jul 18, 2013
    Avinash Rai, Jul 18, 2013 IP
  2. mbaldwin

    mbaldwin Active Member

    Messages:
    215
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    95
    #2
    Try using PDO or mysqli.

    Michael
     
    mbaldwin, Jul 18, 2013 IP
  3. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #3
    Yes use PDO as the mysql_ functions are depreciated as of PHP 5.5.0. Example connection and select:

    $dsn = "mysql:host=localhost;dbname=test";
    $username = "XXXXXX";
    $password = "YYYYYY";
     
    try {
        $pdo = new PDO($dsn, $username, $password);
    $stmt = $pdo->prepare("SELECT * FROM fruit WHERE name = ?");
    $stmt->execute(array("Apple"));
    while($row = $stmt->fetch()) {
        print_r($row);
    }
    }
    catch(PDOException $e) {
        die("Could not connect to the database\n");
    }
    PHP:
     
    mfscripts, Jul 31, 2013 IP
  4. qtriangle

    qtriangle Active Member

    Messages:
    179
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #4
    change mysql_select_avinash to mysql_select_db.
    It will solve the issue.
     
    qtriangle, Aug 14, 2013 IP
  5. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #5
    Yeah, where does this : mysql_select_avinash come from?
     
    samyak, Aug 14, 2013 IP