简体   繁体   English

我的MYSQL的信息没有显示?

[英]The info from my MYSQL are not showing?

I'm trying to make a social network and are adding a notification bar on the site, but the code is not loading the data from the database. 我正在尝试建立一个社交网络,并在网站上添加了一个通知栏,但是代码没有从数据库中加载数据。

$newpm_sql = mysql_query("SELECT * FROM `pm` 
                          WHERE `to` = '". $_SESSION['id'] ."' 
                          ORDER BY `id` DESC") or die(mysql_error());

if (mysql_num_rows($newpm_sql) == 0) { 
    $newpm = '<div id="notificationTitle">Meddelande</div>
    <div id="notificationsBody" class="notifications">
       Du har inga meddelanden! 
    </div>
'; 
} else {

    while ( $newpm = mysql_fetch_array( $newpm_sql )) {

        $from_sql = mysql_query("SELECT * FROM `members` 
                                 WHERE `id` = '". $newpm['from'] ."'") 
               or die(mysql_error());
        $from = mysql_fetch_array($from_sql);

        if ($newpm['status'] == 0) { 
            $newpm = '<div id="notificationTitle">Meddelande</div>
                      <div id="notificationsBody" notifications">'. 
                         $newpm['subject'] .' '. $newpm['from'] .
                       '</div>'; 
        }
    }
}

You are using the variable $newpm for almost everything and therefore destroying what was held in it up to that point. 您几乎对所有内容都使用了变量$newpm ,因此破坏了到目前为止保存的内容。

Use a new variable in the second query, I have used $row as an example below 在第二个查询中使用新变量,下面以$row为例

$newpm_sql = mysql_query("SELECT * FROM `pm` 
                          WHERE `to` = '". $_SESSION['id'] ."' 
                          ORDER BY `id` DESC") or die(mysql_error());

if (mysql_num_rows($newpm_sql) == 0) { 
    $newpm = '<div id="notificationTitle">Meddelande</div>
    <div id="notificationsBody" class="notifications">Du har inga</div>'; 
} else {
    while ( $row = mysql_fetch_array( $newpm_sql )) {

        $from_sql = mysql_query("SELECT * FROM `members` 
                                 WHERE `id` = '". $newpm_sql['from'] ."'") 
               or die(mysql_error());
        $from = mysql_fetch_array($from_sql);

        if ($row['status'] == 0) { 
            $newpm = '<div id="notificationTitle">Meddelande</div>
                      <div id="notificationsBody" notifications">'. 
                         $row['subject'] .' '. $row['from'] .
                       '</div>'; 
        }
    }
}

Also you should not be using the mysql_ database access extension, it is deprecated ie soon to be removed. 另外,您不应该使用mysql_数据库访问扩展,它已被弃用,即将被删除。 As you are obviously learning, spend your time learning the mysqli_ extension or the PDO extension. 显然,您正在学习mysqli_扩展或PDO扩展。 See this for some help deciding which you prefer Why shouldn't I use mysql_* functions in PHP? 请参阅此以获得帮助确定您首选的帮助。 为什么我不应该在PHP中使用mysql_ *函数?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM