gpt4 book ai didi

php - 用 PHP 编写数据库的属性

转载 作者:行者123 更新时间:2023-11-29 02:17:43 25 4
gpt4 key购买 nike

我正在编写一个用户可以在其中输入数据库名称的应用程序,我应该使用 PHP 将其所有内容写入表中。当我知道数据库名称时,我可以使用以下代码执行此操作。

$result = mysqli_query($con,"SELECT * FROM course");

echo "<table border='1'>
<tr>
<th>blablabla</th>
<th>blabla</th>
<th>blablabla</th>
<th>bla</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['blabla'] . "</td>";
echo "<td>" . $row['blablabla'] . "</td>";
echo "<td>" . $row['bla'] . "</td>";
echo "</tr>";
}
echo "</table>";

在这个例子中,我可以显示它,因为我知道表的名称是 course 并且它有 4 个属性。但是我希望能够显示结果,而不考虑用户输入的名称。所以如果用户想要查看讲师的内容应该有两列而不是 4 列。我怎样才能做到这一点。我用 html 获取表名。

Table:<input type="text" name="table">

编辑:Denis 的回答和 GrumpyCroutons 的回答都是正确的。你也可以问我,如果你不明白他们的解决方案中的某些内容。

最佳答案

快速写下它,对其进行评论(这样你就可以很容易地了解发生了什么,你看),并为你测试它。

    <form method="GET">
<input type="text" name="table">
</form>

<?php


//can be done elsewhere, I used this for testing. vv
$config = array(
'SQL-Host' => '',
'SQL-User' => '',
'SQL-Pass' => '',
'SQL-Database' => ''
);
$con = mysqli_connect($config['SQL-Host'], $config['SQL-User'], $config['SQL-Pass'], $config['SQL-Database']) or die("Error " . mysqli_error($con));
//can be done elsewhere, I used this for testing. ^^


if(!isSet($_GET['table'])) { //check if table choser form was submitted.
//In my case, do nothing, but you could display a message saying something like no db chosen etc.
} else {

$table = mysqli_real_escape_string($con, $_GET['table']); //escape it because it's an input, helps prevent sqlinjection.

$sql = "SELECT * FROM " . $table; // SELECT * returns a list of ALL column data
$sql2 = "SHOW COLUMNS FROM " . $table; // SHOW COLUMNS FROM returns a list of columns

$result = mysqli_query($con, $sql);
$Headers = mysqli_query($con, $sql2);

//you could do more checks here to see if anything was returned, and display an error if not or whatever.

echo "<table border='1'>";
echo "<tr>"; //all in one row

$headersList = array(); //create an empty array

while($row = mysqli_fetch_array($Headers)) { //loop through table columns
echo "<td>" . $row['Field'] . "</td>"; // list columns in TD's or TH's.
array_push($headersList, $row['Field']); //Fill array with fields
} //$row = mysqli_fetch_array($Headers)

echo "</tr>";

$amt = count($headersList); // How many headers are there?

while($row = mysqli_fetch_array($result)) {
echo "<tr>"; //each row gets its own tr
for($x = 1; $x <= $amt; $x++) { //nested for loop, based on the $amt variable above, so you don't leave any columns out - should have been <= and not <, my bad
echo "<td>" . $row[$headersList[$x]] . "</td>"; //Fill td's or th's with column data
} //$x = 1; $x < $amt; $x++
echo "</tr>";
} //$row = mysqli_fetch_array($result)

echo "</table>";
}
?>

关于php - 用 PHP 编写数据库的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36939826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com