db 쿼리를 준비된 구문으로 변환하는 중입니다. 다음과 같은 시나리오가 주어지면 어떻게 결과 바인딩을 수행 할 수 있습니까? 즉조건부 결과 바인딩
function getRecord($type = 1)
{
$sql = "select field1, field2, field3";
if ($type == 2) $sql .= ", field4, field5";
$sql .= " from table";
$sql .= " where field1 = 'somevalue'";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
if ($record = mysqli_fetch_assoc($result))
{
$val1 = $record['field1'];
$val2 = $record['field2'];
$val3 = $record['field2'];
if ($type == 2)
{
$val4 = $record['field4'];
$val5 = $record['field5'];
}
}
}
:
function getRecord($type = 1)
{
$stmt = mysqli_stmt_init($conn);
$sql = "select field1, field2, field3";
if ($type == 2) $sql .= ", field4, field5";
$sql .= " from table";
$sql .= " where field1 = ?";
if (mysqli_stmt_prepare($stmt, $sql))
{
mysqli_stmt_bind_param($stmt, 's', 'somevalue');
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $record['field1'],
$record['field2'],
$record['field3']);
if ($type == 2)
{
// How do I bind the rest of the results?
}
if (mysqli_stmt_fetch($stmt))
{
// Do something
}
mysqli_stmt_close($stmt);
}
}
어떻게 결과 경우 ($ 유형 = 2)의 나머지 부분을 결합 하는가?
귀하의 질문은 이보다 조금 더 구체적이어야합니다. –
@RobertHarvey 그 밖의 무엇을 알아야합니까? – gr8dane
@ gr8dane 그냥 확인 : http://php.net/manual/es/mysqli.prepare.php –