查找MySQL

时间:2016-12-15 17:21:17

标签: php mysql database

我正在尝试从MySQL数据库进行简单查找。我读过的任何教程都没有运气,大多数人都试图做一些更复杂的事情而且我无法缩小代码范围!

我相信我有数据库工作,因为它连接到它没有错误。但是我似乎无法正确使用代码,这是我到目前为止所得到的...... 我在引用firstname时试图将lastname从数据库中删除。表格中有5行。

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

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname WHERE lastname = 'Barack' FROM MyGuests";

{
    echo "Name: ".$rows['firstname']."<br/>";

}

$conn->close();
?>

4 个答案:

答案 0 :(得分:2)

这里有一些问题:

  1. 您的查询语法不正确。
  2. 您需要实际执行查询。您现在所做的就是将其作为字符串分配给变量。
  3. 更新如下:

    $result = $conn->query( 'SELECT firstname FROM MyGuests WHERE lastname = \'Barack\'' );
    
    while( $guest = $result->fetch_object() )
    {
        echo $guest->firstname.'<br />';
    }
    

    请注意使用query()fetch_object()

答案 1 :(得分:1)

试试这个:

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT firstname FROM MyGuests WHERE lastname = 'Barack'";
$result = mysqli_query($conn, $sql);


while($row = mysqli_fetch_array($result)){
    echo $row['firstname'] ;
}
?>

OR 您可以尝试使用面向对象的概念

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

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT firstname FROM MyGuests WHERE lastname = 'Barack'";
$result = $conn->query($sql);


while($row = $result->fetch_assoc()) {
    echo $row['firstname'] ;
}
$conn->close();
?>

答案 2 :(得分:0)

你的代码看起来很好除了我看到至少3个问题: 1)您的SQL不正确。如果要检查查询结果,您会知道这一点。 2)您的代码实际上并不使用您定义的sql来执行任何查询。 3)运行查询后,您不会从结果中获取任何记录。

这个怎么样:

$servername = 'localhost';
$username = 'root';
$password = 'root';
$dbname = "shipping";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT firstname FROM MyGuests WHERE lastname = 'Barack'";
// you have to actually run a query with your sql and check if it works
if ($result = $conn->query($sql)) {
    // you may have more than one record with a matching last name so you loop
    while ($myrow = $result->fetch_array(MYSQLI_ASSOC)) {
        echo "Name: " . $myrow["firstname"] . "</br>";
    }
} else {
    // something didn't work. NOTE: you shouldn't output errors on a production machine
    // write a log instead or something
    printf("Error: %s\n", $conn->error);
}

$conn->close();

答案 3 :(得分:-2)

如果你不需要找到严格的匹配(像'ABarack'或'Baracka'这样的名字),请使用通配符:

SELECT firstname FROM MyGuests WHERE lastname like '%Barack%'