亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MYSQL 獲取列值并在PHP中顯示

MYSQL 獲取列值并在PHP中顯示

PHP
慕婉清6462132 2021-06-29 22:41:33
我有個問題我想回顯“點”列的值我嘗試過,但沒有用:$stmt = $mysqli->prepare("SELECT points FROM member_profile WHERE user_id = '$firstName'");$stmt->execute();$array = [];foreach ($stmt->get_result() as $row){    $array[] = $row['points'];}print_r($array);這是我當前的代碼:<?phpheader('Content-Type: text/html; charset=Windows-1250');    session_start();$firstName = $_SESSION['firstname'];$servername = "db.xxxx.gsp-europe.net";$username = "xxxxxxxxxxxxx";$password = "xxxxxxxxxxxxxx";$dbname = "xxxxxxxx";/// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connection   if ($conn->connect_error) {    die("Connection failed: " . $conn->connect_error);} // check if the user exist$check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";$result = mysqli_query($conn,$check) or die(mysqli_error($conn));        $rows = mysqli_num_rows($result);        //if exist increse points with 1        if($rows>=1){$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";   if ($conn->query($sql) === TRUE) {    echo "Thingz created successfully";} else {    echo "Error doing sum thingz: " . $conn->error;}        }        //if don't exist create user with points 0        if($rows==0)        {            $query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";            $result = mysqli_query($conn,$query)or die(mysqli_error($conn));$conn->close();        }?>簡而言之,我需要什么:在文件的末尾,將是一個“回聲”,它將顯示帶有標識符“user_id”的“點”列的當前值。就這樣
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

您正在獲取結果,但未獲取數據。


$stmt->get_result()返回一個結果集 -> mysqli_result并且要處理這個,您需要從該結果調用方法fetch_array()。


將您的代碼更改為:


$results = $stmt->get_result();

while ($row = $result->fetch_array(MYSQLI_ASSOC))

{

    $array[] = $row['points'];

}

如果您只想要 1 個結果而不使用數組,請不要使用數組(是的,是的)。


$results = $stmt->get_result();

while ($row = $result->fetch_array(MYSQLI_ASSOC))

{

    $points = $row['points'];

}



查看完整回答
反對 回復 2021-07-02
  • 1 回答
  • 0 關注
  • 194 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號