如何使用MySQLi或$wpdb在WordPress中获取数据

时间:2016-07-24 作者:Ramesh Pardhi

我有这样的自定义表:

useraw

1. id (Primary*)
2. user_ip
3. post_id
4. time
我正在使用在表中插入数据

$wpdb->insert($table_name , array(\'user_ip\' => $user_ip, \'post_id\' =>
$postID, \'time\' => $visittime),array(\'%s\',\'%d\', \'%d\') );
我使用此代码插入了四行:

id                  :        245
user_ip             :        245.346.234.22
post_id             :        24434
time                :        255464

id                  :        345
user_ip             :        245.346.234.22
post_id             :        23456
time                :        23467

id                  :        567
user_ip             :        245.346.234.22
post_id             :        57436
time                :        5678

id                  :        234
user_ip             :        245.356.134.22
post_id             :        2356
time                :        45678
我想学习如何在WordPress中使用MySQL查询。下面是我的问题:

如何显示表格的所有数据time 哪里user_ip = 245.356.134.22

非常感谢。

3 个回复
最合适的回答,由SO网友:Rishabh 整理而成

To fetch data from database table

<?php
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(!empty($results))                        // Checking if $results have some values or not
{    
    echo "<table width=\'100%\' border=\'0\'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
    echo "<tbody>";      
    foreach($results as $row){   
    $userip = $row->user_ip;               //putting the user_ip field value in variable to use it later in update query
    echo "<tr>";                           // Adding rows of table inside foreach loop
    echo "<th>ID</th>" . "<td>" . $row->id . "</td>";
    echo "</tr>";
    echo "<td colspan=\'2\'><hr size=\'1\'></td>";
    echo "<tr>";        
    echo "<th>User IP</th>" . "<td>" . $row->user_ip . "</td>";   //fetching data from user_ip field
    echo "</tr>";
    echo "<td colspan=\'2\'><hr size=\'1\'></td>";
    echo "<tr>";        
    echo "<th>Post ID</th>" . "<td>" . $row->post_id . "</td>";
    echo "</tr>";
    echo "<td colspan=\'2\'><hr size=\'1\'></td>";
    echo "<tr>";        
    echo "<th>Time</th>" . "<td>" . $row->time . "</td>";
    echo "</tr>";
    echo "<td colspan=\'2\'><hr size=\'1\'></td>";
    }
    echo "</tbody>";
    echo "</table>"; 

}
?>
NOTE: Change your data fetching format according to your need (table structure)

To update time field on if condition

<?php if($userip==245.356.134.22){  //Checking if user_ip field have following value
$wpdb->update( 
$table_name, 
array( 
    \'time\' => \'YOUR NEW TIME\' // Entring the new value for time field
),      
array(\'%d\')                   // Specify the datatype of time field
);
}
?>

Update

如果要检查要插入到数据库中的IP是否已经存在,请按如下方式进行检查

<?php
global $wpdb,$ip;
$results = $wpdb->get_results( "SELECT user_ip FROM $table_name");  //query to fetch record only from user_ip field

$new_ip = 245.356.134.22;   //New Ip address storing in variable

if(!empty($results))                       
{    
    foreach($results as $row){  
    $old_ip = $row->user_ip;        // putting the value of user_ip field in variable
    if($new_ip==$old_ip){           //  comparing new ip address with old ip addresses
      $ip = \'Already Exist\';        // if ip already exist in database then assigning some string to variable
    }
    }

}
if($ip = \'Already Exist\'){          // Checking if variable have some string (It has some string only when if IP already exist in database as checked in if condition by comparing old ips with new ip)
//Insert query according to Ip already exist in database
}else{
//Insert query according to Ip doesn\'t exist in database
}


?>

SO网友:Ganesh
//For fetching data use
global $wpdb;
$results = $wpdb->get_results("SLECT * FROM table_name"); 
//and for update use below code 
$wpdb->update( 
  $table_name, 
  array( 
    \'time\' => time(),   // string
  ), 
  array( \'user_ip\' => \'245.356.134.22\' ), 
  array(\'%s\'), 
  array( \'%d\' )
);
SO网友:hosker

您在问题中指出MYSQLi,但在问题中标记并引用MySQL。如果您使用的是MySQL,以下内容将适用于您:

这相当简单,考虑到您已经构造了insert语句,您可以像这样使用update:

$wpdb->update($table_name , array(\'user_ip\' => $user_ip, \'post_id\' =>$postID, \'time\' => $visittime),array(\'%s\',\'%d\', \'%d\') );
然后,您所要做的就是在查询中设置要更改数据库中哪些数据的值。你可以找到examples here

至于获取所有数据,您可以使用以下方法:

$results = $wpdb->get_results("SELECT * FROM table_name"); 
您可以向其中添加任何WHERE参数,也可以像和标准SQL查询语句一样对其进行排序。然后可以使用foreach循环遍历is,然后只回显检索到的数据。

相关推荐

$wpdb->GET_RESULTS()足够安全吗

原始问题如果我使用get\\u results()函数,这对mysql注入攻击足够安全吗?global $wpdb; $wpdb->get_results("select * from tableA where B = C"); 还是应该先做些准备?最佳做法是什么?在2021 12月的今天,我们应该如何做到这一点?更新,所以我应该用这个来代替?global $wpdb; $my_variable = "sometext"; $p