Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP的SQL数据库的数组_Php_Sql - Fatal编程技术网

使用PHP的SQL数据库的数组

使用PHP的SQL数据库的数组,php,sql,Php,Sql,有人能帮忙吗?我是PHP新手,正在努力使这段代码正常工作。例如,我有一个sql数据库表,其中包含以下架构和数据: Type....rent_price a..........100 b..........200 c..........300 我希望能够在一节中重复说“a”,在另一节中重复说“200”。下面的代码将显示“a”,但我似乎无法使用第二个数组让它显示租金价格列中的任何内容 $result = $mysqli->query("SELECT * FROM dbc_post

有人能帮忙吗?我是PHP新手,正在努力使这段代码正常工作。例如,我有一个sql数据库表,其中包含以下架构和数据:

Type....rent_price  
a..........100  
b..........200  
c..........300
我希望能够在一节中重复说“a”,在另一节中重复说“200”。下面的代码将显示“a”,但我似乎无法使用第二个数组让它显示租金价格列中的任何内容

$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");

for ($set = array (); $row = $result->fetch_assoc(); $set[] = $row['type']);
for ($set1 = array (); $row = $result->fetch_assoc(); $set1[] =$row['rent_price']);
?>

<?php echo $set[0];?>
<?php echo $set1[1];?>
$result=$mysqli->query(“从dbc中选择*按ID ASC限制3发布订单”);
对于($set=array();$row=$result->fetch_assoc();$set[]=$row['type']);
对于($set1=array();$row=$result->fetch_assoc();$set1[]=$row['rent\u price']);
?>

您的数据位于数组的第一个元素中
$set1[0]

但你最好还是在整个过程中保持命名

$results = array();

while ($row = $result->fetch_assoc()){
    $results[] = $row;
}

foreach ($results as $result){
    echo $result['type'];
    echo $result['rent_price'];
}


在结果中循环两次,而不重置。仅尝试循环一次:

$result = $mysqli->query("SELECT * FROM dbc_posts ORDER BY ID ASC limit 3");

$set = array ();
$set1 = array ();

while ($row = $result->fetch_assoc())
{
  $set[] = $row['type'];
  $set1[] =$row['rent_price'];
}
?>

<?php echo $set[0];?>
<?php echo $set1[1];?>
$result=$mysqli->query(“从dbc中选择*按ID ASC限制3发布订单”);
$set=array();
$set1=array();
而($row=$result->fetch_assoc())
{
$set[]=$row['type'];
$set1[]=$row['租金价格'];
}
?>

根据一节中“a”和另一节中“200”的含义,您可以放弃创建中间数组,只需在获取它们时打印查询中的值即可。表行中的两个单元格,例如:

while ($row = $result->fetch_assoc()) {
    echo "<tr><td>$row[type]</td><td>$row[rent_price]</td></tr>";
}
while($row=$result->fetch_assoc()){
回显“$行[类型]$行[租金价格]”;
}

非常感谢您的帮助,这似乎正是我想要的,谢谢您如果只返回一行会怎么样?;)
while ($row = $result->fetch_assoc()) {
    echo "<tr><td>$row[type]</td><td>$row[rent_price]</td></tr>";
}