Php 使用codeigniter获取数据并将其显示在表中
有人能帮我从数据库中提取数据并在表中查看吗?我是codeigniter框架的新手。下面是一段从SQL表获取数据的简短代码 首先创建一个连接:Php 使用codeigniter获取数据并将其显示在表中,php,codeigniter,Php,Codeigniter,有人能帮我从数据库中提取数据并在表中查看吗?我是codeigniter框架的新手。下面是一段从SQL表获取数据的简短代码 首先创建一个连接: // Create connection $db = mysqli_connect($host,$username,$password,$database) or die('Error connecting to MySQL server.'); The Values: $host = your host-ip or URL or localhost $
// Create connection
$db = mysqli_connect($host,$username,$password,$database)
or die('Error connecting to MySQL server.');
The Values:
$host = your host-ip or URL or localhost
$username = Your Database Username
$password = your Database Password
$database = your database's Name
然后,您需要从该数据库的表中请求(查询)某些内容
例如:
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
}
while($row = mysqli_fetch_assoc($res))
{
$username = $row['username'];
$date= $row['date'];
}
echo"<table border = '1'>";
echo"<tr>";
//Do <td> and </td> for as many Columns as you have and write them between the td's
echo"<td>";
echo"Column Name";
echo"</td>";
//Then comes the Data part
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
//open new Row
echo"<tr>";
//Do this td, data, /td loop for as many Columns you have in your
Database. For a Database with id, username and Password for example:
echo"<td>";
echo"$row['id']";
echo"</td>";
echo"<td>";
echo"$row['username']";
echo"</td>";
echo"<td>";
echo"$row['password']";
echo"</td>";
echo"</tr>";
//Close row
}
现在,这段代码将从表中获取所有数据,并按ID降序排列。
在这段代码中,$table代表您的表名,$db代表您的数据库名,因为PHP7在每个查询中都需要它
结果(例如,表中按ID排序的所有数据)现在存储在单个变量$res中。如果需要,可以使用代码检查“If($res=true)”,以确保实际从查询中获得结果并捕获异常
“方法mysqli\u fetch\u assoc()”现在将为您提供方便的所有数据。
您所要做的就是像这样使用While循环:
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
}
while($row = mysqli_fetch_assoc($res))
{
$username = $row['username'];
$date= $row['date'];
}
echo"<table border = '1'>";
echo"<tr>";
//Do <td> and </td> for as many Columns as you have and write them between the td's
echo"<td>";
echo"Column Name";
echo"</td>";
//Then comes the Data part
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
//open new Row
echo"<tr>";
//Do this td, data, /td loop for as many Columns you have in your
Database. For a Database with id, username and Password for example:
echo"<td>";
echo"$row['id']";
echo"</td>";
echo"<td>";
echo"$row['username']";
echo"</td>";
echo"<td>";
echo"$row['password']";
echo"</td>";
echo"</tr>";
//Close row
}
这意味着,每隔一个While循环都会按照您选择的查询顺序遍历一行结果。
$row充当一个数组,其中Idice([]括号中的文本)对应于表中给定的Clunn名称
希望我能帮助你:)
请在以后的提问中更清楚、更详细地陈述你的问题,并在提问之前表明你已经解决了你的问题
间谍
编辑
我读得太多了:)
要在表中查看它,您应该执行以下操作:
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
}
while($row = mysqli_fetch_assoc($res))
{
$username = $row['username'];
$date= $row['date'];
}
echo"<table border = '1'>";
echo"<tr>";
//Do <td> and </td> for as many Columns as you have and write them between the td's
echo"<td>";
echo"Column Name";
echo"</td>";
//Then comes the Data part
$sql = "SELECT * FROM $table ORDER BY id DESC";
$res = mysqli_query($db, $sql) or die("Fehler:$sql");
while($row = mysqli_fetch_assoc($res))
{
//open new Row
echo"<tr>";
//Do this td, data, /td loop for as many Columns you have in your
Database. For a Database with id, username and Password for example:
echo"<td>";
echo"$row['id']";
echo"</td>";
echo"<td>";
echo"$row['username']";
echo"</td>";
echo"<td>";
echo"$row['password']";
echo"</td>";
echo"</tr>";
//Close row
}
echo”“;
回声“;
//尽可能多地填写和,并在td之间填写
回声“;
呼应“列名”;
回声“;
//然后是数据部分
$sql=“从$table ORDER BY id DESC中选择*”;
$res=mysqli_query($db,$sql)或die(“Fehler:$sql”);
while($row=mysqli\u fetch\u assoc($res))
{
//开新行
回声“;
//对数据库中尽可能多的列执行td、data、/td循环
数据库。对于具有id、用户名和密码的数据库,例如:
回声“;
回显“$row['id']”;
回声“;
回声“;
回显“$row['username']”;
回声“;
回声“;
回显“$row['password']”;
回声“;
回声“;
//近排
}
您可以从函数mysql\u query中执行此操作。这是文档,请给一些时间进行研究。