简体   繁体   English

用php读取mysql并在下拉菜单中显示结果

[英]Reading mysql with php and displaying result in dropdown menu

i'm farly new to php and are trying to make a php script where it's suppose to connect to a mysql db and get the vaule id, then show as an option in a drop down menu. 我是php的新手,正在尝试制作一个php脚本,该脚本应该连接到mysql db并获取有效ID,然后在下拉菜单中显示为选项。

This is the code I have so far (got help from a friend): 这是我到目前为止的代码(得到朋友的帮助):

$username = "root";
$password = "";
$hostname = "localhost"; 
$database = "customers";
$id = "";

mysql_connect("$hostname", "$username", "$password") or die (mysql_error()) or die (mysql_error());

mysql_select_db("$database") or die (mysql_error());

$result = mysql_query("SELECT id FROM users WHERE id='$id'") or die(mysql_error());
      while ($row = mysql_fetch_assoc($result)) {
       $valuestring = $row['id'];
       print_r($result);
          echo "<option value='$valuestring'>". $valuestring ."</option>";
          mysql_close();
      }
print_r($id);

But when I use this code the option is returned empty :/ 但是,当我使用此代码时,该选项返回空:/

I have also tried to do print_r($result); 我也尝试做print_r($ result); and that give me Resource id #4, so I guess that works. 这给了我资源ID#4,所以我想这可行。

If anyone could help me solve this I would be one happy guy :D 如果有人可以帮助我解决这个问题,我将是一个快乐的人:D

the $id value in your code is empty, are you avare of that ? 您代码中的$ id值是空的,您可以使用吗? and can you print the query before sending it to mysql ? 并且可以将查询发送到mysql之前打印查询吗? use : 采用 :

"$query = "select id from table where id = '$id'";
mysql_query($query);
echo $query;

Perhaps if you showed us what output you did get it would help. 也许如果您向我们展示了您获得的输出将会有所帮助。

the option is returned empty 该选项返回空

If the output includes HTML generated inside the loop then that means the query returned at least 1 row. 如果输出包括在循环内部生成的HTML,则意味着查询返回了至少1行。 But the only way that echo "<option value='$valuestring'>" would produce an empty string is if $valuestring was an empty string. 但是, echo "<option value='$valuestring'>"的唯一方法是生成一个空字符串,如果$ valuestring是一个空字符串。 It's populated from "SELECT id FROM users WHERE id='$id'" implying that you must have a row in your database where id is null or an empty string and $id in your php code is null/empty string - indeed that is the case ($id = "";). 它是从“ SELECT id FROM users WHERE id ='$ id'”填充的,这意味着您必须在数据库中有一行,其中id为null或一个空字符串, $ id在您的php代码中为null /空字符串-实际上,这就是大小写($ id =“”;)。

NTW the mysql_close(); NTW mysql_close(); should be outside the loop. 应该在循环之外。

Let's simplify this code: 让我们简化这段代码:

<select name="user" id="user" width="200px" style="width: 200px">
<option value="">Select State</option>
<?php       
$query_uf =  "SELECT id FROM users WHERE id="'.$id.'";      
$result = mysql_query($query_uf,$bd);       
while ($users =mysql_fetch_assoc($result)) {
echo "<option value='".$uf['id']."'>".$uf['user']."</option>";      } 
?>
</select>

Obs: It's better you use mysqli_query and connect. Obs:最好使用mysqli_query并连接。 And, in your code it's missing the connection in mysql_query. 而且,在您的代码中,它缺少mysql_query中的连接。

I've previously needed to retrieve a list of albums from a table using a similar method, maybe try this function. 我以前需要使用类似的方法从表中检索专辑列表,也许可以尝试使用此功能。 Create your form and call the function within the and tags leading this to work. 创建您的表单,然后在和标记中调用函数以使其正常工作。 This should list your id(s) where specified in the query. 这应列出您在查询中指定的ID。

functions.php (or wherever you'd like to put the function): functions.php(或您想放置函数的任何地方):

function stateList() {
$username = "username"; 
$password = "password"; 
$host = "localhost"; 
$dbname = "dbname"; 
$id = RETRIEVE VALUE HERE;
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
$query = " 
    SELECT 
    id,
    FROM users 
    WHERE
    id = $id // YOU MAY WANT TO REMOVE WHERE - $ID AS STATED ABOVE, DOESN'T MAKE SENSE.
    "; 

    try 
    { 
        $stmt = $db->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
        $valuestring = $row['id'];
    $rows = $stmt->fetchAll(); 
foreach($rows as $row):
    print "<option value='" . $valuestring . "'>" . $valuestring . "</option>";
endforeach;
}
?>

Selection page: 选择页面:

<? include 'functions.php' ?> <!-- This will allow you to call the function. -->
<form action="example.php" method="post" enctype="multipart/form-data">
    <select name="album">
        <? stateList(); ?> <!-- Calls the function and retrieves all options -->
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM