我希望MySQL返回一个数字,但它返回一个表

时间:2017-01-17 18:03:15

标签: php mysql

我正在尝试使用此代码:

$servername = "localhost";
$username = "[censored]";
$password = "[censored]";
$dbname = "[censored]";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("disconnected");
}

$secret_token = sha1(rand(1,9999999999));
$sql = "SELECT COUNT(username) FROM `sessions` WHERE username = 'isOnline4'";
echo $conn->query($sql);

if ($conn->query($sql) == 1) {
    echo "I am fairly sure you already have a secret token boi!";
    die;
} else {
    echo "Nope";
}
$conn->close();

我认为$conn->query($sql)会返回1,2或3之类的数字,但它会返回一行。我尝试了几种不同类型的查询,但它们都返回表或包含数字的表。

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情

$obj = mysqli_fetch_object( mysqli_query(your_db_connection_obj , "SELECT COUNT(username) as n FROM `sessions` WHERE username = 'isOnline4'"));

echo $obj->n;

答案 1 :(得分:0)

您唯一需要做的就是更改查询,例如

$sql = "SELECT COUNT(username) as number FROM `sessions` WHERE username = 'isOnline4'";

然后,在if中,检查别名" number"