2016-04-21 88 views
-2

mysqli select語句有點問題,因爲我做了一個select語句,它實際上會計算結果的數量。但它不會返回我想要的值,但它不返回任何值。需要幫助的人。我沒有使用的mysqli和PHP這個SELECT語句作爲函數mysqli語句不能在php下工作

function count_result($data){ 
    global $con; 
    $sql = "SELECT count(user_id) as userssss from credentials where user_id = '$data'"; 
    $result = mysqli_query($con,$sql) or die('userssss'); 
    echo "string</br>"; 
    $row = mysqli_fetch_assoc($result,MYSQLI_ASSOC); 
    echo $row['userssss']."asdasd</br>"; 
    die("userssss"); 
    $return = $row['user']; 

    return $return; 
} 

結果


asdasd
userssss

它應該顯示asdasd之前的結果

+0

檢查'mysqli_query'和'mysqli_fetch_assoc'的結果。使用預先準備好的語句, – chris85

+0

'user'是一個保留字 – FuzzyTree

+0

我不想使用預備語句。只需要一個簡單但易於理解的代碼。我正在檢查它,我認爲mysqli_query和mysqli_fetch_assoc存在問題。除了準備好的聲明之外的任何建議,即使除了用戶使用其他單詞,它仍然不會返回預期的輸出。 – NewbieProggie

回答

0

我發現了。我傻了。

而不是使用副教授,必須使用陣列

function count_result($data){ 
    global $con; 
    $sql = "SELECT count(user_id) as userssss from credentials where user_id = '$data'"; 
    $result = mysqli_query($con,$sql) or die('userssss'); 
    $row = mysqli_fetch_array($result,MYSQLI_ASSOC); 
    $return = $row['user']; 

    return $return; 
} 
+0

這也是錯誤,我猜是主要問題'$ row ['usersss']',你已經用'$ row ['user']' – Nehal

0

添加全局$ CON;

function count_result($data){ 
    global $con; 
    $sql = "SELECT count(user_id) as user from credentials where user_id = '$data'"; 
    $result = mysqli_query($con,$sql); 
    $row = mysqli_fetch_assoc($result,MYSQLI_ASSOC); 
    echo $row['user'][0]."asdasd"; 
    die(); 
    $return = $row['user'][0]; 

    return $return; 
} 
+0

我更新了關於您的建議的帖子。 – NewbieProggie

0

一個愚蠢的錯誤在你的代碼:

function count_result($data){ 
    global $con; 
    $sql = "SELECT count(user_id) as userssss from credentials where user_id = '$data'"; 
    $result = mysqli_query($con,$sql) or die('userssss'); 
    echo "string</br>"; 
    $row = mysqli_fetch_assoc($result,MYSQLI_ASSOC); 
    echo $row['user']."asdasd</br>"; // did changes on this line 
    die("userssss"); 
    $return = $row['user']; 

    return $return; 
} 
+1

替換了'請不要轉發我的答案只是爲了獲得點。很明顯,你正在這樣做。 – NewbieProggie

+0

@NewbieProggie,我在你的代碼中也提到過這個問題,你需要檢查我在你的答案中提到的評論。由於這個錯誤,我發佈了新的答案 – Nehal

0

你需要計算匹配where子句一切意味着行。也嘗試採用準備好的語句。波紋管代碼起作用。

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 
function count_result($data){ 
$user = 'username'; 
$password = 'password'; 
$db = 'database'; 
$host = 'hostname'; 
$port = 3306; 

/* Attempt MySQL server connection. Assuming you are running MySQL server */ 

$link = mysqli_connect($host, $user, $password, $db); 

// Check connection 
if($link === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
} 

if($stmt = $link -> prepare("SELECT COUNT(*) FROM test WHERE ID= ?")) 
    { 
    /* Bind parameters, s - string, b - blob, i - int, etc */ 
    $stmt -> bind_param("i", $data); 
    $stmt -> execute(); 

    /* Bind results */ 
    $stmt -> bind_result($testfield1); 

    /* Fetch the value */ 
    $stmt -> fetch(); 
    $numberofrows = $stmt->num_rows; 

} else{ 
    echo "ERROR: Could not able to execute SQL. " . mysqli_error($link); 
} 

/* Close statement */ 
$stmt -> close(); 
echo '# rows: '. $numberofrows . PHP_EOL; 
echo 'Count = '. $testfield1 ; 

} 

count_result(24); 
?>