[英]What is wrong with my table? It will not display DB
對於PHP,我是新手。 我在從站點中的mysql中顯示表時遇到了問題。 我發現此代碼似乎無效。 有人可以告訴我代碼有什么問題嗎? 或者建議將mysql輸出到html表的更好方法。
<?php $username="user";$password="password";$database="database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename";$result=mysql_query($query);
$num=mysql_numrows($result);mysql_close();?>
<div class="panel-body">
<table class="table table-bordered table-striped mb-none" id="datatable-tabletools" data-swf-path="assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf">
<thead>
<tr>
<th>id</th>
<th>Value1</th>
<th>Value2</th>
<th>Value3</th>
<th>Value4</th>
<th>Value5</th>
<th>Value6</th>
<th>Value7</th>
<th>Value8</th>
</tr>
</thead>
<?php$i=0;while ($i < $num)
$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"v1");
$f3=mysql_result($result,$i,"v2");
$f4=mysql_result($result,$i,"v3");
$f5=mysql_result($result,$i,"v4");
$f6=mysql_result($result,$i,"v5");
$f7=mysql_result($result,$i,"v6");
$f8=mysql_result($result,$i,"v7");
$f9=mysql_result($result,$i,"v8"); ?>
<tbody>
<tr class="gradeX">
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
<td><?php echo $f5; ?></td>
<td><?php echo $f6; ?></td>
<td><?php echo $f7; ?></td>
<td><?php echo $f8; ?></td>
<td><?php echo $f9; ?></td>
</tr>
</tbody>
</table>
<?php$i++;}?>
</div>
你做這個:
$num=mysql_numrows($result);mysql_close();?>
^^^^^^^^^^^^^
在執行此操作之前:
<?php$i=0;while ($i < $num)
$f1=mysql_result($result,$i,"id");
^^^^^^^^^^^^^^^^^
無法從查詢結果中獲取任何內容,因為您終止了該結果將來自的連接。
並且請注意,mysql _ *()函數已過時且已過時。 你不應該在任何新的代碼中使用它們。
如果您可以重新開始使用,以下是連接到數據庫的代碼:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "";
try
{
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
echo "Connected successfully" . "<br /> \n";
}
catch (PDOException $e)
{
echo "Can't connect to the server !: " . $e->getMessage() . "<br />";
die();
}
try
{
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "";
$stmt = $db->query($sql);
$array_res = $stmt->fetchAll();
}
catch (Exception $e)
{
echo "Failed: " . $e->getMessage();
}
?>
您只需要在頂部&$ sql編輯您的個人信息。 然后可以免費顯示它,所有內容在下面的鏈接中說明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.