我正在尝试从数据库构建select标签但是当我返回字符串时我得到了NULL。 请帮助:
PHP代码
<?php
function getDropDown($table,$value,$display){
$dbConx = new mysqli("localhost", "root", "", "fw_request");
if ($dbConx->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
/* Select queries return a resultset */
$sql = "SELECT * FROM `$table`";
$result = $dbConx->query($sql);
$a="<select name='streams'>";
$a="<option value=\"0\">Choose Stream</option>";
while($row = $result->fetch_row()) {
$a="<option value=\"1\" selected>1</option>";
$a='<br>';
}
$a="</select>";
return $a;
}
HTML代码
Sterams:<?php echo getDropDown('streams','Streams_ID','Streams_ShortName');
查询没问题。
谢谢, CFIR。
答案 0 :(得分:2)
你继续覆盖$ a,你需要做的是通过连接 -
添加到$ a$a = "<select name='streams'>";
$a .= "<option value=\"0\">Choose Stream</option>";
while($row = $result->fetch_row()) {
$a .= "<option value=\"1\" selected>1</option>";
}
$a .= "</select>";
return $a;
答案 1 :(得分:0)
您没有连接$ a的值。
这样做
function getDropDown($table, $value, $display) {
$dbConx = new mysqli("localhost", "root", "", "fw_request");
if ($dbConx->connect_errno) {
echo "Failed to connect to MySQL: (" . $dbConx->connect_errno . ") " . $dbConx->connect_error;
}
/* Select queries return a resultset */
$sql = "SELECT * FROM `$table`";
$result = $dbConx->query($sql);
$a = "<select name='streams'>";
$a .= "<option value=\"0\">Choose Stream</option>";
while ($row = $result->fetch_row()) {
$a .= "<option value=\"1\" selected>1</option>";
}
$a .= "</select>";
return $a;
}