PHP查询失败,显示错误?

Lia*_*iam 2 php mysql

我的页面上有一个查询,它使用GET变量从我的表中提取数据...

如果我回显我的GET var,数据就在那里,所以即时通讯我的查询有问题,而不是或死我可以在浏览器中显示错误吗?

// Get USER ID of person
$userID = $_GET['userID'];

// Get persons
$sql = 'SELECT * FROM persons WHERE id = $userID';
$q   = $conn->query($sql) or die('failed!');
Run Code Online (Sandbox Code Playgroud)

max*_*hud 6

$sql = "SELECT * FROM persons WHERE id = $userID";
Run Code Online (Sandbox Code Playgroud)

必须使用双引号才能在查询字符串中使用变量.

你也可以这样做:

$sql = "SELECT * FROM persons WHERE id = ".$userID;
Run Code Online (Sandbox Code Playgroud)

你应该做的是(为了保护自己免受sql注入):

$safeuid = $conn->prepare($userID);
$sql = "SELECT * FROM persons WHERE id = ".$safeuid;
Run Code Online (Sandbox Code Playgroud)

您可以随时在php页面的顶部使用它进行调试:

ini_set('display_errors',1); 
error_reporting(E_ALL);
Run Code Online (Sandbox Code Playgroud)