* 强文 *我不知道标题是否清晰,
但是我想要一个PHP脚本来告诉MySQL列是否有数据,如果打印一行文本,但是如果 NULL ,则为打印别的东西......
有意义吗?
这就是我所拥有的,我知道这是完全错误的......
$link = mysql_connect($DBHOST, $DBUSER, $DBPASSWORD);
mysql_select_db($DBNAME);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_assoc($result))
{
if($row['VIP'] = '1')
{
echo('VIP');
}
else
{
echo('Non-VIP');
}
}
修改::
以下是完整代码:
<?php
$DBTYPE = 'mysql';
$DBHOST = 'host';
$DBUSER = 'username';
$DBPASSWORD = 'password';
$DBNAME = 'database';
$link = mysql_connect($DBHOST, $DBUSER, $DBPASSWORD);
mysql_select_db($DBNAME);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$member_id= '5'; // get value of the member, assumed that you have post the data into id. change if it is different into your code
$result = mysql_query("SELECT * FROM members where id=$member_id");
while($row = mysql_fetch_assoc($result))
{
if($row['VIP'] == '1')
{
echo "<img src='../images/VIP.png' />";
}
else
{
echo 'Non-VIP';
}
}
?>
答案 0 :(得分:1)
$result = mysql_query("SELECT * FROM members");
if(mysql_num_rows($result) > 0) // or you can check for 1 too
{
$row = mysql_fetch_assoc($result);
// your text output here
//echo VIP or whatever
}
else { echo "non-vip"; }
答案 1 :(得分:0)
使用==检查相等性,如下所示:
if($row['VIP'] == '1')
{
echo('VIP');
}
else
{
echo('Non-VIP');
}
修改强>
使用以下代码获得所需结果
$link = mysql_connect($DBHOST, $DBUSER, $DBPASSWORD);
mysql_select_db($DBNAME);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$member_id=$_POST['id']; // get value of the member, assumed that you have post the data into id. change if it is different into your code
$result = mysql_query("SELECT * FROM members where id=$member_id");
while($row = mysql_fetch_assoc($result))
{
if($row['VIP'] == '1')
{
echo "<img src='../images/VIP.png' />";
}
else
{
echo 'Non-VIP';
}
}
答案 2 :(得分:0)
如果我理解正确,您希望显示用户是VIP
还是Regular User
。如果是这样,您需要修改SELECT
以获取用户行,而不是每行。
$id = ...; //logged in user id [ie. $loggedin->id, $_SESSION['id'], etc]
$result = mysql_query("SELECT * FROM members WHERE id = $id");
while($row = mysql_fetch_assoc($result)){
if($row['VIP'] == '1')
{
echo('VIP');
}
else
{
echo('Non-VIP');
}
}
请注意,不应使用mysql
函数编写新代码。您应该学会使用mysqli or PDO