我有一個登錄頁面(沒有任何保護,即時將去設置它們)但每次我運行任何查詢時,即使是一個簡單的SELECT * FROM 'player'
,$risultato
總是錯誤的。我已經檢查過所有連接信息是否正確(它們在註冊頁面中工作)。有人知道爲什麼嗎?查詢總是返回false - PHP MYSQL
的login.php
<?php
$connessione = mysqli_connect("localhost", "my_id", "my_pass", "my_db");
$user_name = $_GET['user_name'];
$user_pass = $_GET['user_pass'];
$query = "SELECT username FROM 'player';";
$risultato = mysqli_query($connessione, $query); <---- !!always false!!
$count = mysqli_num_rows($risultato); <---- it say that he receved a boolean (false)
if ($count != 1) {
echo "Nessun risultato.";
}else{
$row = mysqli_fetch_assoc($risultato);
$name = $row['username'];
}
編輯:
我的工作register.php:
<?php
$connessione = mysqli_connect("localhost", "my_id", "my_pass", "my_db");
$user_name = $_POST['user_name'];
$user_pass = $_POST['user_pass'];
$query = "INSERT INTO `player` (`username`, `password`, `money`) VALUES ('".$user_name."', '".$user_pass."', '100');";
$risultato = mysqli_query($connessione, $query);
if($risultato){
http_response_code(200);
}else{
http_response_code(400);
}
編輯#2:
這個心不是工作壓力太大:
<?php
$connessione = mysqli_connect("localhost", "id2967178_fyruz", "Ferru9800", "id2967178_collectcards");
echo mysqli_error($connessione);
$user_name = $_GET['user_name'];
$user_pass = $_GET['user_pass'];
$query = "SELECT username FROM `player`";
$risultato = mysqli_query($connessione, $query);
$count = mysqli_num_rows($risultato);
if ($count != 1) {
echo "Nessun risultatooooo.";
}else{
$row = mysqli_fetch_assoc($risultato);
$name = $row['username'];
}
它是錯誤的,因爲您的查詢失敗或連接不起作用 – clearshot66
注意:mysqli的面向對象的接口明顯較少冗長,使得代碼更易於閱讀和審計,並且不易與過時混淆'mysql_query'界面。在你過於投入程序風格之前,它是值得轉換的。例如:'$ db = new mysqli(...)'和'$ db-> prepare(「...」)過程接口是PHP4時代的一個神器,當引入mysqli API時,不應該在新的代碼 – tadman
很多問題都可以通過[在mysqli中啓用例外]來檢測和解決(https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli),所以錯誤不要輕易忽略 – tadman