从MySQL数据库中获取特定信息

时间:2015-03-15 19:26:54

标签: php mysql

我有一个名为“user”的数据库;我想创建一个PHP脚本,它将像这样执行:http://forum.com/phpscript.php?username=MyName

它应该在表格中搜索username,然后返回userid。 这是我在一些教程之后尝试过的:

<?php

$servername     = "localhost";
$username       = "username";
$password       = "password";
$dbname         = "database";

$uname      = $_GET["user"];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT userid FROM user WHERE username = '$uname'"
$result = $conn->query( $sql );

if( $result->num_rows > 0 )
{
    $userid = $conn->result( $result, 0 );

    echo "$userid";
}
$conn->close();
?>

我已经通过在服务器上上传并从浏览器调用它来执行此操作:http://forum.com/phpscript.php?username=MyName

1 个答案:

答案 0 :(得分:1)

$ result附近有语法错误,$ sql后缺少(;);试试这个

$sql = "SELECT userid FROM user WHERE username = '$uname'";

,或者

$sql = "SELECT userid FROM user WHERE username = '".$uname."'";