您需要定义用户名变量,还需要连接到数据库。如果你打算使用 mysqli,那么最好使用面向对象的风格。Mysqli 可能不是理想的解决方案,请在此处查看此 Cletus 答案:MySQL vs MySQLi when using PHP
您需要查看 php.net 上的 php 指南以学习该语言,并确保查看评论。你也可以谷歌教程。如果一切都失败了,那么 stackoverflow 将是寻求帮助的地方。
<?php
$username = trim($_POST['username']); //where username is a form field sent using post method, we trim it to remove white spaces so if user just enters spaces, the script will see it as empty string.
//Connect to database http://www.php.net/manual/en/mysqli.query.php
$conn = mysqli_connect("localhost", "my_user", "my_password", "yourdatabasename");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($username){
$username=mysqli_real_escape_string($conn, $username); //escape the string AFTER you connect http://www.php.net/manual/en/mysqli.real-escape-string.php
$get_user_content = mysqli_query($conn, "SELECT * FROM content WHERE UserName = '$username' LIMIT 1") or die(mysqli_error($conn)); //assuming you have table called content with a field called UserName to store the username, add limit 1 since you only need one anyway. , where does databaseaccess_error comes from? it's undefined. use mysqli_error
if(mysqli_num_rows($get_user_content) == 1 )
{
$row = mysqli_fetch_array($get_user_content);
$title = $row['utitle'];
$content = $row['ucontent'];
echo $title;
echo $content;
}else echo "User couldn't be found";
}