while 循环的问题

发布于 2024-12-04 17:09:46 字数 905 浏览 0 评论 0原文

我的查询没有为我的第二个索引返回任何内容。它总是向我发送一条消息注意:未定义的偏移量:1。我尝试使用 a for 它得到相同的结果,所以我读到我的问题出在查询中,有人告诉我让 $stmtnull 以释放资源我的发言。我不知道出了什么问题。

这些是我的方法。我不知道别人会说什么使用 $database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);

示例: $arrayDirectory[] = {'user1', 'user2'};

它必须回显 1 2 但只打印我 1

for($i=0;$i<sizeof($arrayDirectory;$i++){ 
    $res[$i] = $obj->obtainID($arrayDirectory[$i]);

    echo $res[$i];
}

这是我的 acquireID 方法:

public function obtainID($user){
        $conexion = $this->objConexion->configuracion();
        $query = "CALL sp_xxx('$user')";
        $stmt = $conexion->prepare($query);
        $stmt->execute();
        $resultado = $stmt->fetchColumn();
        return $resultado;
    }

$stmi = null 在哪里?

My query does no return anything for my second index. It always sends me a message Notice: Undefined offset: 1. I tried doing with a for it is the same result, so I have read that my problem it is in the query, somebody told me let $stmt be null for freeing resources of my statement. I dont know what is wrong.

These are my methods. I dont know what to someone say use $database->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);

Example: $arrayDirectory[] = {'user1', 'user2'};

It must echo 1 2 but just prints me 1

for($i=0;$i<sizeof($arrayDirectory;$i++){ 
    $res[$i] = $obj->obtainID($arrayDirectory[$i]);

    echo $res[$i];
}

This is my obtainID method:

public function obtainID($user){
        $conexion = $this->objConexion->configuracion();
        $query = "CALL sp_xxx('$user')";
        $stmt = $conexion->prepare($query);
        $stmt->execute();
        $resultado = $stmt->fetchColumn();
        return $resultado;
    }

$stmi = null where?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请你别敷衍 2024-12-11 17:09:46

其一

 $arrayDirectory[] = {'user1', 'user2'};

是语法错误。 { ... } 不适用于 PHP 中的数组。也许这只是一个拼写错误,您会将 PHP 与 javascsript 混淆。

但更大的问题是 []。这告诉 PHP 将 $arrayDirectory 视为数组(很好),但无论您分配什么,都将 PUSH 作为单个值。

如果您的代码确实是:

$arrayDirectory[] = array('user1', 'user2');

这将创建以下结构:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "user1"
    [1]=>
    string(5) "user2"
  }
}

请注意,它是一个 2 级数组。索引 [0] 处的顶级单元素数组。 0 处的元素包含另一个数组,其中包含您的两个用户名。

你应该用这个代替:

$arrayDirectory = array('user1', 'user2');

$res = array();
foreach($arrayDirectory as $user) {
    $res[] = $obj->obtainID($user);
}

For one,

 $arrayDirectory[] = {'user1', 'user2'};

is a syntax error. { ... } does not work for arrays in PHP. Maybe it's just a typo and you're getting PHP confused with javascsript.

But the bigger issue is the []. That tells PHP to treat $arrayDirectory as an array (fine), but PUSH as a single value whatever you're assigning.

If your code was really:

$arrayDirectory[] = array('user1', 'user2');

This would create the following structure:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(5) "user1"
    [1]=>
    string(5) "user2"
  }
}

Note that it's a 2-level array. A top level single-element array at index [0]. That element at 0 contains ANOTHER array, which contains your two usernames.

You should have this instead:

$arrayDirectory = array('user1', 'user2');

$res = array();
foreach($arrayDirectory as $user) {
    $res[] = $obj->obtainID($user);
}
清风无影 2024-12-11 17:09:46

首先,你的函数错了,你忘记为 sizeof 函数添加结尾 ')',而且数组不是用 { } 定义的,你应该使用 array( ) 代替。

最后你在那里做了一个坏习惯;
你不应该在循环中调用函数(sizeof()),这样每次循环都会启动 sizeof() 函数,并且会占用一些资源,因为 sizeof($object) 在使用时不会改变循环,您应该将其保存在变量中

$sizeofobject = sizeof($object);

并在循环中使用该变量

first of all you are wrong with your function, you forgot to add the ending ')' for sizeof function,also arrays aren't defined with { } you should use array( ) instead.

and finally you are doing a bad practice over there;
you should not call a function in a loop (sizeof()), like this every time the loop goes through it will initiate sizeof() function and it will take some resource, since sizeof($object) won't change while using the loop, you should save it in a variable

$sizeofobject = sizeof($object);

and use that variable in your loop instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文