-1

I am fairly new to php and learning how to run sql queries using php.

So far I wrote following code:

    <?php
      $servername="localhost";
      $user="root";
      $password="mypassword";
      $dbname="mydbname";
      
      //Create Connection
      $conn=mysqli_connect($servername,$user,$password,$dbname);
      
      
      //Check connection
      if(!$conn)
        {
            die("connection failed" . mysqli_connect_error());
        }
        
        echo "<b>Connection is Successfull.</b>";
        
            
            mysqli_close($conn);
            unset($conn);
            $sql = "SELECT * FROM users";

            $query=mysqli_query($conn, $sql) or die(mysql_error())
        
        ?>

When I run above code, it gives me following error

Connection is Successful.

Notice: Undefined variable: conn in /index2.php on line 34

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in index2.php on line 34

I have tried several other methods but all shows some kind of error.

Community
  • 1
  • 1
imrolling
  • 9
  • 3
  • 1
    remove `unset($conn);` – Chetan Ameta Dec 15 '15 at 06:15
  • 1
    Because you unset or close connection before your query execute `mysqli_close($conn); unset($conn);` add this at the end of your query – Saty Dec 15 '15 at 06:15
  • You are closing mysql connection before query execution. Use `mysqli_close($conn); unset($conn);` after query execution or at the end of the file. – Apb Dec 15 '15 at 06:17
  • Note - since your are using `mysqli` and not `mysql`, your `or die(mysql_error())` should be `or die(mysqli_error($conn))` – Sean Dec 15 '15 at 06:18

4 Answers4

0

You close your connection before sending the query. Uncool :)

Move

mysqli_close($conn);
unset($conn);

below

$sql = "SELECT * FROM users";
$query=mysqli_query($conn, $sql) or die(mysql_error())
Marcus
  • 1,910
  • 2
  • 16
  • 27
0

You can't close mysqli connection before query execution.

You have to close connection when all queries executed.

<?php
      $servername="localhost";
      $user="root";
      $password="mypassword";
      $dbname="mydbname";

      //Create Connection
      $conn=mysqli_connect($servername,$user,$password,$dbname);


      //Check connection
      if(!$conn)
        {
            die("connection failed" . mysqli_connect_error());
        }

        echo "<b>Connection is Successfull.</b>";



            $sql = "SELECT * FROM users";

$query=mysqli_query($conn, $sql) or die(mysql_error())
mysqli_close($conn);
            unset($conn);
        ?>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vishal Kamal
  • 1,104
  • 2
  • 10
  • 35
0

For example:

//mysql configuration
     $server = "localhost";
     $dbuser = "root";
     $dbpw = "";
     $db = "db";
     $con = "";
     $con = new mysqli($server,$dbuser,$dbpw, $db);
 //get data from table   
        $sql = "SELECT * FROM users";
            $rs=$con->query($sql);
            while($row = $rs->fetch_assoc()){
    echo $row['ID'];
    }
NormundsP
  • 445
  • 2
  • 7
  • 16
-1
$sql = "SELECT * FROM users";
$query=mysqli_query($conn, $sql) or die( mysqli_error($conn));
mysqli_close($conn);
unset($conn);
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15