Joomla!, Wordpress tips for building your site

How to use your original mysql table in Wordpress post.

Published on| February 18th, 2011 | 2 Comments.

This time, I will show how to use (search, update, etc.) your original mysql table in WordPress post.

You know how to use mysql tables for WordPress. So, you may use some WordPress API(TAG).

But can you use your original mysql table ( not provided by WordPress ) ?
This way only uses WordPress API(TAG) too. It is very easy.

The following will show how to use WordPress API(TAG) for it.

1st step, runPHP and create table for test

1st step, you should install the plugin for use php in WordPress post.
I wrote the post before about it.( See ;WordPress in PHP to run the article )

Also, you should create original mysql table for test.

The following example, I create simple sample mysql table for test.

* You may use the following example, If you can log on your web-server by SSH (or telnet).

But if you can not log on , you should use phpAdmin. So, you should run only the following SQL for table create.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> mysql -uxxx -pyyy -Dzzz
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.36-community MySQL Community Server (GPL)
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> CREATE TABLE `test_run_post` (
    -> `strkey` varchar(32) ,
    -> `numkey` int         ,
    -> PRIMARY KEY (`strkey`) );
Query OK, 0 rows affected (0.11 sec)
 
mysql>

Line 1: -Uxxx: Specify the username. xxx is the name of the user.

-Pyyy: Specify the password for your account.yyy is the password.

-Dzzz: Specify the database name to use.zzz is the name of the database.

Note
You should use same database name of WordPress using.

Line 8: Execute “create table”.
sql statement.

CREATE TABLE `test_run_post` (
  `strkey` varchar(32) ,
  `numkey` int         ,
  PRIMARY KEY (`strkey`) );


It is a simple table.


strkey: String

numkey: Integer

Only two pieces of information.

How to use your original mysql table in WordPress post

You will write php codes for operation your original mysql table in WordPress post.

The following table records removed for testing and record addition, record an example reference is made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
global $wpdb;
 
$html = '';
//	delete all table values.
$sql   = 'DELETE FROM `test_run_post`';
$query_result = $wpdb->query($sql);
if($query_result!==false){
	//	OK
	//	insert table values.
	for($ni=1;$ni<10;$ni++){
		$sql   = "INSERT INTO `test_run_post`(`strkey`,`numkey`) values('testkey".$ni."',".$ni.")";
		$query_result = $wpdb->query($sql);
		if($query_result===false){
			$html .= "*** table insert error\n$sql";
			break;
		}
	}
	if(empty($html)){
		//	no-error 
		//	print table values.
		$html .= '<table><tr><td><b>strkey</b></td><td><b>numkey</b></td></tr>';
		$sql   = 'SELECT * FROM `test_run_post`';
		$results = $wpdb->get_results($sql , ARRAY_A);
 
		foreach($results as $row){
			$html .= '<tr><td>'.$row['strkey'].'</td><td>'.$row['numkey'].'</td></tr>';
		}
		$html .= '</table>';
	}
}  else {
	$html .= "*** table delete error\n";
}
 
//	print out results.
echo $html;
?>

Line 1: WordPress provide an object for operation mysql database.

You can use use the object by declaring “Global”.
Line 5-6: Once you remove all the table values for test.
Lines 10-17: Adds you add 9 recoreds to table for test.
Lines 19-29: All the data in the HTML output in the form of a test table.

The following will actually work with it.

strkey numkey
testkey1 1
testkey2 2
testkey3 3
testkey4 4
testkey5 5
testkey6 6
testkey7 7
testkey8 8
testkey9 9

Hey, I could show how to use your original mysql table in WordPress post.

Here is an example of execution are described in the post, also in the theme template, you can work with as well.
This example will work in WordPress post , and theme template too.

If you can use this technique, you may show original table values on WordPress post and very easy.






Comments

2 Responses to “How to use your original mysql table in WordPress post.”

  1. Neethu
    July 19th, 2011 @ 20:51:09

    Good one.This is veryhelpful for the wordpress beginners.Thanks for such a interesting tutorial.

  2. taro
    August 4th, 2011 @ 17:55:52

    This is Admin.
    Thanks.

Leave a Reply








Transrator

Recent Posts

Categories

Tag Cloud

execute remove フロントページ 投稿ページ トップページ install WIndows Note Convert META generator 日付 donwload file manage multibyte utf-8 unicode shiftjis euc console サイトマップ 問題 ParmaLink Redirect パーマリンク はみ出る pre テンプレート テーマ タグクラウド マルチランゲージ リダイレクト PHP(タグ) タグ table control HTML(タグ) コマンド 国際化(翻訳) SQLite(タグ) MySQL(タグ) qTranslate プラグイン(タグ) Wordpress(タグ)

Links

Site Description

Joomla!, Wordpress as CMS using a site building, site management, software usage to describe the development tips.

  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ