我正在尝试检索MYSQL表的不同列的名称,然后将它们保存为我设置为cookie的单个字符串变量。问题是所有配方都保存在数组中的单个元素中。如果我可以将每个字段保存为数组中的单独对象,这甚至会有所帮助。此外,一些字段名称可能包含空白字符(例如空格)。老实说,我不太确定如何处理这个问题,但是我在下面的简化代码中尝试过:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db_name = "db_name";
$table_id = "table_name";
$conn = mysqli_connect($servername, $username, $password, $db_public_tables);
if (!$conn) {
die("Error: " . mysqli_connect_error());
}
$sql = "SHOW COLUMNS FROM `$table_id`";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($result)){
$field_array = implode(" ", $row[0]);
setcookie('selected_field_array_cookie', $field_array);
}
mysqli_close($conn);
?>
我正在寻找一个输出,我可以在html页面上打印特定的字段名称。例如:
echo $field3;
感谢您的考虑。欢迎任何形式的答案,但最好使用PHP / MYSQL / JS
答案 0 :(得分:1)
我认为这就是你要找的东西:
$servername = "localhost";
$username = "root";
$password = "";
$database = "db_name";
$table_id = "table_name";
$conn = mysqli_connect($servername, $username, $password, $database);
if(!$conn) {
die("Error: " . mysqli_connect_error());
}
$sql = "SHOW COLUMNS FROM `$table_id`";
$result = mysqli_query($conn, $sql);
$field_array = array();
$index = 1;
$omitted_fields = array('id', 'reg_date');
while($row = mysqli_fetch_array($result)){
if(!in_array($row[0], $omitted_fields)) {
$field_array['field' . $index] = $row[0];
setcookie('field' . $index, $row[0]);
extract($field_array);
$index += 1;
}
}
echo $field1 . "<br>";
echo $field2 . "<br>";
echo $field3 . "<br>";
echo $_COOKIE['field1'] . "<br>";
echo $_COOKIE['field2'] . "<br>";
echo $_COOKIE['field3'] . "<br>";
mysqli_close($conn);
希望它有效。