2016-03-02 16 views
-1

嘗試從mysql獲取值到<select>標記。
我用while while循環&從2個不同的表中獲取數據。
我已經嘗試了給定的代碼,但無法獲取數據。
請幫忙!將MySql數據取回到<select>

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 

// Connect to server and select databse. 
$conn=mysqli_connect($host,$username,$password) or die("cannot connect"); 

mysqli_select_db($conn,$db_name); 
$result = mysqli_query($conn,"SELECT * from dummy"); 

$uresult=mysqli_query($conn,"SELECT name from user where role='Support'"); 
$uname=array(); 
while ($rows=mysqli_fetch_array($uresult)) 
{ 
$uname[]=$rows['name']; 
} 
echo "<table border='1' width='100%' > 
<tr><th colspan='7' ><h2 align='center'>Details</h2></th></tr> 
<tr bgcolor='grey'> 
<th width='10%' class='text-center'>Emp No.</th> 
<th width='15%' class='text-center'>Name</th> 
<th width='30%' class='text-center'>Task</th> 
<th width='20%' class='text-center'>Assigned To</th> 
</tr>"; 


while($row = mysqli_fetch_array($result)) 
{ 
echo "<tr>"; 
echo "<td align='center'>" . $row['emp_no']. "</td>"; 
echo "<td align='center'>" . $row['emp_name']. "</td>"; 
echo "<td align='center'>" . $row['task'] . "</td>"; 
echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
mysqli_close($conn); 
?> 

錯誤: 解析錯誤:語法錯誤,意想不到 ''(T_ENCAPSED_AND_WHITESPACE),期望標識符(T_STRING)或可變(T_VARIABLE)或數字(T_NUM_STRING)

+0

你想呼應的<?PHP語法中的PHP代碼。非常差的編碼。回聲「「; – rahul

+0

你爲什麼要嘗試兩次獲取它?你已經把它放在你的容器裏了 – Ghost

+0

這段代碼中有很多紅旗,我不知道從哪裏開始。請多閱讀一下PHP手冊。我想寫一個詳細的答案,但後來我意識到它不會幫助你,因爲你還不熟悉基本的語言語法,我不能在這裏教你所有的東西。 –

回答

1

錯誤是低於線 你有串

echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>"; 
0

嘗試這樣,embedding的PHP中的html

while($row = mysqli_fetch_array($result)) 
{ 
    ?> 
    <tr> 
    <td align='center'><?php echo $row['emp_no'] ?></td> 
    <td align='center'><?php echo $row['emp_name'] ?></td> 
    <td align='center'><?php echo $row['task'] ?></td> 
    <td align='center'> <select name='select1'> 
    <?php while ($rows=mysqli_fetch_array($uresult)) { ?> 
    <option><?php echo $rows['name'];?> </option><?php } ?> 
    </select> </td> 
    </tr>"; 
    <?php 
} 
+0

$ uresult = mysqli_query($ conn,「SELECT name from user where role ='Support'」); --->如果select將填充相同的結果,爲什麼把它放在循環內?將其存儲在外部,並將其作爲變量傳遞給此。確實,代碼非常糟糕,作者,我不是指你,因爲你只是想提供一個快速修復。 –

+0

這就是要求把它放在任何他想要的地方,但有一點可以肯定的是,他希望循環SELECT *表中的虛擬語句和用戶名SELECT角色='支持'在選擇選項 – rahul

1

無需再次獲取它。你已經做了,在這裏:

$uname = array(); 
while ($rows = mysqli_fetch_array($uresult)) { 
    $uname[] = $rows['name']; 
} 

所以內部的其他塊時,只需使用已收集的名字:

while($row = mysqli_fetch_array($result)) { 
    echo "<tr>"; 
     echo "<td align='center'>" . $row['emp_no']. "</td>"; 
     echo "<td align='center'>" . $row['emp_name']. "</td>"; 
     echo "<td align='center'>" . $row['task'] . "</td>"; 
     echo " 
     <td align='center'> 
      <select name='select1'> 
     "; 
     foreach($uname as $names) { 
      echo '<option>' . $names . '</option>'; 
     } 
     echo " 
      </select> 
     </td>"; 
    echo "</tr>"; 
} 

或者只是創建的<options> HTML字符串與$uname,然後只是echowhile內:

$uname = ''; 
while ($rows = mysqli_fetch_array($uresult)) { 
    $uname .= '<option>' . $rows['name'] . '</option>'; 
} 

然後while塊:

while($row = mysqli_fetch_array($result)) { 
    echo "<tr>"; 
     echo "<td align='center'>" . $row['emp_no']. "</td>"; 
     echo "<td align='center'>" . $row['emp_name']. "</td>"; 
     echo "<td align='center'>" . $row['task'] . "</td>"; 
     echo " 
     <td align='center'> 
      <select name='select1'> 
     "; 

     echo $uname; // collection of HTML string options 

     echo " 
      </select> 
     </td>"; 
    echo "</tr>"; 
} 
+0

這是我通過預填充選擇。但是爲什麼讓這個foreach在內部運行,而在外部運行呢,在while()中你可以有一個常量字符串。 –

+0

@穆罕默德。是的,我已經編輯 – Ghost

+0

謝謝。而且我是內部回聲的強大反對者,同時它會降低性能,降低可讀性。我更喜歡給循環內的一個虛擬變量賦值並在循環之後輸出它($ out。=「..」) –