본문 바로가기
MySql

MySQL 데이터베이스에서 PHP 드롭 다운 목록 채우기

by 베이스 공부 2020. 11. 4.
반응형

하나의 열 (pathology_id) 만있는 mysql 데이터베이스 테이블에서 웹 페이지의 드롭 다운 목록을 채우려 고합니다. 거기에 테스트 데이터가 있다는 것을 알고 있지만 할 수있는 최선은 행 값이 아닌 필드 이름으로 상자를 채우는 것입니다. 지금까지 내가 가지고있는 코드는 다음과 같습니다. 누구나 열 이름 이상을 얻는 방법을 제안 할 수 있습니까? 미리 감사드립니다.

<?php $con = mysql_connect("localhost","dname","dbpass");
    if(!$con)
    {
        die('Could not connect: ' . mysql_error());
    }

    $fields = mysql_list_fields("dbname","PATHOLOGY",$con);
    $columns = mysql_num_fields($fields);
    echo "<form action = newcase.php method = POST><select name = Field>";
    for($i = 0; $i < $columns ; $i++)
    {
        echo "<option value = $i>";
        echo mysql_field_name($columns , $i);
    }

    echo "</select></form>";

    if(!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    else
    {
        echo "1 record added";
    }

    mysql_close($con) ?>

 

해결 방법

 

이 시도:

<?php
// This could be supplied by a user, for example
$firstname = 'fred';
$lastname  = 'fox';

// Formulate Query
// This is the best way to perform an SQL query
// For more examples, see mysql_real_escape_string()
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
    echo $row['firstname'];
    echo $row['lastname'];
    echo $row['address'];
    echo $row['age'];
}

// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>


mysql_list_fields 는 포함 된 데이터가 아닌 주어진 테이블에 대한 정보 만 반환합니다.

 

참조 페이지 https://stackoverflow.com/questions/3889230

 

 

반응형

댓글