Php 有没有一种方法可以让我从MySQL数据库中获取数据并将其显示在引导表中? 搜寻
上面是我的form.php代码。这就是我需要桌子的地方。到目前为止,我已经设法从数据库中获取数据并显示出来,但只显示在一行中。以下是backend search.php代码以防万一:Php 有没有一种方法可以让我从MySQL数据库中获取数据并将其显示在引导表中? 搜寻,php,html,mysql,twitter-bootstrap,Php,Html,Mysql,Twitter Bootstrap,上面是我的form.php代码。这就是我需要桌子的地方。到目前为止,我已经设法从数据库中获取数据并显示出来,但只显示在一行中。以下是backend search.php代码以防万一: <!DOCTYPE html> <html> <body> <!-- [SEARCH FORM] --> <form method="post" action="1-form.php"> <h1&g
<!DOCTYPE html>
<html>
<body>
<!-- [SEARCH FORM] -->
<form method="post" action="1-form.php">
<h1>SEARCH</h1>
<input type="text" name="search" required/>
<input type="submit" value="Search"/>
</form>
<?php
if (isset($_POST['search'])) {
// SEARCH FOR USERS
require "2-search.php";
// DISPLAY RESULTS
if (count($results) > 0) {
foreach ($results as $r) {
printf($r['name']);
printf($r['email']);
printf($r['phone']);
printf($r['industry']);
printf($r['about']);
}
} else {
echo "No results found";
}
}
?>
</body>
</html>
您可以迭代每个结果,并使用echo
将其与元素一起输出,如下面的代码片段所示。回显html元素并将其与数据库中的数据连接起来
然后可以在表和tds中使用引导类
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'db');
define('DB_CHARSET', 'utf8');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
try {
$pdo = new PDO(
"mysql:host=" . DB_HOST . ";charset=" . DB_CHARSET . ";dbname=" . DB_NAME,
DB_USER, DB_PASSWORD, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false ]
);
} catch (Exception $ex) {
die($ex->getMessage());
}
$stmt = $pdo->prepare("SELECT * FROM `service_providers` WHERE `industry` LIKE ?");
$stmt->execute(["%" . $_POST['search'] . "%"]);
$results = $stmt->fetchAll();
if (isset($_POST['ajax'])) { echo json_encode($results); }
名称
电子邮件
电话
您好,非常感谢CodeCreate。几次调整,效果很好!
<body>
<table class="">
<thead>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</thead>
<tbody>
<?php
foreach ($results as $res)
{
echo '<tr>';
echo '<td class="">'. $res['name'] .'</td>';
echo '<td class="">'. $res['phone'] .'</td>';
echo '<td class="">'. $res['email'] .'</td>';
echo '<tr>';
}
?>
</tbody>
</table>
</body>