我们已经设置了一个练习,在该练习中,我们在数据库上获得了1000个名称,并将其集成到phpMyadmin中。我们的练习是在获得一个特定用户名(一系列数字)后找到一个人的身份证号码。
任何帮助都会很棒。
答案 0 :(得分:0)
这很简单,因为你是php和Stack Overflow的新手,我知道你还没有读过如何发帖的问题,请做一些简单的研究,如果你找不到答案就把它发布在SO上,无论如何,在网上有几个教程。你走了。
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, username, name FROM users WHERE username='username'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row['id']. " - Nme: " . $row['name']. " " . $row['username']. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
在http://www.w3schools.com/php/default.asp学习php的简单方法,它也有SQL部分。
答案 1 :(得分:0)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare('SELECT id, username FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$username = 'username'; //change this to the username you want to lookup
$stmt->execute();
$result = $stmt->fetchAll();
# If one or more rows were returned...
if ( count($result) ) {
foreach($result as $row) {
print_r($row);
}
} else {
echo "No rows returned.";
}
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
这是使用PDO时的一个示例,它通过使用预准备语句更好地防止SQL注入。在线有很多关于如何做到这一点的tutuorials。