简体   繁体   English

MySQL数据库PHP特定行

[英]MySQL Database PHP Specific Rows

My current code is this: 我当前的代码是这样的:

   <?php
$con=mysqli_connect("stevie.heliohost.org","rbxdataa_Art","mydata123art");
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);
?>

As I am currently very new to PHP and MySQL, I have no idea why this code will not work. 由于我目前对PHP和MySQL还是很陌生,所以我不知道为什么这段代码不起作用。 I am also confused as to how I would make it echo only the top ($Amount) as determined by how large the "EventId" value is. 我也困惑于如何使它仅回显由“ EventId”值的大小确定的顶部($ Amount)。 My intent is to gather the [$Amount] highest rows in the table with the EventType $GetType. 我的目的是使用EventType $ GetType收集表中的[$ Amount]最高行。 I am aware of SQL Injection vulnerability, however for my purposes this does not affect me. 我知道SQL注入漏洞,但是出于我的目的,这并不影响我。 Error: "Parse error: syntax error, unexpected T_STRING on line 4" 错误:“解析错误:语法错误,第4行出现意外的T_STRING”

As I see, you have a normal code, may be you have no any rows in DataBase with requested EventType ? 如我所见,您具有正常的代码,可能是您的数据库中没有具有请求的EventType行?

At second: you have SQL Injection vulnerability. 第二点:您有SQL Injection漏洞。 Use PDO and Prepared Statements instead your mysqli_query code. 使用PDO和Prepared Statements代替mysqli_query代码。

If you want to get the row with the max eventid, you need do this: 如果要获取具有最大eventid的行,则需要执行以下操作:

$sql = "SELECT MAX(EventId)  FROM EventRecord WHERE EventType='$GetType';

is not necessary order by eventid when u can get the max using MAX(An AGGREGATION FUNCTION) 当您可以使用MAX(AGGREGATION FUNCTION)获得最大值时,按eventid排序不是必需的

PD: you can add more attributes in select statement. PD:您可以在select语句中添加更多属性。

如果您需要使用EventType的数量最多,请尝试此操作。

$sql = "SELECT MAX(amount_field) FROM EventRecord WHERE EventType='$GetType';

If you want record sorted based on amount from highest to lowest then order by amount desc. 如果要基于从最高到最低的数量对记录进行排序,则按数量desc排序。 (if you have amount column in table) (如果表中有金额列)

<?php
$con=mysqli_connect("Removed for Privacy");
mysqli_select_db($con, "Removed") or die(mysqli_error());

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM