这是我想要做的;我将一个编码数据作为查询变量传递给我的网页,然后在我的php页面中,解码数据并在数据库中检查相同的数据。我使用的代码如下所示:PHP从mysql数据库中获取查询字符串变量的数据
<?php
// Get the ID from URL.
$id = (isset($_GET["id"]) && !empty($_GET["id"]) ? $_GET["id"] : "");
// If "id" is not empty, proceed.
if(!empty($id)) {
$id = base64_decode($id);
global $wpdb;
$res = $wpdb->query("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found.";
for($i=0; $i<count($res); $i++) {
echo $res[$i]->id;
}
exit;
}
else {
echo "No ID";
exit;
}
?>
我在我的数据库中有一条记录。上面的代码正确地说“找到1条记录”。我不确定如何获得该行中的字段值。我有3列,他们是id,field1和field2。代码“echo $res[$i]->id
”不返回任何内容。
请帮
顺便说一句,我在我的WordPress的博客尝试这一点。
谢谢大家的建议。我试过你的建议,这里是我的结果:
$res = $wpdb->query("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";
它说找到1条记录。
$res = $wpdb->get_row("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";
它说找到0条记录。
$res = $wpdb->get_results("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";
它说找到0条记录。
while ($row = mysql_fetch_array($res)) {
echo $row[0];
}
如果我使用mysql_fetch_array,
警告:mysql_fetch_array():提供的参数不是在/home/myfolder/mytheme/index.php一个有效的MySQL结果资源上线38
第38行是while($ row = mysql_fetch_array($ res)){。
我试图做到:
我有一个WordPress博客。所有上面的代码进入我的index.php。我会通过查询字符串变量将产品ID传递给我的index.php。我在我的wordpress数据库中创建了一个名为S_redirect的新表。我将检索查询字符串变量id的值,并在我的数据库表中检查相同的值。如果记录存在,则从该表格行中检索其中一个列值(产品的url)并重定向到该产品url。如果记录不存在,则重定向到主页。希望这有助于大家了解我在做什么。
'的var_dump($水库);' – zerkms 2011-03-27 12:43:17
根据http://codex.wordpress.org/Function_Reference/wpdb_Class你最好'get_results'方法 – zerkms 2011-03-27 12:49:22
好吧,我已经使用$ wpdb-> get_results尝试。我找到0条记录。 – joghn 2011-03-27 13:13:09