如何连接localhost php

时间:2017-10-23 06:55:20

标签: php mysql

这是我想要连接本地主机的HTML代码

<div class="design-w3l">
            <div class="mail-form-agile">
                <form action="#" method="post">
                    <input type="text"  name="username" class="padding" placeholder="user name" required=""/>
                    <input type="text" name="name" placeholder=" email..." required=""/>
                    <input type="password"  name="password" class="padding" placeholder="Password" required=""/>
                    <input type="password"  name="cnfpassword" class="padding" placeholder="Password" required=""/>
                <input type="submit" value="submit">
                </form>
            </div>
          <div class="clear"> </div>
        </div>

如何连接localhost phpmyadmin(php)?

2 个答案:

答案 0 :(得分:2)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "yourDatabase";

//connect to database
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);


//create table
$sql = "CREATE TABLE TableName (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
    column1 VARCHAR(30) NOT NULL,
    column2 VARCHAR(30) NOT NULL,
    column3 VARCHAR(30) NOT NULL,
    reg_date TIMESTAMP
)";

// execute
$conn->exec($sql);



// insert data
$sql = "INSERT INTO TableName (column1, column2, column3)
VALUES ('aaaa', 'bbbb', 'cccc')";

//execute
$conn->exec($sql);

答案 1 :(得分:1)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";
?>