【问题标题】:Create an Community Members List From MySQL从 MySQL 创建社区成员列表
【发布时间】:2015-05-02 18:51:25
【问题描述】:

如何从 mysql 表中创建成员列表。我使用了这段代码,它给了我一个白页。

<?php
 $sql = new mysqli('localhost','DB-USER','PASS','DB'); /

 $sql -> set_charset ( 'utf8' );
 if ($sql->connect_errno) {
    printf("Connect failed: %s\n", $sql->connect_error);
    exit();
 }

 while ($fetch = $profile_query_check->fetch (PDO::FETCH_ASSOC)) {
    if ($fetch == NULL OR $fetch["id"]) {
        break;
    }


    $username = $fetch['username'];
    $avatar = $fetch['avatar'];
    $id = $fetch['id'];

    //echo the profile info
}
 ?>

这是用户表

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(50) NOT NULL,
  `level` int(11) NOT NULL,
  `username` varchar(50) NOT NULL,
  `password` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `usertype` int(11) NOT NULL,
  `rpname` varchar(50) NOT NULL,
  `fbname` varchar(50) NOT NULL,
  `avatar` varchar(100) NOT NULL,
  `avatarchangedate` int(11) NOT NULL,
  `new_activity_count` int(11) NOT NULL,
  `online` tinyint(4) NOT NULL,
) ENGINE=MyISAM AUTO_INCREMENT=13397 DEFAULT CHARSET=utf8;

请帮助我如何创建代码?我是菜鸟

更新: 但是这个 whoisisonline 代码正在运行

<?php

if(!defined('A')){
  include "error.php";die(header('HTTP/1.0 403 Forbidden'));
}

global $sql;

//http://www.webmonkey.com/2010/02/how_many_users_are_on_your_site_right_now/
//Checks the sesson file to see how many files there are
function usersonline() {
    $sessionfiles = session_save_path() . "/sess*";

    $usersonline = count(glob($sessionfiles));

    return $usersonline;
}

if(isset(user::$current->id)){
    $current_ID = user::$current->id;
}else{
    $current_ID = "";
}

$sql->run("select_who_is_online", array(
    "userID" => $current_ID,
    "time" => (time() - 180)
));

// $sql->last_query_info();

$users = $sql->fetch_array();

$count = count($users);


$members_online = $count;

if (user::$loggedIn) {
    $members_online++;
}

$vistors_online = (usersonline() - $members_online);


//plural check
if ($members_online == 0)
    $members_online_message = "There are currently no NEETs online";
else if ($members_online == 1)
    $members_online_message = "There is currently only one NEET online";
else
    $members_online_message = "There are currently ".$members_online." NEETs online";

if($vistors_online == 1)
    $vistors_online_message = "and ".$vistors_online." Person passing by";
else
    $vistors_online_message = "and ".$vistors_online." People passing by";



echo "<div class='content_header blue small align_left' style='margin-top: 20px;'>Who is Online";
echo "<x style='float:right;margin-right:8px'>".$members_online_message.", ". $vistors_online_message ."<x>";
echo "</div>";

// echo "<h1 class='content_centered'>Who is Online</h1><br/>";
echo "<div class='content_padded content_centered content_feed light'>";
if($count){
    foreach($users as $user){
        // print_r($user);
        $user_obj = new user($user["id"]);
        $gUser = new gUser($user_obj);

        $tmp = '';
        $tmp .= "<div style='text-align: center; display: inline-block; width: 110px; margin-bottom: 10px;'>";
        // if ($friend->online)
        $tmp .= "<div class='basicSuccess' style='display: block; margin-bottom: 3px; width: 73px;'><i class='fa fa-wifi'></i> Online</div>";
        $tmp .= $gUser->htmlAvatar(70, "middle");
        $tmp .= "<div style='margin-top: 5px;'>" . $gUser->htmlSmall() . "</div>";
        $tmp .= "</div>";
        echo $tmp;
    }
}else if(user::$loggedIn){
    echo "<h5>No one else is online T^T</h5>";
}else {
    echo "<h5>No one is online T~T</h5>";
}


echo "</div>";

?>

【问题讨论】:

  • 空白页要么意味着您没有打印任何内容(在这种情况下,因为没有返回结果),要么意味着您的代码中有错误。在您的new mysqli 呼叫末尾,分号后面有一个斜线;是在您的原始代码中吗?
  • 有一个错字...从第一行删除/
  • 试过了,还是没用

标签: php html mysql sql


【解决方案1】:

看看下面的代码。您在第二行有一个额外的 \ 并且从未实际输出数据。

 $sql -> set_charset ( 'utf8' );
 if ($sql->connect_errno) {
    printf("Connect failed: %s\n", $sql->connect_error);
    exit();
 }

 while ($fetch = $profile_query_check->fetch (PDO::FETCH_ASSOC)) {
    if (!$fetch) {
        break;
    }


    $username = $fetch['username'];
    $avatar = $fetch['avatar'];
    $id = $fetch['id'];

    //echo the profile info
    echo "Username: " . $username . "<br />";
    echo "Avatar : " . $avatar;

}

【讨论】:

  • 在这之后,它把空白白页扔给我,而我的其他 whoisonline 页面工作得很好。
  • 最后 2 个回显是 "Username:" 。 $username 但你不附加字符串。你只是把 br 放在没有任何东西的地方,然后打开一个字符串,据我所知,虽然有限,但它是错误的 xD,因为它破坏了它的其余部分。
  • 感谢余震,已修复。
  • 我试过那个 Colin,但还是不行。只给出一个空白页
  • 试用更新版本。您在 while 循环中的条件语句中有一个奇怪的条件。
猜你喜欢
  • 2015-10-05
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 2015-08-01
  • 2010-12-01
  • 2020-02-28
相关资源
最近更新 更多