我需要帮助解决连接到我的数据库并获得我想要的东西的问题。我以前用 php 编程过,从来没有遇到过这个问题。我收到此错误消息。
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'character WHERE username = David' at line 1
这是我的代码。
<?php
$con = mysqli_connect("localhost","root","","thespianassistant");
$username = $_GET['username'];
$query = "SELECT * FROM `character` WHERE `username` = $username";
$data = mysqli_query($con, $query) or die(mysqli_error($con));
?>
最佳答案
这是您使用 PDO 的代码端口,也许您会感兴趣,不要使用旧的 mysql_* 函数。
<?php
//connect to database using PDO
try{
$db = new PDO('mysql:host=localhost;dbname=thespianassistant', 'root', 'you_should_set_a_password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
//Query the db & fetch result
$sql = "SELECT *
FROM `character`
WHERE username = :username";
$query = $db->prepare($sql);
$query->execute(array(':username' => $_GET['username']));
$result = $query->fetchAll(PDO::FETCH_ASSOC);
}catch(PDOException $e){
die($e->getMessage());
}
//do something with result
if(!empty($result)){
echo '<pre>'.print_r($result, true).'</pre>';
}else{
//no character found
}
关于php - 我的页面出现mysql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23256597/