while 循环的问题
我的查询没有为我的第二个索引返回任何内容。它总是向我发送一条消息注意:未定义的偏移量:1
。我尝试使用 a for 它得到相同的结果,所以我读到我的问题出在查询中,有人告诉我让 $stmt
为 null
以释放资源我的发言。我不知道出了什么问题。
这些是我的方法。我不知道别人会说什么使用 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其一
是语法错误。
{ ... }
不适用于 PHP 中的数组。也许这只是一个拼写错误,您会将 PHP 与 javascsript 混淆。但更大的问题是
[]
。这告诉 PHP 将$arrayDirectory
视为数组(很好),但无论您分配什么,都将 PUSH 作为单个值。如果您的代码确实是:
这将创建以下结构:
请注意,它是一个 2 级数组。索引
[0]
处的顶级单元素数组。 0 处的元素包含另一个数组,其中包含您的两个用户名。你应该用这个代替:
For one,
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:
This would create the following structure:
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:
首先,你的函数错了,你忘记为 sizeof 函数添加结尾 ')',而且数组不是用 { } 定义的,你应该使用 array( ) 代替。
最后你在那里做了一个坏习惯;
你不应该在循环中调用函数(sizeof()),这样每次循环都会启动 sizeof() 函数,并且会占用一些资源,因为 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
and use that variable in your loop instead