-3

MY dbMySql.PHP FILE CODING

<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'dbtuts');

class DB_con
{
 function __construct()
 {
 global $conn;
 $conn = mysql_connect(DB_SERVER,DB_USER,DB_PASS) or die('localhost connection problem'.mysql_error());
  mysql_select_db(DB_NAME);
 }
 public function insert($fname,$lname,$city)
 {
  $sql = "INSERT users(first_name,last_name,user_city)VALUES('$fname','$lname','$city')";
  $res = mysql_query($sql);
  return $res;
 }
 public function select()
 {
    // $db=new DB_con();
   //  $db->__construct();
   $sql = "SELECT * FROM users";
   $res=mysql_query($sql);

    // return $conn;
   return $res;
 }
}
?>

MY index.php FILE

<?php
include_once 'dbMySql.php';
$con = new DB_con();
$table = "users";
$res=$con->select($table);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="header">
 <div id="content">
    <label></label>
    </div>
</div>
<div id="body">
 <div id="content">
    <table align="center">
    <tr>
    <th colspan="3"><a href="add_data.php">ADD</a></th>
    </tr>
    <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>City</th>
    </tr>
    <?php
 while($row=mysql_fetch_row($res))
 {
   ?>
            <tr>
            <td><?php echo $row[1]; ?></td>
            <td><?php echo $row[2]; ?></td>
            <td><?php echo $row[3]; ?></td>
            </tr>
            <?php
 }
 ?>
    </table>
    </div>
</div>

<div id="footer">
 <div id="content">
    <hr /><br/>
    <label>Appxone Private Limited<a href="http://cleartuts.blogspot.com"></a></label>
    </div>
</div>

</center>
</body>
</html>

MY add_data.php FILE CODING

<?php
include_once 'dbMySql.php';
$con = new DB_con();

// data insert code starts here.
if(isset($_POST['btn-save']))
{
 $fname = $_POST['first_name'];
 $lname = $_POST['last_name'];
 $city = $_POST['city_name'];

 $res=$con->insert($fname,$lname,$city);
 if($res)
 {
  ?>
  <script>
  alert('Record inserted...');
        window.location='index.php'
  </script>
  <?php
 }
 else
 {
  ?>
  <script>
  alert('error inserting record...');
        window.location='index.php'
        </script>
  <?php
 }
}
// data insert code ends here.

?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Data Insert and Select Data Using OOP - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>

<div id="header">
 <div id="content">
    <label>PHP Data Insert and Select Data Using OOP - By Cleartuts</label>
    </div>
</div>
<div id="body">
 <div id="content">
    <form method="post">
    <table align="center">
    <tr>
    <td><input type="text" name="first_name" placeholder="First Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="last_name" placeholder="Last Name" required /></td>
    </tr>
    <tr>
    <td><input type="text" name="city_name" placeholder="City" required /></td>
    </tr>
    <tr>
    <td>
    <button type="submit" name="btn-save"><strong>SAVE</strong></button></td>
    </tr>
    </table>
    </form>
    </div>
</div>

</center>
</body>
</html>

MY style.css Coding is

@charset "utf-8";
/* CSS Document */

*
{
 margin:0;
 padding:0;
}
#header
{
 width:100%;
 height:50px;
 background:#00a2d1;
 color:#f9f9f9;
 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
 font-size:35px;
 text-align:center;
}
#header a
{
 color:#fff;
 text-decoration:blink;
}
#body
{
 margin-top:50px;
}
table
{
 width:40%;
 font-family:Tahoma, Geneva, sans-serif;
 font-weight:bolder;
 color:#999;
 margin-bottom:80px;
}
table a
{
 text-decoration:none;
 color:#00a2d1;
}
table,td,th
{
 border-collapse:collapse;
 border:solid #d0d0d0 1px;
 padding:20px;
}
table td input
{
 width:97%;
 height:35px;
 border:dashed #00a2d1 1px;
 padding-left:15px;
 font-family:Verdana, Geneva, sans-serif;
 box-shadow:0px 0px 0px rgba(1,0,0,0.2);
 outline:none;
}
table td input:focus
{
 box-shadow:inset 1px 1px 1px rgba(1,0,0,0.2);
 outline:none;
}
table td button
{
 border:solid #f9f9f9 0px;
 box-shadow:1px 1px 1px rgba(1,0,0,0.2);
 outline:none;
 background:#00a2d1;
 padding:9px 15px 9px 15px;
 color:#f9f9f9;
 font-family:Arial, Helvetica, sans-serif;
 font-weight:bolder;
 border-radius:3px;
 width:100%;
}
table td button:active
{
 position:relative;
 top:1px;
}
#footer
{
 margin-top:50px;
 position:relative;
 bottom:30px;
 font-family:Verdana, Geneva, sans-serif;
}

all code is working and data insert successfully and show but if you see above first file i am using mysql,i want to use mysqli but issue is that when show $conn as a global variable (because mysqli needed 2 parameters) and use in mysqli($sql,$conn),error show undefined variable $conn why?

All World
  • 65
  • 7
  • Define the variable outside the function and it should work? –  Apr 17 '18 at 05:59
  • $conn global variable make in function but when use in another function doesnot work why eror undefined see only my first file and last wordings then you understand my problem – All World Apr 17 '18 at 06:01
  • 2
    **The `mysql` PHP extension is dead** -- Don't use the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php) in new code. They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Apr 17 '18 at 06:01
  • You have to declare what variables are global on the scope of every function that uses them. If you want your method `select()` to have access to global variable `$conn`, you have to add `global $conn` in there too. – Havenard Apr 17 '18 at 06:01
  • Read about [variable scope](http://php.net/manual/en/language.variables.scope.php). – axiac Apr 17 '18 at 06:02
  • tell me how i can use $conn in another function with mysqli because when i am using give me error undefined – All World Apr 17 '18 at 06:03
  • Just put `global $conn` on every function that uses `$conn`. Also, MySQLi introduces object oriented support, you can use `$conn->query($sql)` instead of `mysqli_query($conn, $sql)`. – Havenard Apr 17 '18 at 06:04
  • 1
    Alternatively, you can make `$conn` a property of your object instead of making it a global variable. Set and use `$this->conn`. – Havenard Apr 17 '18 at 06:05
  • Also use [prepared statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) too, to protect yourself from SQL injections – JustCarty Apr 17 '18 at 06:14

1 Answers1

1

Use $this for access variables in your class

define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS' ,'');
define('DB_NAME', 'dbtuts');

class DB_con {
    private $conn;
    function __construct() {
        $this->conn = mysqli_connect(DB_SERVER,DB_USER,DB_PASS) or die('localhost connection problem'.mysql_error());
        mysqli_select_db($this->conn, DB_NAME);
    }
    public function insert($fname,$lname,$city) {
        $sql = "INSERT users(first_name,last_name,user_city)VALUES('$fname','$lname','$city')";
        $res = mysqli_query($this->conn, $sql);
        return $res;
    }
    public function select() {
        // $db=new DB_con();
        //  $db->__construct();
        $sql = "SELECT * FROM users";
        $res = mysqli_query($this->conn, $sql);

        // return $conn;
        return $res;
    }
}
Almog
  • 220
  • 1
  • 10
  • @Almog i am using your code but error show in second last line, mysqli_query() expects parameter 1 to be mysqli – All World Apr 17 '18 at 06:10
  • @AllWorld I edited my code, B.Desai thanks for correcting me. – Almog Apr 17 '18 at 06:12
  • @AllWorld `while($row = mysqli_fetch_row($res))`. You are mixing `mysql` and `mysqli`. Also, both this and your `select` function don't accept any parameters but you pass in the table in your code – JustCarty Apr 17 '18 at 06:16