从MYSQL正确检索和格式化数据

时间:2018-10-03 14:58:19

标签: javascript php html mysqli

好的,所以我试图创建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%的屏幕,而较低的部分则在单击扩展时可以显示/消失图标。

1 个答案:

答案 0 :(得分:0)

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

我删除了内部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 /样式问题。首先进行数据库查询,然后再提出一个新的,具体的问题,包括屏幕截图在内的更多详细信息。