-1

*强文本*我不知道标题是否清楚,

但我想要一个 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';
     }
}
?>
4

3 回答 3

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"; }
于 2012-10-20T06:15:17.537 回答
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';
     }
}
于 2012-10-20T06:14:00.593 回答
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 或 PDO

于 2012-10-20T06:32:46.383 回答