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(); ?>
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: