반응형
하나의 열 (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
반응형
'MySql' 카테고리의 다른 글
MySQL node.js에 사용할 수있는 MySQL 드라이버는 무엇입니까? (0) | 2020.11.04 |
---|---|
MySQL의 멀티 스레딩? (0) | 2020.11.04 |
MySQL MYSQL : 사용자-프로필 세부 정보 테이블 설정-모범 사례 (0) | 2020.11.04 |
MySQL SQL Injection을 사용하여 테이블을 업데이트하려면 어떻게해야합니까? (0) | 2020.11.04 |
MySQL Bin 로그에서 쿼리 검색 (0) | 2020.11.04 |
댓글