Come recuperare i dati in WordPress usando MySQLi o $ wpdb
3 risposta
- voti
-
- 2016-07-25
Per recuperarei dati dallatabella del database
<?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)
Per aggiornareil campo dell'ora sulla condizioneif
<?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 ); } ?>
<"Aggiorna"
Se vuoi controllare se l'IP che staiperinserirenel database ègiàesistente omeno,controllaloin questomodo
<?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 } ?>
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
If you want to check if the IP you are going to insert in database is already exist or not then check it like this
<?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 } ?>
-
Èfantastico ...puoi dirmi comeposso controllare se l'indirizzo `ip` ègiànel database ono?Se l'indirizzo IP ègiànel database,eseguiilprogramma 1e senon lo è,eseguiilprogramma 2. So che dobbiamo usare `WHERE`nella querymanon sobene usarloThat's great... can you tell me how I can check if the `ip` address is already in database or not? If Ip address already in database then run program 1 and if it's not, then run program 2. I know we need to use `WHERE` in query but I dont know hot to use it
- 0
- 2016-07-25
- Ramesh Pardhi
-
Hai visto la risposta aggiornata?Have you seen the updated answer?
- 0
- 2016-07-26
- Rishabh
-
Hoprovato questoe funziona.Main questomomento sta recuperandotuttii dati dal database usando `foreach`,quindi sta compilandoilnostro codiceper verificare se l'ip è lo stesso omeno.C'è un altromodoperfar controllare quell'ip direttamente usando la query del database?I tried this and its working. But right now its fetching all the data from database using `foreach`, then its compiling our code to check if the ip is same or not. Is there any other way to get that ip get checked directly using database query?
- 0
- 2016-07-26
- Ramesh Pardhi
-
La risposta è stata aggiornata!Controlla se è quello cheti serve.Answer has been updated! Check if its what you need.
- 0
- 2016-07-27
- Rishabh
-
Rishabh Grazieper aiutarmi ... Solo un'ultima domanda.Posso usare query come `SELECT user_ip FROM $table_name WHERE` user_ip`=$ username`.Funzionerà?Grazieper latua rispostae aiuta Rishabh.Rishabh Thank you to help me... Just one last question. Can I use query like `SELECT user_ip FROM $table_name WHERE `user_ip` = $username`. Will it work? Thank you for your answer and help Rishabh.
- 0
- 2016-07-27
- Ramesh Pardhi
-
Sì,puoi usarloin questomodo` $ risultati=$ wpdb->get_results ("SELECT user_ip FROM $table_name WHERE user_ip='$ username'"); `Yes you can use it like this` $results = $wpdb->get_results( "SELECT user_ip FROM $table_name WHERE user_ip = '$username' ");`
- 1
- 2016-07-27
- Rishabh
-
- 2016-07-27
//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' ) );
//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' ) );
-
Perfavore aggiungi alcuni commenti davanti alletue righe di codicein modo cheil visualizzatorepossa capire cosafail codicee come.Please add some comments in front of you code lines so that viewer can understand what you code does and how.
- 0
- 2016-07-27
- Rishabh
-
@Rishabhnon c'èbisogno di ulteriori commentipoiché hoinserito un commento appropriatoprima del codice.@Rishabh no need for more comment as i have inserted appropriate comment before the code.
- 0
- 2016-07-27
- Ganesh
-
- 2016-07-25
Indichi MYSQLinellatua domanda,maetichettie fai riferimento a MySQLnellatua domanda. Se stai usando MySQL,quanto seguefunzioneràperte:
Questo è abbastanza semplicee,dato che haigià costruito l'istruzioneinsert,puoi semplicemente usare updatein questomodo:
$wpdb->update($table_name , array('user_ip' => $user_ip, 'post_id' =>$postID, 'time' => $visittime),array('%s','%d', '%d') );
Quinditutto ciò che dovrestifare èimpostarei valorinellatua query su quali datinel DB desiderimodificare. Puoitrovareesempi qui: https://codex.wordpress.org/Class_Reference/wpdb#UPDATE_rows
Per quanto riguardail recupero dituttii dati,potresti utilizzare quanto segue:
$results = $wpdb->get_results("SLECT * FROM table_name");
Èpossibile aggiungere qualsiasiparametro WHERE o ordinarlo come un'istruzione di query SQL standard. Puoi semplicementeeseguireil loopthroughis con un loopforeache quindifaresti semplicemente l'eco deituoi dati recuperati.
You indicate MYSQLi in your question, but label and refer to MySQL in your question. If you are using MySQL the following will work for you:
This is fairly simple, and given the fact that you already have the insert statement constructed, you can just use update like so:
$wpdb->update($table_name , array('user_ip' => $user_ip, 'post_id' =>$postID, 'time' => $visittime),array('%s','%d', '%d') );
Then all you would have to do is set the values in your query as to what data in the DB you want to change. You can find examples here: https://codex.wordpress.org/Class_Reference/wpdb#UPDATE_rows
As for fetching all of the data you could use the follow:
$results = $wpdb->get_results("SLECT * FROM table_name");
You can add any WHERE parameters to it or sort it just like and standard SQL query statement. You can just then loop through is with a foreach loop and then you would just echo out your data you retrieved.
-
Grazieper la risposta @hosker.Ma volevo sapere a caldoper recuperare quei "$ risultati"Thank you for reply @hosker. But I was wanted to know hot to fetch that `$results`
- 0
- 2016-07-25
- Ramesh Pardhi
-
Sono disposto ad aiutare,a differenza di @rishabh,che è stato cosìgentile da darti una risposta completa con la codifica,ti stavo solo dando lebasi su comefare quello che stavi chiedendo.Una semplice ricerca su Googlee uno sguardo al codice Wordpresspotrebbero aver risoltoiltuoproblema.Questiforum sono qui come una risorsae li uso,manonmi aspettereimai che qualcuno li codifichiperme.Ne avevigiàimpostatometà,maeritroppopigroper capirloe scrivereil codice da solo.Nonimparimai se qualcunofatuttoperte.I am willing to help, unlike @rishabh, who was nice enough to give you a complete answer with coding, I was just giving you the basics of how to do what you were asking for. A simple google search and a look through the Wordpress Codex could have solved your problem. These forums are here as a resource, and I use them, but I would never expect someone to code it for me. You had half of it already set, but were too lazy to figure it out and write the code yourself. You never learn if someone does it all for you.
- 1
- 2016-07-25
- hosker
-
@ hosker hai ragione amico!Lo studiopersonale ènecessario ... Orami rendo conto chenon avrei dovuto dare la risposta completa.@ hosker you are right buddy! Self study is must... Now I realize I shouldn't have given the complete answer.
- 0
- 2016-07-26
- Rishabh
-
Hmm!avete ragione ragazzi.quello che ho chiesto qui èpermio riferimento.Quello che sto cercando difare è qualcosa di diverso.Voglio sapere alcune coseper usarlo conilmioplugin.Quello che voglio sapere è come utilizzare queste querypermostrare l'IP che desidero senza recuperaretuttii dati dal database.Voglio una query che controlli l'IP che desideroe quindi lo visualizzi.E se ho 30000 IP?Recupereràtuttii 30000 IP usando "foreach".Non ègiusto!Eccoperché hobisogno deltuo aiuto.Hmm! you are right guys. what I asked here is for my reference. What I am trying to do is something different. I want to know some things to use it with my plugin. What I want to know is how to use these queries to show the IP I want without fetching all the data from database. I want a query which checks for the IP I want and then displays it. What if I have 30000 IPs? It will fetch all the 30000 IPs using `foreach`. That's not right! Thats why I need your help.
- 0
- 2016-07-26
- Ramesh Pardhi
Ho unatabellapersonalizzata come questa:
<"useraw"
Stoinserendo datinellatabella utilizzando
Ci sono quattro righe che hoinserito utilizzando questo codice:
Voglioimparare a usare le query MySQLin WordPress. Quindiecco lemie domande:
ora
doveuser_ip=245.356.134.22
Fammi sapere se c'è qualcosa che devoimparare.
Grazie