具有远程MySQL的PHP​​许可证密钥

时间:2014-07-03 06:23:05

标签: php mysql

我用这个表创建了一个数据库

    CREATE TABLE IF NOT EXISTS `software` (
  `ID` int(255) NOT NULL AUTO_INCREMENT,
  `CompanyName` varchar(255) NOT NULL,
  `valid` int(1) NOT NULL DEFAULT '0',
  `license` varchar(255) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

并在另一台服务器上创建licence.php以连接远程mysql

        <?php
    function connect(){
$dsn = 'mysql:dbname=NAME;host=xx.xx.xx.xx';
$user = 'USER';
$password = 'PASS';
try {
    $dbh = new PDO($dsn, $user, $password);
    // run query
    $license = "test";
    $sth = $dbh->prepare("SELECT * FROM software WHERE license='$license' AND valid='1'");
    $sth->execute();
    $result = $sth->fetchAll();
    if ($sth->rowCount() < 1) {
        die("Your License Has Expired!");
    }
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
}
    ?>

更新:什么都没发生!只是空白页面(有效= 0)或(有效= 1)

有什么问题?

3 个答案:

答案 0 :(得分:1)

$dbh功能之外的connect()不存在。如果你想在外面使用它,你必须退回它。

function connect(){
        $dsn = 'mysql:dbname=NAME;host=xx.xx.xx.xx';
        $user = 'USER';
        $password = 'PASS';    
        try {
            $dbh = new PDO($dsn, $user, $password);
        } catch (PDOException $e) {
            echo 'Connection failed: ' . $e->getMessage();
            return false;
        }
        return $dbh;
}

答案 1 :(得分:1)

您的问题是您正在定义connect()功能而不是调用它。您似乎正在进行冗余编码。

你应该只是运行这样的东西:

$dsn = 'mysql:dbname=NAME;host=xx.xx.xx.xx';
$user = 'USER';
$password = 'PASS';
try {
    $dbh = new PDO($dsn, $user, $password);
    // run query
    $license = "test";
    $sth = $dbh->prepare("SELECT * FROM software WHERE license='$license' AND valid='1'");
    $sth->execute();
    $result = $sth->fetchAll();
    if ($sth->rowCount() < 1) {
        die("Your License Has Expired!");
    }
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

此外,而不是:

if (count($result) === 0) {

您应该使用原生PDO rowCount

if($sth->rowCount() < 1) {....

以上将检查行数是否小于1(有效 0

答案 2 :(得分:0)

connect()函数

$sth = $dbh = ( 应该是$sth = $dbh->prepare(