[英]can not fetch from database using mysqli_fetch_assoc($result)
I can not fetch rows from database and populate my select input 我无法从数据库中获取行并填充我的选择输入
I have tried to populate the input by calling a function in another php file which fetches the result from the database and returns the result. 我试图通过在另一个php文件中调用一个函数来填充输入,该文件从数据库中获取结果并返回结果。
my function in getData.php: 我在getData.php中的函数:
function listTechnician(){
global $conn;
$position="technician";
$sql="SELECT user.Name FROM user INNER JOIN user_account ON user.id = user_account.id WHERE user_account.position=$position";
$result = mysqli_query($conn, $sql) or die("<script>console.log(".mysqli_error($result).")</script>");
return mysqli_fetch_row($result) ;
}
and my code in newAppModal.php(contains the select input to be populated): 和我在newAppModal.php中的代码(包含要填充的选择输入):
<select class="form-control select2" id="appTime" name="appTechnician" required="">
<?php
$data=listTechnician();
while ($row=$data) {
echo "<option>".$row['name']."</option>";
}
?>
<option selected="selected"></option>
<option>Laboratory</option>
</select>
this is contained in a modal 这包含在模态中
I want to populate my select input with the row fetched from database but it is giving me an error in the console like: 我想用从数据库中获取的行填充我的选择输入,但这在控制台中给我一个错误,例如:
main.js:388 Uncaught TypeError: Cannot read property 'addEventListener' of null
at listenBySelector (main.js:388)
at Calendar.bindHandlers (main.js:6619)
at Calendar.render (main.js:6595)
at HTMLDocument.<anonymous> (receptionist1.php:66)
and i had a calendar which now doesn't appear 我有一个现在不出现的日历
You have a couple of issues. 您有几个问题。 Firstly,
listTechnician
is only fetching one row of data instead of all of them. 首先,
listTechnician
仅获取一行数据,而不是所有数据。 Change 更改
return mysqli_fetch_row($result) ;
to 至
return mysqli_fetch_all($result);
Secondly, you are not looping through the data properly in newAppModal.php
. 其次,您没有在
newAppModal.php
正确遍历数据。 You should do something like this: 您应该执行以下操作:
$data=listTechnician();
foreach ($data as $row) {
echo "<option>".$row['Name']."</option>";
}
Note also that the field in your query will be called Name
(not name
); 还要注意,查询中的字段将称为
Name
(不是name
); I have changed that in the above code too. 我也更改了上面的代码。 It's possible the undefined index message for this is causing the problems with your Javascript.
未定义的索引消息可能会导致Javascript出现问题。
Thirdly, in your SQL query, you need to enclose $position
in single quotes ie 第三,在您的SQL查询中,您需要将
$position
用单引号引起来,即
$sql="SELECT user.Name FROM user INNER JOIN user_account ON user.id = user_account.id WHERE user_account.position='$position'";
Html Part ".$d.""; }
HTML零件“。$ d。”“;}
?> <option>Laboratory</option> </select>
PHP function .
PHP功能。
function listTechnician(){ $mysqli_connection;
函数listTechnician(){$ mysqli_connection; $mysqli_connection = new MySQLi('localhost', '', '', 'test');
$ mysqli_connection = new MySQLi('localhost','','','test'); if ($mysqli_connection->connect_error) { echo "Not connected, error: " .
if($ mysqli_connection-> connect_error){回显“未连接,错误:”。 $mysqli_connection->connect_error;
$ mysqli_connection-> connect_error; }
}
$position="technician"; $sql="SELECT user.Name FROM user INNER JOIN user_account ON user.id = user_account.id WHERE user_account.position='$position'"; $result = mysqli_query($mysqli_connection, $sql) or die("<script>console.log(".mysqli_error($result).")</script>"); $data=array(); while ($row= mysqli_fetch_row($result)) { $data = $row; } mysqli_close($mysqli_connection); return $data; }
Blockquote
块引用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.