drq231358 2015-06-30 04:02
浏览 23

MySQL没有查询

The logger shows that I have connected to the database, but I cannot query to it correctly or add tables because I get "Master Table Failed" then "Player is not in database" then "Could not add entry" and then "Player is not in database" when I join the game so it's clearly an error on my part with MySQL. If someone could point me in the right direction that would be great!

<?php

namespace gladiusdata;

use pocketmine\plugin\PluginBase;
use pocketmine\level\particle\FloatingTextParticle;
use pocketmine\math\Vector3;
use pocketmine\utils\TextFormat;
use gladiusinfo\AnnounceTask;
use gladiusinfo\gladiusinfo;
use pocketmine\utils\Config;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerJoinEvent;

class Main extends PluginBase implements Listener
{
    private $db;

    public function onEnable()
    {
        $this->getServer()->getPluginManager()->registerEvents($this, $this);

        $user = /*username*/;
        $pass = /*password*/;
        $server = /*server*/;
        $database = /*database*/;
        $port = /*port*/;

        $this->db = @mysqli_connect($server, $user, $pass, $database, $port);
        if(!$this->db)
        {
            $this->getServer()->getLogger()->info(TextFormat::RED . "Did not work");
        } else {
            $this->getServer()->getLogger()->info(TextFormat::GREEN . "Worked!");
        }

        if($this->db->query("CREATE TABLE IF NOT EXISTS master (player TEXT PRIMARY KEY COLLATE NOCASE, rank TEXT, bal INTEGER);"))
        {
            $this->getServer()->getLogger()->info(TextFormat::GREEN . "Created master table");
        } else {
            $this->getServer()->getLogger()->info(TextFormat::RED . "Master table failed");
        }
    }

    public function onPlayerJoin(PlayerJoinEvent $pje)
    {
        $this->insert($pje->getPlayer()->getName());
    }

    public function insert($player, $rank = "none", $bal = 0)
    {
        if(!$this->playerExists($player))
        {;
            $sql = "INSERT INTO master (player, rank, bal) VALUES ($player,                 $rank, $bal)";
            if($this->db->query($sql))
            {
                $this->getServer()->getLogger()->info(TextFormat::GREEN . "Added new entry successfully");
            } else {
                $this->getServer()->getLogger()->info(TextFormat::RED . "Could not add entry");
            }
        }
        $this->playerExists($player);
    }

    public function playerExists($player)
    {
        $result = $this->db->query("SELECT * FROM master WHERE player='$player'");
        if($result)
        {
            $this->getServer()->getLogger()->info(TextFormat::GREEN . "Player is in database!");
        } else {
            $this->getServer()->getLogger()->info(TextFormat::RED . "Player is not in database!");
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dpstir0081 2015-06-30 04:22
    关注

    Your table is not creating because TEXT column is set to primary key. You can't set TEXT column as primary key. The error is

    ERROR 1170 (42000): BLOB/TEXT column 'name' used in key specification without a key length
    

    for more information see the links-

    MySQL error: key specification without a key length

    MySQL error: key specification without a key length

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动