我试图复制How to Check if value exists in a MySQL database并制作我自己的,但由于某种原因它不会工作......
这就是我得到的:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con=mysqli_connect($host, $username, $password, $dbname);
$check_player_ip=mysqli_query($con, 'SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0');
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
} else{
//found something
}
?>
我收到此错误: 解析错误:语法错误,意外&#39; $ check_player_ip&#39;第1行的C:\ xampp \ htdocs \ test.php中的(T_VARIABLE)
解决方案
如果收到T_VARIABLE错误,请检查此规则之前的变量。你可能会忘记&#39 ;;&#39;的xD
感谢所有支持!
答案 0 :(得分:2)
<击>
您正在混合MySQL API,它们不会混合在一起。 mysql_num_rows
击>
使用mysqli_num_rows()
另外,请确保您的数据库连接也是mysqli_*
而不是mysql_*
<击> 撞击>
编辑完问题后,编辑。
您需要将DB连接传递给您的查询:
$check_player_ip=mysqli_query($con,'SELECT...`
$con
是您的数据库连接。相应地改变。
加WHERE username = remco
- 单词remco
需要用引号括起来,它是一个字符串而不是int
。
WHERE username = 'remco'
<强> 旁注: 强>
您现有的代码向SQL injection开放 使用prepared statements或PDO with prepared statements。
编辑2:
尝试反转引号:
$check_player_ip=mysqli_query($con, "SELECT `player_ip` FROM `playerdata` WHERE username = 'remco' AND active = 0");
if (mysqli_num_rows($check_player_ip) == 0) {
//didnt find anything
}
答案 1 :(得分:1)
你应该执行那个mysqli查询...
while($rows = mysql_arrayAssoc($ursql)){
$data[]=$rows;
}
if($something== $data['attribute']) //attribute(id,name...)
echo "ok some data is in"
else
echo "no matching data"
我希望它可以帮到你:)。
答案 2 :(得分:1)
试试这个:
<?php
$host = '127.0.0.1';
$username = 'root';
$password = '';
$dbname = 'multiplayer';
$con = new mysqli( $host, $username, $password, $dbname );
/* Check Connection */
if ( $con->connect_error ) {
printf( "Connect failed: %s\n", $con->connect_error );
exit();
}
/* Query - Returns a resultset */
if ( $result = $con->query('SELECT `player_ip` FROM `playerdata` WHERE username = "remco" AND active = 0 ') ) {
if ( $result->num_rows <= 0 ) {
//didnt find anything
printf("No player");
} else {
//found something
printf("Select returned %d rows.\n", $result->num_rows);
}
/* free result set */
$result->close();
}
/* close connection */
$con->close();
?>