通过mysqli读取数据

<?php

	$mysqli = new mysqli("localhost", "user", "******", "dbtest");

	if (mysqli_connect_errno()) 
	{
		printf("Connect failed: %s\n", mysqli_connect_error());
		exit();
	}else
	{
		printf("Connect succeed");
	}

	printf("<br>");
	
	$query = "select * from tabletest";
	$result= $mysqli->query($query);  
	if($result)
	{
		if($result->num_rows>0)
		{                          
			while($row =$result->fetch_array() )
			{             
				echo ($row[0])."<br>";  
				echo ($row[1])."<br>";  
				echo "<hr>";  
			}  
		}
	}else
	{
		echo "查询失败";
	}

	$result->free(); 
	$mysqli->close();
?>

展开阅读全文

使用mysqli从数据库中获取所有结果

03-20
<div class="post-text" itemprop="text"> <p>please check out my code below. With that class I am able to display results like so:</p> <pre><code>$connectTest = new testResults(); $test = $connectTest->grabResults(test, id, id); echo $test['id']; echo $test['name']; echo $test['address']; </code></pre> <p>In my database I have several fields in the "test" table. I go to my page using index.php?id=1. With this I am displaying just the results from one row because it grabs all results WHERE id = 1.</p> <p>What I need is the class below to display multiple results. It just displays one row. But if I have multiple rows with id = 1 I would like to display these results, but I cannot get it to work. I have tried a lot of things but I always end up with just one result.</p> <p>class:</p> <pre><code>class testResults { public function grabResults($table, $field, $id) { $result = $this->db->mysqli->query("SELECT * FROM $table WHERE $field = $id"); $resultData[] = array(); if(!$result) { return false; } while($row = $result->fetch_assoc()) { $rows[] = $row; } foreach ($rows as $resultData) { return $resultData; } } } </code></pre> <p>Edit:</p> <pre><code>Array ( [id] => 25 [name] => test [status] => 1 ) Array ( [id] => 25 [name] => test [status] => 3 ) Array ( [id] => 25 [name] => test [status] => 5 ) Array ( [id] => 25 [name] => test [status] => 4 ) Array ( [id] => 26 [name] => test [status] => 1 ) Array ( [id] => 26 [name] => test [status] => 3 ) Array ( [id] => 27 [name] => test [status] => 1 ) Array ( [id] => 27 [name] => test [status] => 3 ) Array ( [id] => 27 [name] => test [status] => 5 ) Array ( [id] => 27 [name] => test [status] => 4 ) Array ( [id] => 27 [name] => test [status] => 2 ) Array ( [id] => 27 [name] => test [status] => 4 ) Array ( [id] => 27 [name] => test [status] => 1 ) </code></pre> <p>I am getting results as above, any way to easily display these results in an echo? For each id there are different results, so results will vary with each query. So I would like to display results in a table for example like so:</p> <pre><code>echo '<table> <tr> <td>$id</td> <td>$name</td> <td>$status</td> </tr> </table>'; </code></pre> <p>So all results will be displayed like in a while loop.</p> </div>
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值