MySQL数据库访问

时间:2013-12-18 07:43:55

标签: php mysql

注意:问题已结束。我设法弄清楚出了什么问题。 我目前的代码是:

<?php
    $con=mysqli_connect("HOST","USERNAME","PASSWORD");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }


    mysqli_select_db($con, "rbxdataa_Data");
    $Amount=$_GET["Amount"];
    $GetType=$_GET["Type"];
    $sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
    $sql_run = mysqli_query($con, $sql);
    while($sql_row = mysqli_fetch_assoc($sql_run)){
        echo $sql_row['EventId'].'<br>';
    }
    mysqli_close($con);
?>

由于我目前对PHP和MySQL不熟悉,我不知道为什么这段代码不起作用。我也很困惑,我将如何使它仅回显顶部($Amount),这取决于“EventId”值的大小。

我的目的是使用EventType [$Amount]收集表中的$GetType最高行。 我知道SQL注入漏洞,但是对于我的目的,这不会影响我。

Error:
"Parse error: syntax error, unexpected T_STRING on line 4"
This isn't the main error, however. I just can't read the main error yet.

3 个答案:

答案 0 :(得分:1)

在第4行中,mysqli_connect_error()不存在。请查看mysqli文档http://www.php.net/manual/en/book.mysqli.php

修改:您可以尝试:printf("Connect failed: %s\n", mysqli_error($con));

答案 1 :(得分:1)

试试这个。

$con=mysqli_connect("HOST","USERNAME","PASSWORD","rbxdataa_Data");
$sql_query = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_result = mysqli_query($con,$sql_query)
    or exit("Query fail message");

否则看起来像我习惯的那样。

我刚才意识到,甚至没有人问过你正在使用的$ _GET变量。您从$ _GET ['Type']设置$ GetType。可能就是这样。

答案 2 :(得分:1)

请使用此代码:

<?php
    $con=mysqli_connect("HOST","USERNAME","PASSWORD");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }


    mysqli_select_db($con, "rbxdataa_Data");
    $Amount=$_GET["Amount"];
    $GetType=$_GET["Type"];
    $sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
    $sql_run = mysqli_query($con, $sql);
    while($sql_row = mysqli_fetch_assoc($sql_run)){
        echo $sql_row['EventId'].'<br>';
    }
    mysqli_close($con);
?>