简体   繁体   English

获取 SQL 表值的最有效方法是什么?

[英]What is the most efficient way to get SQL table values?

I wrote some code a while back to retrieve a player's saved score when they logged into the website.不久前我写了一些代码来检索玩家登录网站时保存的分数。 It used AJAX, and looked a little something like this:它使用了 AJAX,看起来有点像这样:

Javascript: Javascript:

function getHighScore(user){
    $.get("getScore.php?userName="+user,function(data){
        console.log(data);
                output = data;
    });
}

PHP: PHP:

<?php
$username = strval($_GET['userName']);

$con = mysqli_connect('localhost','XXXX','XXXX','XXXX');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
} else {
$sql="SELECT * FROM users WHERE username = '".$username."'";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
    $wealth = $row['wealth'];

    echo $wealth;

    }
}

mysqli_close($con);
//return $wealth;
?>

I was faced with the fact today that I would have to grab a whole bunch of data from a database, so I had a look at the old high score code I wrote.我今天面临这样一个事实,即我必须从数据库中获取大量数据,因此我查看了我编写的旧高分代码。 I'll attach a screenshot of what the table looks like that I will be retrieving info from.我将附上一张我将从中检索信息的表格外观的屏幕截图。 Anyway, I need to grab info from 6 columns and 10 rows.无论如何,我需要从 6 列和 10 行中获取信息。 I need to assign a PHP variable to the data from P1, P2, P3 etc on MON1;我需要为来自 MON1 上的 P1、P2、P3 等的数据分配一个 PHP 变量; P1, P2, P3 etc on TUE1; TUE1 上的 P1、P2、P3 等; so on and so forth until we reach P6 on FRI2.依此类推,直到我们在 FRI2 到达 P6。 I'm really quite new to PHP, so what would be the best way to go around doing this?我对 PHP 真的很陌生,那么最好的方法是什么?

I apologise if I worded the question strangely.如果我的问题措辞奇怪,我深表歉意。 Feel free to ask if you didn't understand something.如果您有什么不明白的地方,请随时提问。

Thank you!谢谢!

PS: Ignore the P#_WORK columns. PS:忽略 P#_WORK 列。 I don't need to refer to them right now我现在不需要参考它们

https://ibb.co/gq8u5a https://ibb.co/gq8u5a

I'd suggest you use an object to store everything, using the "day" column as key, and as value the P* columns in another.我建议您使用一个对象来存储所有内容,使用“day”列作为键,并将 P* 列作为另一个值。 Here's some code you can use right away:下面是一些您可以立即使用的代码:

<?php
$con = mysqli_connect('localhost','XXXX','XXXX','XXXX');
if(!$con) exit('Could not connect: ' . mysqli_error($con));

$username = $_GET['userName'];

$sql = "SELECT * FROM TABLE WHERE username = '%s'";
$results = $con->query(sprintf($sql, $con->escape_string($username))); // Always escape parameters to prevent SQL injection attacks

$data = new stdClass; // An empty object

while($result = $results->fetch_object()){
    $day = $result->day; // Use the day as the key of the object
    $data->{$day} = $result;
}

// Now we output the results below by accesing the data in a chain-like fashion by accesing the "data" object
echo $data->MON1->P1 . '<br>'; // will output "Geo E"
echo $data->FRI1->P4 . '<br>'; // will output "Maths"
echo $data->THU2->P6 . '<br>'; // will output "DT"

Be sure to replace "TABLE" in the SQL query with the actual table name, as that wasn't visible in the screenshot you attached.请务必将 SQL 查询中的“TABLE”替换为实际的表名,因为这在您附加的屏幕截图中不可见。

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

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