dounao5856 2016-09-25 14:29
浏览 29
已采纳

PHP在准备好的语句中选择多于1个结果

Ok, so let's assume i have a database like this

//table name is: exampleTable
 _________________________
| ID | NAME | EXAMPLEDATA |
|----|------|-------------|
| 0  | TOD  | a123        |
| 1  | JEF  | 142b        |
| 2  | MAX  | c412        |
| 3  | TOD  | 124d        |
| 4  | TOD  | e634        |
| 5  | MAX  | 233f        |
| 6  | MAX  | g444        |
|____|______|_____________|

How would i get all the values of EXAMPLEDATE with a query like this ( using prepared statements )

SELECT EXAMPLEDATA FROM exampleTable WHERE NAME = 'TOD'

and store results into an array so i can access it like this :

$todArray[0] // equals a123
$todArray[1] // equals 124d
$todArray[2] // equals e634

The only way i know how to use prepared statements to get results is to use bind_result but that doesn't take the result(s) and put it in an array like what i showed above.

So here is what I've tried but it failed to work

$TOD = "TOD";
$dbCON = new mysqli(blah, blah, blah, blah);
$getData = $dbCON->prepare("SELECT `EXAMPLEDATA` FROM `exampleTable` WHERE `NAME`=?");
$getData->bind_param("s", $NAMEVAR);
$NAMEVAR = $TOD;
$getData->execute();
$getData->bind_result($todArray);
$getData->fetch();
$getData->close();

Any help would be appreciated thanks :)

  • 写回答

1条回答 默认 最新

  • douzhuo6270 2016-09-25 15:03
    关注

    You can do this :

    First and i prefer that you change your db connect from mysqli to PDO .

    Now you can can type the query like this with PDO :

    <?php 
    
                $servername = "your server name";
                $username  = "your username";
                $password   = "your password";
                $database  = "your database name";
    
                try {
                        $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
                        // set the PDO error mode to exception
                        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    }
                catch(PDOException $e)
                    {
                        echo "Connection failed: " . $e->getMessage();
                    }
                // End of database connection 
    
                $TOD = "TOD";
                $fetchdata = $conn->prepare("SELECT EXAMPLEDATA FROM exampleTable WHHERE NAME = ?");
                $fetchdata->execute(array($TOD));
    
                if($fetchdata)
                    {
                        while($fd = $fetchdata->fetch(PDO::FETCH_ASSOC))
                        {
                            echo $fd['EXAMPLEDATA']."
    ";
                        }
                    }
                else
                    {
                        echo "Query did not executed";
                    }
    
    ?>
    

    Now you can store the fetched data in array by doing this :

    if($fetchdata)
            {
                $ResultsArray = array();
    
                while($fd = $fetchdata->fetch(PDO::FETCH_ASSOC))
                    {
                        $ResultsArray[] = $fd['EXAMPLEDATA'];
                    }
            }
    

    Edit : This is MYSQLI code ( Not tested )

    $TOD = "TOD";
    $dbCON = new mysqli(blah, blah, blah, blah);
    $getData = $dbCON->prepare("SELECT `EXAMPLEDATA` FROM `exampleTable` WHERE `NAME`=?");
    $NAMEVAR = $TOD;
    $getData->bind_param("s", $NAMEVAR);
    $getData->execute();
    call_user_func_array(array($mysqli_stmt_object,"bind_result"),$AnArray);
    $ResultsArray = array();
    
    while ($mysqli_stmt_object->fetch()) {
        $results[] = $AnArray;
    }
    
    $getData->close();
    
    ?>
    

    You can find more here :

    Mysqli - Bind results to an Array

    Im sorry im not good in MYSQLI , hope the code works , but i truly recommend that you start using PDO , its much easier .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器