php - MySQL 不查询

标签 php mysql database

记录器显示我已连接到数据库,但我无法正确查询或添加表,因为我收到“主表失败”然后“玩家不在数据库中”然后“无法添加条目”然后“当我加入游戏时,“玩家不在数据库中”,因此这显然是我的 MySQL 方面的错误。如果有人能指出我正确的方向,那就太好了!

<?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!");
        }
    }
}

最佳答案

您的表未创建,因为 TEXT 列设置为主键。您不能将 TEXT 列设置为主键。错误是

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

有关更多信息,请参阅链接-

MySQL error: key specification without a key length

MySQL error: key specification without a key length

关于php - MySQL 不查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31129315/

相关文章:

php - 什么情况下可能不继承父字段?

mysql - 试图获取单行数据 - Mysql

php - session 未与用户名 PHP 相关联

mysql - 如何为mysql的位列插入值

mysql - 从一张表中选择数据。然后使用该数据从另一个表获取信息

php - Codeigniter 调用我不调用的表数据库

php - 基于数量计算的产品类别的购物车折扣

node.js - MongoDB (Node.js) 发现没有返回预期结果

mysql - 如何获得 MySQL "Explain"的虚拟值?

php - 在 lampp 套件中编译 gmp php 扩展时缺少 config.m4 文件