【问题标题】:My mysql connection returns on error?我的mysql连接返回错误?
【发布时间】:2012-03-28 18:35:06
【问题描述】:

我正在尝试连接到 api 并从捐赠系统获取用户,然后打开游戏的套接字以自动向用户提供他们捐赠的金额。我需要摆脱这个错误: “您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '$r' 附近使用正确的语法”

我似乎看不出问题出在哪里?这是脚本:

<?php
$tablename="CENSORED";
$DBUSER="CENSORED";
$DBPASSWORD="CENSORED";
$DBHOST="CENSORED";
?>
<?php

$urlMask = 'CENSORED';    

$getUser = function($id) use ($urlMask) {
    list($user) = json_decode(file_get_contents(sprintf($urlMask, $id)));
    return (object) $user;
};

$user = $getUser(4087396);

$Username = $user->user->username;
$Rank = $user->item_name;
$IGN = $user->custom_field;
echo '<center> Your username is '.$IGN.' correct? </center> ';
?>

<?php
if(isset($_POST['Clickbutton'])){

    $con = mysql_connect($DBHOST,$DBUSER,$DBPASSWORD);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());

  }
mysql_select_db($tablename, $con);
$sql="SELECT IGN FROM fisktable WHERE IGN='$IGN' and Rank='$Rank'" ;
$r = mysql_query($sql);
if(!$r) {
   $err=mysql_error();
   print $err;
}

    $result = mysql_query('$r') or die(mysql_error());
    if(mysql_num_rows($result) == 1) {
echo 'That username has already been given their rank!';
    } else {
         $HOST = "77.45----"; //the ip of the bukkit server
         $password = "chdfxfdxh";
        //Can't touch this:
        $sock = socket_create(AF_INET, SOCK_STREAM, 0)
        or die("error: could not create socket\n");
        $succ = socket_connect($sock, $HOST, 4445) 
        or die("error: could not connect to host\n");
        //Authentification
        socket_write($sock, $command = md5($password)."<Password>", strlen($command) + 1)
        or die("error: failed to write to socket\n");
        //Begin custom code here.
        socket_write($sock, $command = "/Command/ExecuteConsoleCommand:pex user ($IGN) group set ($Rank);", strlen($command) + 1) //Writing text/command we want to send to the server
        or die("error: failed to write to socket\n");
        socket_write($sock, $command = "Thanks, ($IGN) for donating to the ($Rank) rank! ;", strlen($command) + 1)
        or die("error: failed to write to socket\n");
    mysql_select_db($tablename, $con);
$sql="INSERT INTO $tablename(IGN,Rank) VALUES ('$IGN','$Rank')" ;
        exit();
}}
?>
<center>
<form method="POST">
<input name="Clickbutton" type="submit" value="Yes! I would like to receive my rank!"/> 
</form>
</center>

我正在尝试检查用户是否已经获得了他们的排名和项目,方法是在用户给出他们的项目时将它们添加到数据库中。然后如果他们尝试做两次,他们会得到一个错误,说他们已经被赋予了他们的等级!

如果您发现任何其他问题或潜在问题,请随时指出。 谢谢!

【问题讨论】:

  • 可以在查询编辑器中运行 sql 语句吗?您选择的列的数据类型是什么?
  • 好像你在那里留下了密码——如果你打算保留它,你可能应该更改它。
  • 您正在将字符串 $r 发送到 MySQL。删除它周围的引号:mysql_query( $r )
  • @Juhana 所说的 - 如果找不到,请在第 41 行
  • 这里有所有编辑/帖子的历史记录。如果您还没有更改密码,您应该在 bukkit 服务器端更改密码。

标签: php mysql sockets connection


【解决方案1】:

基本的 PHP 语法错误:

$result = mysql_query('$r') or die(mysql_error());
                      ^--^--- remove the quotes

单引号字符串不插入值。您将文字 $r 作为查询传递给 mysql。

【讨论】:

  • 大声笑,真是个愚蠢的错误!谢谢!而且它运行速度很慢,加载大约需要一分钟。这对用户来说看起来不太好。这是因为连接还是什么?
  • 可能。注释掉套接字的东西,看看它是否会加速(可能会)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 2015-02-10
  • 1970-01-01
相关资源
最近更新 更多