MySQL connect on PHP

前端 未结 4 1305
情书的邮戳
情书的邮戳 2021-01-23 19:57

what is the best way to connect PHP application on MySQL.

So far I had the below connection classes.

class Connection{
    private static $server = \"127         


        
相关标签:
4条回答
  • 2021-01-23 20:17

    try this

    <?php
    
      $user = $_POST["username"];//if this doesnt work try the next line
      $user = (isset($_POST["username"]) ? $_POST["username"] : "");
    
      $host = "localhost";//mysql password
      $username = "";//mysql username
      $password = "";//mysql password
      $db_name = "database";//database name
      $tbl_name ="test";//table name
      //make the connection
      $con = mysql_connect("$host","username","password")or die("Could not connect.");
      $conn = mysql_select_db("$db_name")or die("Could not select database.");
    
      $sql = "SELECT * FROM $tbl_name WHERE username='$username'";
      //query mysql
      $result = mysql_query($sql);
    
      if($result){
      //if it works show this
    
      }else{
    
      //if it doesnt work show this
    
      }
    
    ?>
    

    ive tryed a lot of times to make a connection to a database and i finaly found one.

    0 讨论(0)
  • 2021-01-23 20:22

    You can try using the PDO object:

    <?php
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    
    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
    ?>
    

    Have a look at PHP PDO documentation page

    0 讨论(0)
  • 2021-01-23 20:26

    Try to use php frameworks like codeigniter, Yii, cake php. If you implement in any one of this framework no need to write php mysql query It will automatically generate. You just need to enter your database configuration like give in below

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'sample';
    
    0 讨论(0)
  • 2021-01-23 20:37

    You can connect through data using PDO, here is an example

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "nopass";
    
    try {
        $conn = new PDO("mysql:host=$servername;dbname=wireframe", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully"; 
    
        $stmt = $conn->prepare("SELECT * FROM todolist"); 
        $stmt->execute();
    
        }
    catch(PDOException $e)
        {
        echo "Connection failed: " . $e->getMessage();
        }
    ?>
    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <body>
    
    <table border="1" align="center">
    <tr>
      <th>name</th>
      <th>type</th>
      <th>status</th>
    </tr>
    
    
    
    
    <?php
    
    foreach($stmt->fetchAll() as $k=>$v){
         echo
       "<tr>
        <td>{$v['name']}</td>
        <td>{$v['type']}</td>
        <td>{$v['status']}</td>
       </tr>\n";
    
    }
    
    
    ?>
    </table>
    </body>
    </html>
    
    0 讨论(0)
提交回复
热议问题