Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Monday, September 08, 2008

PHP mysqli_connectのサンプル

if (!($cn = mysqli_connect("localhost", "hoge", "hoge"))) {
die;
}

if (!(mysqli_select_db($cn,"test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysqli_query($cn,$sql))) {
die;
}

$i=0;
while ($item = mysqli_fetch_array($rs)) {
print "${item['id']} ";
print "${item['name']} ";
print "
";
$data[$i]['name']= $item['name'];
$data[$i]['tel']= $item['tel'];
$i++;
}

mysqli_close($cn);

Tuesday, August 26, 2008

外部キーの取得 MYSQL

select * from information_schema.table_constraints where constraint_type like 'for%'

Wednesday, August 20, 2008

mysql install

インストール
cd /home/**/app-src/mysql
wget mysql-**.tar.gz
./configure --prefix=/home/**/app/mysql
make; make install

DBの初期化
./scripts/mysql_install_db

サーバを起動
/home/**/app/mysql/bin/mysqld_safe &

パスワードを設定する
/home/***/app/mysql/bin/mysqladmin -u root password 'new-password'
パスワードはnew-passwordになる。

ユーザの追加
mysql -u root mysql

GRANT ALL PRIVILEGES ON *.* TO hogehoge@localhost IDENTIFIED BY 'password' WITH GRANT OPTION;
パスワードはpasswordとなる。


GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY 'hogehoge' WITH GRANT OPTION;




サーバーの停止
./mysqladmin -u root -p*** shutdown

Create table の表示
SHOW CREATE TABLE ****;

エラーはログを見ること。
次のエラーの場合、
Table 'mysql.host' doesn't exist

mysql_install_dbをすると直るときもある。

http://kajuhome.com/mysql.shtml#n07

Monday, August 04, 2008

mysql 場所

インストールの方法によって異なりますが。。。

/usr/local/mysql/var

ls -la
drwx------ 17 mysql mysql 4096 2000-00-00 00:00 var/

Monday, January 07, 2008

年齢 MYSQL PHP

select (YEAR(CURDATE())-YEAR(birth))- (RIGHT(CURDATE(),5) <RIGHT(birth,5)) AS age from hoge

----

PHPの場合

<?php
$birthday = '1990-01-12';
$lapse = getdate(mktime()-mktime(0,0,0,substr($birthday,5,2),
substr($birthday,8,2),substr($birthday,0,4)));
$age = $lapse['year']-1990;
echo $age;
?>
~

Thursday, May 10, 2007

Smarty MySQLとの連携方法2

前回紹介した方法より若干速度が速い。ただしプログラム側にデータベースのカラムを追加する必要がある。

array配列を使用したSmaryのassignのサンプル sectionの場合
---php---

if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}

$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}

$id= array();
$name= array();
$tel= array();
$email= array();

while ($item = mysql_fetch_array($rs)) {
array_push($id,$item['id']);
array_push($name,$item['name']);
array_push($tel,$item['tel']);
array_push($email,$item['email']);
}

$objSmarty->assign('id',$id);
$objSmarty->assign('name',$name);
$objSmarty->assign('tel',$tel);
$objSmarty->assign('email',$email);

mysql_close($cn);
$objSmarty->display('html.tmpl');

---html----
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>

<hr />
{section name=customer loop=$id}
<p>
name: {$name[customer]}<br />
id: {$id[customer]}<br />
cell: {$tel[customer]}<br />
e-mail: {$mail[customer]}
</p>
{/section}


</body>
</html>

Smarty MySQLとの連携方法1

この方法のメリットはデータベースのカラム名はテンプレートのみに記述するのでプログラム管理が簡単になる。ただし、後から紹介する方法の方が速度は若干速い。

--PHP部分--
require_once( 'MySmarty.class.php');
$objSmarty =& new MySmarty;
if (!($cn = mysql_connect("localhost", "hoge", "hoge"))) {
die;
}
if (!(mysql_select_db("test"))) {
die;
}
$sql = "select * from address";
if (!($rs = mysql_query($sql))) {
die;
}
while ($item = mysql_fetch_array($rs)) {
$objSmarty->append('contacts',$item);
}
mysql_close($cn);
$objSmarty->display('hoge1.tmpl');

---hoge1.tmpl---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>テンプレート</title>
</head>
<body>

<hr />
{section name=customer loop=$contacts}
<p>
name: {$contacts[customer].name}
id: {$contacts[customer].id}
cell: {$contacts[customer].tel}
e-mail: {$contacts[customer].email}
</p>
{/section}


</body>
</html>