나는 세 개의 파일을 가지고 conn.php
, func.php
그리고 index.php
에서 conn.php
i는 데이터베이스와 나의 연결되어 index.php
있는 동안 나는 연결 파일을 포함 시켰 func.php
내가 함수를했다고 열 카운트 수
func.php
<?php
function countusers($connection, $column, $table)
{
$stmt = mysqli_prepare($connection, "
SELECT
COUNT($column)
FROM
$table");
if($stmt)
{
mysqli_stmt_bind_result($stmt, $num);
mysqli_execute($stmt);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
}
return $num;
}
그러나 기능에 연결 파일을 추가하려고 시도했지만 작동하지 않는 것 같습니다.
여기 내 색인 파일이 있습니다.
index.php
<?php
require_once "conn.php";
include "func.php";
echo countusers($conn, "id", "mods");
conn.php
<?php
$conn = mysqli_connect("localhost", "root", "", "test", 3306);
if(!$conn)
{
echo "An Error Occurred";
}
php.net의 빠른 읽기에 따르면 bind_result는 실행 후에 발생해야합니다 ... 나는 이것을 실행하지 않았으므로 제안 일뿐입니다.
그래서 시도해 볼 수 있습니다 ...
//... SNIP...
if($stmt)
{
mysqli_stmt_execute($stmt); // Changed this to mysqli_stmt_execute
mysqli_stmt_bind_result($stmt, $num);
mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);
}
else {
$num = 0;
}
return $num;
// ... SNIP ...
또한 $ stmt가 false 인 경우 $ num의 기본값은 무엇입니까? 당신은 그것을 결정하고 설정해야합니다.
업데이트 : 어떤 이유로 든 stmt가 실패하는 경우 $ num의 기본값이 추가되었습니다. 이 코드를 실행 한 후 작동합니다.
코드 조각에서 제안 된 변경 사항은 $ num에 대한 값을 제공하지만 원래 코드는 그렇지 않습니다.
사용 된 실제 방법 / 구현의 타당성에 관해서는이 답변이 당면한 질문을 직접 해결했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다