PHP mysqli返回行并设置为var help [英] PHP mysqli return row and set to var help

查看:64
本文介绍了PHP mysqli返回行并设置为var help的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的php不太好,我正在努力解决可能很简单的问题.基本上我有以下代码,如果找到1行,我想将结果设置为$ aPrds,我该怎么做?

My php is not very good, and i'm struggling with something that is probably quite simple. Basically I have the following code and where if 1 row is found I would like to set the result to $aPrds, how can I do this?

$stmt = $db->prepare("select * from products where id=?");
$stmt->bind_param("s", $_GET['id']);

if($stmt->execute())
{
  $stmt->store_result();
  echo $stmt->num_rows;
  if($stmt->num_rows==1)
  {
    //SET RETURNED ROW TO aPrds 

  }
  else
  {
    echo "no results or too many found";
  }
}
else
{
   echo "sql invalid";
}

------------------ UPDATE ------------

------------------UPDATE------------

我还尝试了以下不成功的代码(返回(空)):

I have also tried the following code which has been unsuccessful (returns (null)):

            $stmt = $db->prepare("select productid, product_name, description from product where productid=?");
            $a=1;
            $stmt->bind_param("i", $a);
            if($stmt->execute()){
                $stmt->store_result();
                if($stmt->num_rows==1){
                    $stmt->bind_result($b, $c, $d);         
                    print_r($b);
                    print_r($c);
                    print_r($aPrds);
                }else{
                    echo "no result or more than 1 returned";
                }
            }else{
                echo "invalid sql";
            }

请注意,我已经对sql进行了测试,并且可以正常工作,而且$ db mysqli连接肯定可以正常工作.

Please note that I have tested the sql and it works, also the $db mysqli connection is definitely working.

推荐答案

我认为您正在寻找 fetch_assoc 方法:

I think you are looking for the get_result and fetch_assoc methods:

// ....
$result = $stmt->get_result();
$aPrds = $result->fetch_assoc();
// ....

显然,这些功能尚不可用(应该对此进行了测试,对不起).因此,这已经过测试:

Apparently these functions are not yet available (should have tested this, sorry). So this is tested:

function bind_array($stmt, &$row) {
    $md = $stmt->result_metadata();
    $params = array();
    while($field = $md->fetch_field()) {
        $params[] = &$row[$field->name];
    }

    call_user_func_array(array($stmt, 'bind_result'), $params);
}

// ....

if($stmt->execute()) {
    bind_array($stmt, $row);
    $stmt->fetch();

    print_r($row);

    // ....

如果在$stmt->bind_result($b, $c, $d);

这篇关于PHP mysqli返回行并设置为var help的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆