【问题标题】:Retrieving and Formatting Data Properly From MYSQL从 MYSQL 中正确检索和格式化数据
【发布时间】:2018-10-03 14:58:19
【问题描述】:

好的,所以我正在尝试创建 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%,并且在单击扩展图标时可以出现/消失的下部。

【问题讨论】:

  • “很多错误”...比如什么?在哪里?我们必须猜测吗?
  • ID 必须是唯一的 (&lt;div id='moreinfo'&gt;)
  • @ADyson 对不起。让我编辑它
  • "mysqli_query: 失败时返回FALSE。对于成功的SELECTSHOWDESCRIBEEXPLAIN查询@987654331 @ 将返回一个mysqli_result 对象。对于其他成功查询mysqli_query() 将返回TRUE。" (Source)
  • 我不懂foreach。您已经遍历了每一行。

标签: javascript php html mysqli


【解决方案1】:

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

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

<?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/样式问题。首先获取数据库查询,然后提出一个新的、具体的问题,其中包含更多详细信息,包括屏幕截图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多