简体   繁体   English

PHP从mysql数据库获取数据以获取查询字符串变量

[英]PHP getting data from mysql database for the Query string variable

Here is what I would like to do; 这是我想做的; I am passing an encoded data as a query variable to my webpage, then in my php page, I am decoding the data and checking the same in my database. 我将经过编码的数据作为查询变量传递到我的网页,然后在我的php页面中,我对数据进行解码并在数据库中对其进行检查。 The code I am using is shown below: 我正在使用的代码如下所示:

<?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;
}
?>

I have one record in my database. 我的数据库中有一条记录。 The above code correctly says "1 records found". 上面的代码正确地说“找到1条记录”。 I am not sure how to get the value of the field in that row. 我不确定如何获取该行中字段的值。 I have 3 columns, they are id, field1 and field2. 我有3列,分别是id,field1和field2。 The code " echo $res[$i]->id " returns nothing. 代码“ echo $res[$i]->id ”不返回任何内容。

Please help 请帮忙

BTW, I am trying this in my Wordpress blog. 顺便说一句,我正在我的Wordpress博客中尝试此操作。

Thank you all for your suggestions. 谢谢大家的建议。 I tried your suggestions and here are my results: 我尝试了您的建议,这是我的结果:

$res = $wpdb->query("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";

it says 1 records found. 它说找到1条记录。

$res = $wpdb->get_row("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";

it says 0 records found. 它说找到0条记录。

$res = $wpdb->get_results("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = count($res);
echo $tot . " records found."."</br>";  

it says 0 records found. 它说找到0条记录。

while ($row = mysql_fetch_array($res)) {
echo $row[0];  
}

If I use mysql_fetch_array, 如果我使用mysql_fetch_array,

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/myfolder/mytheme/index.php on line 38. 警告:mysql_fetch_array():在第38行的/home/myfolder/mytheme/index.php中提供的参数不是有效的MySQL结果资源。

line 38 is while ($row = mysql_fetch_array($res)) {. 第38行是while($ row = mysql_fetch_array($ res)){。

What I am trying to accomplish: 我要完成的工作:

I have a wordpress blog. 我有一个wordpress博客。 All the above code goes into my index.php. 上面所有的代码都进入了我的index.php。 I will pass a product id to my index.php via query string variable. 我将通过查询字符串变量将产品ID传递给我的index.php。 I have created a new table called S_redirect in my wordpress database. 我在我的wordpress数据库中创建了一个名为S_redirect的新表。 I will retrieve the value of query string variable id and check the same in my database table. 我将检索查询字符串变量id的值,并在数据库表中对其进行检查。 If record exists, then retrieve one of the column value (url of the product) from that table row and redirect to that product url. 如果记录存在,则从该表行中检索列值之一(产品的网址),然后重定向到该产品的网址。 If the record doesn't exists then redirect to home page. 如果记录不存在,则重定向到主页。 hope this helps everyone to understand what I am doing. 希望这可以帮助每个人了解我在做什么。

Judging by $wpdb , it looks like this is a WordPress. $wpdb来看,这似乎是一个WordPress。 Hopefully this will help: http://codex.wordpress.org/Function_Reference/wpdb_Class 希望这会有所帮助: http : //codex.wordpress.org/Function_Reference/wpdb_Class


Update 更新资料

Don't use count() to get the number of rows; 不要使用count()来获取行数; use $wpdb->num_rows (no () because it's a property, not a function). 使用$wpdb->num_rows (no ()因为它是属性,而不是函数)。 The count() function always returns 1 for any non-null value that it doesn't recognize as a "countable" value (ie, arrays and certain objects), so it's likely your code will always yield 1. 对于任何无法识别为“可计数”值的非空值(即数组和某些对象), count()函数始终返回1,因此您的代码很可能始终产生1。

$tot = $wpdb->num_rows;

===== =====
If you run your query with get_results() instead of query() , then you can loop through the results like this: 如果使用get_results()而不是query()运行查询,则可以像这样循环遍历结果:

foreach ($res as $row) {
    echo $row->id;
}

Ultimately, I think your code will end up looking like this: 最终,我认为您的代码最终将看起来像这样:

$res = $wpdb->get_results("SELECT * FROM S_redirect WHERE source = ".$id);
$tot = $wpdb->num_rows;

echo $tot . " records found.";

foreach ($res as $row) {
    echo $row->id;
}

Disclaimer: This is untested, as I've never used WordPress. 免责声明:这是未经测试的,因为我从未使用过WordPress。 This is just how I understand it from the documentation linked above. 这就是我从上面链接的文档中了解的方式。 Hopefully it at least gets you on the right track. 希望它至少能使您走上正确的道路。

Use: 采用:

$tot = count(mysql_fetch_array($res));

or 要么

$tot = mysql_num_rows($res);

The best is 最好的是

while($row = mysql_fetch_assoc($res)){ echo $row['column_name']; while($ row = mysql_fetch_assoc($ res)){echo $ row ['column_name']; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM