MySQL查找第一个Colum的字符串,Echo第二列

时间:2013-08-25 21:39:14

标签: php mysql multiple-columns

首先,抱歉英文不好:( 我想要关键系统,如蒸汽,原产地或加油。我向用户提供密钥,用户将该代码写入输入,代码将回显用户的代码
我有1张桌子。其中有2列。 2列2行。像这样:

.----------------------.
|  sifre  |  bizimkey  |
|______________________|
|  A5Sr2A | First Code |
|______________________|
|  FaQ1fS | Scnd. Code |
|______________________|

如果用户将A5Sr2A输入到Input,PHP Echos" First Code"。如果用户输入了错误的代码,请显示警告。

我尝试了很多代码。我可以像这样运行代码:

<?php
 if ($_POST['pass'] == "A5Sr2A") {
     {
         echo "First Code";
     }
 } else {
     header('Location:index.html');
 }
?>

但我不想要这个。这是一件极具挑战性的事情我问自己,&#34;为什么不使用MySQL?&#34;
我正在尝试7个小时。真。我想这样做。我想学习MySQL。请帮忙。谢谢!

    <? ob_start(); ?>
    <html>
        <link rel=stylesheet href="style.css">
        <table>
            <tr>
                <td>
                    <center>
<?php 
$host = "localhost";
$user = "user";
$password = "pass";
$database = "db";
$con = mysqli_connect($host, $user, $password, $database);
if (mysqli_connect_errno()) {
    echo "ERROR";
}
$result = m ysqli_query($con, "SELECT sifre FROM keyler");
if ($_POST['pass'] == $ result) {
    {
        $mykey = m ysqli_query($con, "SELECT bizimkey FROM keyler");
        echo $mykey;
    }
} else {
    header('Location:index.html');
}
mysqli_close($con);
?>
                    </center>
                </td>
            </tr>
        </table>

    </html>
    <? ob_flush(); ?>

1 个答案:

答案 0 :(得分:0)

好的,请参阅下面的基本实现示例:

$host = "localhost";
$user = "user";
$password = "pass";
$database = "db";
$con=mysqli_connect($host,$user,$password,$database);
if(mysqli_connect_errno()!=0)//mysqli_connect_errno() returns 0 when there are no errors
{
    echo "ERROR";
}
$sifre_test = mysqli_real_escape_string($con,$_POST['pass']);
$result = mysqli_query($con,"SELECT '1' FROM `keyler` WHERE `sifre`='".$sifre_test."'");
if($result!==false)
{
    //If $result !== false, the query was successful, so we'll try to grab a row
    $row = mysqli_fetch_assoc($result);
    //$row will be null if there wasn't a row found where `sifre`=$sifre_test
    if(!is_null($row))
    {
        $result = mysqli_query($con,"SELECT `bizimkey` FROM `keyler` WHERE `sifre`='".$sifre_test."'");
        if($result!==false)
        {
            $row = mysqli_fetch_assoc($result);
            $bizimkey = $row['bizimkey'];
            echo $bizimkey;
        }
        else
        {
            die("bizimkey query failed");
        }
    }
}
else
{
    die("sifre query failed");
}

为了更好地理解和更多示例,请参阅以下文档链接:

mysqli_query mysqli_fetch_assoc