所以我有一个包含大约5列数据的表的数据库。我需要在包含分页的网页上获取此数据(页面将包含5个项目)。我想我找出了分页部分,但我不知道如何从数据库中获取所有数据并创建每个项目。你可以看到我的模型here。
我怎么可能这样做?
答案 0 :(得分:1)
假设您在用户表中有5列作为id,uname,fname,lname,age。你可以用这个:
$i = 0; // Start from row $i
$j = 10; // Number of rows you want
$connection = mysqli_connect("your_server","User_name","Password","db");
$stmt = $connection->prepare('
SELECT
id, uname, fname, lname, age
FROM
user
LIMIT
?, ?
');
$stmt->bind_param('ii', $i, $j);
$stmt->execute();
$stmt->bind_result($id, $uname, $fname, $lname, $age);
if ($stmt->fetch()) {
$result = array();
do {
array_push($result, array(
'id' => $id,
'uname' => $uname,
'fname' => $fname,
'lname' => $lname,
'age' => $age
));
while ($stmt->fetch());
} else {
// no rows fetched
}
之后使用数据你可以这样做:
foreach ($result as $r) {
echo $r['id'].$r['uname'].$r['fname'].$r['lname'].$r['age'];
}
答案 1 :(得分:0)
在mysql中你需要类似的东西:
SELECT
*
FROM
yourtable
WHERE
somecol = somevalue
LIMIT
start, results
其中start是起始编号,结果是每页的结果数。
答案 2 :(得分:0)
在底部你使用了1,2,3 .... n。使用此号码作为按钮表格将提交:
<form action="form.php" method="post">
<button name="subject" type="submit" value="1">1</button>
<button name="subject" type="submit" value="6">2</button>
</form>
//At form.php add the below code
$status=$_POST['subject'];
$i=1;
////////////////////////Database connection start
$con = mysql_connect('MySQL host name ', 'username', 'password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('Database_name', $con);
$sql="set names 'utf8'";// for unicode support, must use after mysql_select_db() function.
mysql_query($sql);
mysql_query('SET CHARACTER SET utf8')or die('charset problem:'.mysql_error());
mysql_query("SET SESSION collation_connection ='utf8_general_ci'")
or die('collation problem:'.mysql_error());
////////////////////////Database connection ends
$result = mysql_query("SELECT * FROM table WHERE status='$status'");
while($row = mysql_fetch_array($result))
{
echo $row['email'];
if(i==6) break;
}