显示记录或字段的非 oop 方式 - mysqli

non-oop way to display records or fields - mysqli

我一直在使用 mysql,它满足了我的要求,但随着我的项目越来越大,我决定选择 mysqli。

我查看了 enter link description here 上的教程,它非常简单,直到我想显示一些数据

存储过程(connect.php)

<?php

function db_connect() {

    // Define connection as a static variable, to avoid connecting more than once 
    static $con;

    // Try and connect to the database, if a connection has not been established yet
    if(!isset($con)) {
         // Load configuration as an array. Use the actual location of your configuration file
        $config = parse_ini_file('config.ini'); 
        $con = mysqli_connect('localhost',$config['username'],$config['password'],$config['dbname']);
    }

    // If connection was not successful, handle the error
    if( $con === false) {
        // Handle error - notify administrator, log to a file, show an error screen, etc.
        return mysqli_connect_error(); 
    }
    return  $con;
}

function db_query($query) {
    // Connect to the database
     $con = db_connect();


    // Query the database
    $result = mysqli_query( $con,$query);

    return $result;
}

function db_error() {
     $con = db_connect();

        return mysqli_error($con);
}

function db_select($query) {
    $rows = array();
    $result = db_query($query);

    // If query failed, return `false`
    if($result === false) {
        return false;
    }

    // If query was successful, retrieve all the rows into an array
    while ($row = mysqli_fetch_assoc($result)) {
        $rows[] = $row;
    }
    return $rows;
}
function db_quote($value) {
     $con = db_connect();

        return "'" . mysqli_real_escape_string($con,$value) . "'";
}
?>

php/html

<div class="grid_4">
        <div class="left-1">
            <h2 class="top-1 p3">Find a property</h2>
            <form id="form-1" method="post" class="form-1 bot-1" action="prop_result.php">
                <div class="select-1">
                    <label>Select Area</label>
                    <select name="field4" id="field4" >
    <?php



$rows = db_select("SELECT id,city_id,area FROM area");
    if($rows === false) {
    $error = db_error();

    }else
    {
    while($rows=mysqli_fetch_assoc($result))
{
?>
<option value=""><?php $rows['
area'];?></option>

<?php 
}}

?>

                    </select>   
                </div>

我不明白的是如何在 while 循环中使用存储过程,这样它将在 ID 和 Area 字段中输出数据,这样我的 select 框和任何其他输入就可以正确填充基于查询

当前

我尝试过不同的方法:

<?php



$rows = db_select("SELECT id,city_id,area FROM area");
    if($rows === false) {
    $error = db_error();

        }
    while($rows=mysqli_fetch_assoc($result))
{
?>
<option value=""><?php $rows['
area'];?></option>

<?php 
}

?>

$rows = db_select("SELECT id,city_id,area FROM area");
    if($rows === false) {
    $error = db_error();

        }
{
?>
<option value=""><?php $rows['
area'];?></option>

<?php 
}

?>

 <?php



$rows = db_select("SELECT area_id from property");
    if($rows === false) {
    $error = db_error();

    }

{
echo "<option value='".$rows['id']."'>".$rows[$col4]."</option>";
}
    ?>

None 这些输出任何数据。回显 $rows 不提供任何数据。我不知道使用存储过程显示输出的逻辑是什么。

任何帮助将不胜感激,如果需要任何其他信息来帮助解决此问题,请告诉我。

很高兴听到数据正在返回。试试这个尺码...

foreach($rows as $key => $value){
    foreach($value as $k => $v){
        if($k == 'id'){
           $newID = $v;
        }
        if($k == 'type'){
           $newType = $v
        }
    }
    echo "<option value='".$newID."'>".$newType."</option>";
}

有了这个,您应该可以根据自己的喜好使用它。

编辑:直到后来才看到其他数组...嵌套循环应该更适合您。

Siniseus 方法可行,但对于一个简单的任务来说它的代码太多了。我确实使用它最终得出了这个

<select name="field4" id="field4" >

<?php       
$rows = db_select("SELECT id, city_id,area FROM area");
foreach($rows as $row){
echo "<option value='".$row['id']."'>".$row['area']."</option>";
}
?>
</select>

简单、干净、非常直接,没有太多变数。

$rows = db_select ("Select Query")foreach($rows as $row){ 做这个 }