我正在尝试编写一个 PHP 函数来检查给定值是否在数据库的列中。该函数接受参数$userName
,并且将在数据库的列中检查$link
位置。这是我到目前为止所得到的:$userName
userid
$link
<?php
/*This function checks to see if the username is already in use
INPUT: the userName to check for and a link to the database
OUTPUT: true if there are no other users registered with userName
*/
function isUnique($userName, $link)
{
$result = !!mysqli_fetch_row(mysqli_query($link, 'SELECT `userid`
FROM `login`
WHERE `userid` = \''.$userName.'\''));
return $result;
}
echo '<html><head><title>testing</title><body>';
$uName = $_POST['us'];
$pass = $_POST['pa'];
//database work
$link = mysqli_connect("localhost:3306", "root", "");
if(!$link)
die('Could not connect to MySQL: '.mysql_error());
$db_selected = mysqli_select_db($link, 'Accounts');
if(!$db_selected)
die('Can\'t use Accounts: '.mysql_error());
echo '<br />Using Accounts database<br />';
if(isUnique($uName, $link))
{
echo 'username is unique!<br />';
}
else
{
echo 'username is not unique!<br />';
}
mysqli_close($link);
echo '</body></html>';
?>
该页面总是说给定的用户名不是唯一的。