首页 > 解决方案 > 从 MYSQL 中正确检索和格式化数据

问题描述

好的,所以我正在尝试创建 Billboard Hot 100 的副本。我已经有一个数据库和具有我想要的正确格式的页面。但是当我试图从数据库中检索数据时,我使用的格式变得一团糟,并且出现了很多错误。

这是我的代码:

<div class="chartsList">
	<ul class="charts" style="list-style: none;">
		


		<?php
		$songQuery = mysqli_query($con, "SELECT * FROM songs ORDER BY sales DESC LIMIT 100");
		while($row = mysqli_fetch_array($songQuery)) {
			

		$i = 2;
		foreach($songQuery as $songId) 

			echo "<li class='chartsRow'>
						<div style='width:10%;height:100%;float:left; text-align: center;color:black;font-size: 40px;line-height: 150px;font-weight: 600;'>$i</div>
					<img style='height: 30%; float: right; position: relative; top: 50; right: 89%;'src='" . $row['movement'] . "'>

					<img type='button' style='float: right;width: 25px;position: relative;top: 60;'onclick='myFunction()'src='assets/icons/expand.png'>
					<div style='width:90%; height:100%; display: block;''>

				<div style='width:15%;height:100%;float:left'>
					<img style='height: 90%;margin-top: 8;'src='". $row['coverArt'] . "'>
				</div>
				<div style='width:85%; height: 100%;margin-left: 26%;''>
					<a style='font-size: 30px; font-weight: 600; position: relative; top: 30;'>" . $row['title'] . "</a>
					<p style='position: relative;top: 10;''>" . $row['artist'] . "</p>
				</div>
			</div>

			<div id='moreinfo'>
						<div class='lefty'>	
						<a>" . $row['lastWeek'] . "</a>
						<p>LAST WEEK</p>
				</div>
				<div class='lefty' style='border-left-style: solid;border-left-width: 1px;border-right-style: solid;border-right-width: 1px;border-right-color: #b7b7b7; border-left-color: #b7b7b7;'>
					<a>" . $row['peak'] . "</a>
					<p>PEAK POSITION</p>
				</div>
				<div class='lefty'>
					<a>" . $row['woc'] . "</a>
					<p>WEEKS ON CHART</p>
				</div>
				</div>
	

				</li>";

			$i = $i + 2;
		}

		?>

	</ul>
			
</div>

目前的问题:

“警告:mysqli_fetch_array() 期望参数 1 为 mysqli_result,布尔值在 C:\xampp\htdocs\billboard\hot-100.php 第 52 行给出”

我想要实现的是看起来像这样的东西:https ://www.billboard.com/charts/hot-100但 2-100 的行占据了屏幕的 60%,而较低的部分可以单击扩展图标时出现/消失。

标签: javascriptphphtmlmysqli

解决方案


不确定所有错误是什么,但这里的主要问题是您需要在尝试使用查询之前打开错误报告并检查连接错误,然后在运行查询之后但在访问结果集之前检查错误。

我删除了内部foreach循环,因为它是多余的。您可以通过简单地增加一个变量来创建一个计数器。请参阅代码中的注释。

<?php
// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Turn MySQL errors into PHP exceptions
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// I assume you have a procedural-style connection setup here.
$con = mysqli_connect("localhost", "xxx", "xxx", "xxx");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
?>

<div class="chartsList">
    <ul class="charts" style="list-style: none;">

        <?php
        $counter = 0;
        $songQuery = mysqli_query($con, "SELECT * FROM songs ORDER BY sales DESC LIMIT 100");

        // Now, check if your query has an error
        if ($songQuery === false){
            die('Error in query: ' . mysqli_error($con));
        }

        while ($row = mysqli_fetch_array($songQuery)) {
            // Your HTML appears to be malformed. I'm getting all sorts of errors in my IDE.
            // Removing the HTML here but you should be able to merge that back in without issues.

            // If you need a counter to display row numbers, you can just create one.
            // Notice I created a $counter variable above.
            // increment it by:
            //    $counter++; to increment by 1 OR
            //    $counter = $counter + 2; to increment by 2

            // Now, use $row['column_name'] where you need it.
        } ?>

    </ul>
</div>

编辑

听起来你也有 CSS/样式问题。首先获取数据库查询,然后提出一个新的、具体的问题,其中包含更多详细信息,包括屏幕截图。


推荐阅读