查询一个mysql数据库并显示返回的内容

时间:2018-10-30 03:59:50

标签: php mysql

抱歉,如果我的语言不正确,并且这是一个新手问题,请原谅。

这是我的php代码,用于创建一个基本下拉列表(表中的名称为Sandy,Tom,Tina),该下拉列表由php查询提供。

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

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

$sql = "select * from  potluck";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo "<select name='list2' id='list2'>";
    while($row = $result->fetch_assoc()) {
        echo "<option>".$row["name"]."</option>";
    }
    echo "</select>";
} else {
    echo "0 results";
}
$conn->close();
?>

如何在下拉菜单下方显示屏幕?
或更具体地说。
我如何将此select * from potluck where name="Sandy";发回php db 然后在下拉列表的正下方显示返回的内容(在此阶段只需一个基本字符串就足够了)?我不确定这部分的顺序,将不胜感激。

mysql> select * from potluck;
+----+-------+----------------+-----------+-------------+
| id | name  | food           | confirmed | signup_date |
+----+-------+----------------+-----------+-------------+
|  1 | Sandy | Key Lime Tarts | N         | 2012-04-14  |
|  2 | Tom   | BBQ            | Y         | 2012-04-18  |
|  3 | Tina  | Salad          | Y         | 2012-04-10  |
+----+-------+----------------+-----------+-------------+
3 rows in set (0.00 sec)



mysql> select * from potluck where name="Sandy";
+----+-------+----------------+-----------+-------------+
| id | name  | food           | confirmed | signup_date |
+----+-------+----------------+-----------+-------------+
|  1 | Sandy | Key Lime Tarts | N         | 2012-04-14  |
+----+-------+----------------+-----------+-------------+
1 row in set (0.00 sec)

参考 here-可能有助于我的理解


“开始工作”的简单示例:

<form id="s" method="get">
<select name="size">
    <option value="small">Small</option>
    <option value="medium">Medium</option>
    <option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form>

<?php
if( $_GET["size"])
{
echo "Welcome: ". $_GET['size']. "<br />";
}
?>

1 个答案:

答案 0 :(得分:-1)

您需要一个表单,一个提交按钮和一些PHP代码来处理表单提交。

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

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

$sql = "select * from  potluck";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    echo '<form action="http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'" method="POST">';
    echo "<select name='list2' id='list2'>";
    while($row = $result->fetch_assoc()) {
        echo '<option value="'.$row['name'].'">'.$row["name"]."</option>";
    }
    echo "</select>";
    echo '<input type="submit" value="Submit"/></form>';
} else {
    echo "0 results";
}

if (isset($_POST['name'])) {
   $result = $conn->query("SELECT * FROM potluck WHERE name='".$_POST['name']."' LIMIT 1");
   var_dump($result->fetch_assoc());
}
$conn->close();
?>