gpt4 book ai didi

php - 如何从 MySQL 表中获取值 - PHP

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:06 24 4
gpt4 key购买 nike

我很难从我的 sql 数据库中获取值。我的代码在下面,我试图显示 $balance,但是当我回显它时,它只显示“Array”。

这是我的数据库表的图像: database table

<?php 
session_start();
echo "<!DOCTYPE html>\n<html><head><script src='OSC.js'></script>";
include 'functions.php';

$userstr = ' (Guest)';

if (isset($_SESSION['user']))
{
$user = $_SESSION['user'];
$loggedin = TRUE;
$userstr = " ($user)";
$balancequery = mysql_query("SELECT balance FROM members WHERE user = '$user'");
$balance = mysql_fetch_assoc($balancequery);
}
else $loggedin = FALSE;

echo "<title>$appname$userstr</title><link rel='stylesheet' " .
"href='styles.css' type='text/css' />" .
"</head><body><div class='appname'>$appname$userstr</div>";

if ($loggedin)
{
echo "<br ><ul class='menu'>" .
"<li><a href='members.php?view=$user'>Home</a></li>" .
"<li><a href='members.php'>Members</a></li>" .
"<li><a href='stockdata.php'>Stocks</a></li>" .
"<li><a href='logout.php'>Log out</a></li></ul><br />";
}
else
{
echo ("<br /><ul class='menu'>" .
"<li><a href='index.php'>Home</a></li>" .
"<li><a href='signup.php'>Sign up</a></li>" .
"<li><a href='login.php'>Log in</a></li></ul><br />" .
"<span class='info'>&#8658; You must be logged in to " .
"view this page.</span><br /><br />");
}
?>

最佳答案

mysql_fetch_assoc返回该行的字段的关联数组。要回显该行中的字段,您必须为其提供正确的索引,这将是该字段的名称:

echo $balance["balance"];

mysql php 扩展也已弃用。请查看MySQLiPDO_MySQL .

关于php - 如何从 MySQL 表中获取值 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com