-3

i am new to PHP how to group functions inside a class and use it in other pages to reduce typing this is for single page what if i use multiple pages with pageination this is my coding

<?php

include ('conn.php');
class sel
{
function sell()
{
if(isset($_POST['submit']))
{
$id=$_POST['id'];   
$username = mysql_real_escape_string($_POST['username']);   
$password = mysql_real_escape_string($_POST['password']);
$result = $myDBObject -> select('*', 'admin', 'username = "'.$username.'" and
  password ="'.$password.'"');
$x = $rdata['id'];
echo $x;
 $_SESSION['username'] =$id;
echo $_SESSION['username'];
if(! $rdata)
{
echo "Enter your username and password correctly";
}
else
{

echo '<script type="text/javascript">
     document.location="list.php";
     </script>';

}
}
}
}
$myDBObject = new sel();
$myDBObject->sell();
?>

and my connection.php code is

<?php
class DBConnect
{
private $host = 'localhost';
private $user = 'root';
private $pass = '';
private $db = 'enquiry';

public function __construct(){
$conn = mysql_connect($this -> host, $this -> user, $this -> pass);
$sql = mysql_select_db($this -> db, $conn) or die(mysql_error());
}
private function select($select, $table, $where){
$sql = "select ".$select." from ".$table." where ".$where;
$result = mysql_query($sql);
return $rdata = mysql_fetch_array($result);
}

} 
$myDBObject = new DBConnect();
$myDBObject->__construct();
$myDBObject->select($select, $table, $where);
?>

if i did like this i get this error what did i do wrong in code

Fatal error: Call to private method DBConnect::select() from context '' in C:\xampp\htdocs
\Raj\cform\conn.php on line 23
  • 5
    Your efforts have been so thorough that you've not even defined a single function! – planestepper Dec 03 '13 at 14:15
  • 2
    I don't understand what the question is really asking either. On a completely unrelated note, you've opened yourself up to some pretty terrible SQL injection attacks with the username and password coming from post! – Matt Fletcher Dec 03 '13 at 14:16
  • @GeorgeGarchagudashvili yup i am new to programming – Raj the Starter Dec 03 '13 at 14:19
  • @MattFletcher i want to use function and class for this code and group them thats my question – Raj the Starter Dec 03 '13 at 14:20
  • Also, I don't think you quite understand what classes are for- they're definitely not for re-using code in this kind of context, that's for sure. Probably best to not worry about OOP at the moment (running before walking!) And I still don't understand which bit it is that you want to group together... What are you re-using? The database connection code or the login validation? ...or something else? – Matt Fletcher Dec 03 '13 at 14:22
  • @MattFletcher database connection code i wanna reuse – Raj the Starter Dec 03 '13 at 14:28
  • The `mysql_*` functions are **no longer maintained** and shouldn't be used in any new codebase. It is being phased out in favor of newer APIs. Instead you should use [**prepared statements**](https://www.youtube.com/watch?v=nLinqtCfhKY) with either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – tereško Dec 03 '13 at 21:42

2 Answers2

0

You can use database class as mentioned below

class DBConnect
{
private $host = 'localhost';
private $user = 'user';
private $pass = 'pass';
private $db = 'mydb';

public function __construct(){
    $conn = mysql_connect($this -> host, $this -> user, $this -> pass);
    $sql = mysql_select_db($this -> db, $conn) or die(mysql_error());
}

// select query
private function select($select, $table, $where){
    $sql = "select ".$select." from ".$table." where ".$where;
    $result = mysql_query($sql);
    return $rdata = mysql_fetch_array($result);
}

// update query
private function update($update, $table, $where){
    // code here
}
}

When required, create object of the class and call functions as below

require 'dbConnect.php';    // assuming, you have included the database class in this file
$myDBObject = new DBConnect();
$username = mysql_real_escape_string($_POST['username']);   // mysql escaping
$password = mysql_real_escape_string($_POST['password']);   // mysql escaping
// select required data
$result = $myDBObject -> select('*', 'admin', 'username = "'.$username.'" and password = "'.$password.'"');

The function described in the class above are for demonstration purpose. You can include other functions in the class along with data escaping functionality.

FYI: mysql is deprecrated. Use mysqli or PDO instead. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

Community
  • 1
  • 1
Rajat Varlani
  • 456
  • 1
  • 4
  • 19
-2

// Hi,

class ClassName {
    static function funcName1(){
        funcBody1;
    }

    static function funcName2(){
        funcBody2;
    }
}

// to use functions juste do :

ClasseName::funcName1();
ClasseName::funcName2();
  • This has (code-breaking!) spelling mistakes- and the `funcBody1;` bits are quite misleading as they're not commented... but non-code bits... are... commented :P. Also doesn't say where to include the code, or what it will return, or how the OP will actually use it. Plus it doesn't address why oh why anyone would create a class full of static functions. Just an unnecessary many extra steps. I think that's enough of me berating you for your answer, sorry ha :) – Matt Fletcher Dec 03 '13 at 14:35