I have the following code:
我有以下代码:
$link = new PDO("mysql:dbname=$databasename;host=127.0.0.1",$username,$password);
$query = $link->prepare("SELECT * FROM index WHERE sbeid=:idvar");
for($j = 1; $j < count($array); $j++)
{
if($array[$j][16] == "TRUE" || $array[$j][16] == "FALSE")
{
$paramforquery = $array[$j][25];
$query->bindParam(":idvar",$paramforquery);
$query->execute();
$result = $query->fetchAll();
//do things with the $result
$query->closeCursor();
}
//else if, do stuff
}
$link = null;
$array
is a large array composed of input from a CSV file that successfully loads via fopen()
.
$ array是一个大型数组,由CSV文件的输入组成,该文件通过fopen()成功加载。
My problem is this: the query just doesn't work. I know for a fact (ran the query directly on the server with some sample values from the file) that the data is in the database, but when i var_dump
the $result
s each time the for
loop runs, I just get an empty array.
我的问题是:查询不起作用。我知道一个事实(直接在服务器上运行查询,文件中有一些样本值),数据在数据库中,但是当每次for循环运行时我得到$ var_dump,我只得到一个空数组。
What am I doing wrong?
我究竟做错了什么?
TIA.
Increase the error reporting - the standard advice.
Set the error mode of the pdo object to ERRMODE_EXCEPTION - you hardly can miss an error that way.
Use a debugger or add some debug output to your script - a real debugger is way better.
增加错误报告 - 标准建议。将pdo对象的错误模式设置为ERRMODE_EXCEPTION - 您几乎不会错过任何错误。使用调试器或向脚本添加一些调试输出 - 真正的调试器更好。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$array = foo();
echo '<pre>Debug: |array|=', count($array), '</pre>';
$link = new PDO("mysql:dbname=$databasename;host=127.0.0.1",$username,$password);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $link->prepare("SELECT * FROM index WHERE sbeid=:idvar");
$query->bindParam(":idvar", $paramforquery);
foreach($array as $row) {
echo '<pre>Debug: row[16]='; var_dump($row[16]); echo '</pre>';
if($row[16] == "TRUE" || $row[16] == "FALSE") {
$paramforquery = $row[25];
echo '<pre>Debug: paramforquery='; var_dump($paramforquery); echo '</pre>';
$query->execute();
echo '<pre>Debug: rowcount='; var_dump($query->rowCount()); echo '</pre>';
$result = $query->fetchAll();
//do things with the $result
$query->closeCursor();
}
//else if, do stuff
}
$link = null;
Are you sure you are getting a connection ?
你确定你有联系吗?
try {
$link = new PDO("mysql:dbname=$databasename;host=127.0.0.1",$username,$password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
}
This will catch any exceptions from trying to connect. If this works ok, try putting the query under the $link line and see what's returned.
这将捕获尝试连接的任何异常。如果这样可行,请尝试将查询放在$ link行下,看看返回了什么。
If your query runs manually, i'd say its something to do with your DB connection. Make sure you've got error reporting turned on.
如果您的查询是手动运行的,我会说它与您的数据库连接有关。确保您已启用错误报告。
Additional: In your query you have this :idvar ? Shouldnt you be using a PHP variable like this $idvar.
附加:在您的查询中,你有这个:idvar?你不应该使用像这个$ idvar这样的PHP变量。
so
$query = $link->prepare("SELECT * FROM index WHERE sbeid=" . $idvar);
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/28/2f90ff0caeb63f4824d15441ef227feb.html。