SQLiteで
Call to a member function on a non-objectのエラーが再び出て、困った。
初心者によくあるSQL文の記述間違いが主であると複数のサイトで言われていたので、
SQL文を見返すも原因が見つからず。
やっと見つかった原因は単に、SQLiteのファイルへの接続が古いファイルに向いていたことだった。
新しいファイルの方へと接続し直したらOKに、というオチだった。。
SQLiteでのPDO、bindParamの書き方が、に載っていて、とても簡単だったので、引用メモです。
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
// 行を挿入します
$name = 'one';
$value = 1;
$stmt->execute();
// パラメータを変更し、別の行を挿入します
$name = 'two';
$value = 2;
$stmt->execute();
?>
(11) How do I add or delete columns from an existing table in SQLite.
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
$test="hoge,huga,foo,baa";のような場合、
$ary=array();
$ary=explode(",",$test);
print_r($ary);
output_buffering = On
output_handler = mb_output_handler
default_charset = "Shift_JIS"
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = auto
mbstring.http_output = SJIS
mbstring.detect_order = SJIS,EUC-JP,JIS,UTF-8,ASCII
mbstring.substitute_character = "none"
mbstring.encoding_translation = On
select *,count(*) as count,hoge,fuga from a left join b on hoge.id=fuga.id;といったように、二つのテーブルをleft joinするときも、
$text="<tr><td><img>";
$pattern="(<t(d|r|able).*?>|<\/t(d|r|able).*?>)";
$result=preg_replace("/$pattern/","",$text);
echo $result;
$pattern="(<t(d|r|able).*?>|<\/t(d|r|able).*?>| )";としてみました。)
$text="太郎<a@example.com>, 花子<b@example.com>,";
$text.="次郎 <c@example.com>,良子 <d@example.com>";
$result=preg_replace("/>.*?</",",",$text);
echo $result;
LOAD DATA LOCAL INFILE '/path/KEN_ALL.CSV'
INTO TABLE `sample_zip`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LOAD DATA LOCAL INFILE 'C://xampp/mysql/KEN_ALL.CSV'
INTO TABLE `zip`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';
function url($text){
$regex="s?https?:\/\/[a-zA-Z0-9;\/?:@&=\+$,\-_\.!~*'\(\)%#]+";
if(strlen($text)>60){
$text2=mb_substr($text,0,60);
$text2=sprintf("%s...",$text2);
}else{
$text2=$text;
}
echo preg_replace("/$regex/","<a href=\"\\0\"
target=\"_blank\">$text2</a>",$text);
}
function highlight($content,$word){
if(is_array($word)){
$num=count($word);
$lighted[0]=$content;
for($j=0;$j<$num;$j++){
$k=$j+1; //$kを$jの先に進めておく。
//一単語ずつハイライトしていったものを格納。
$lighted[$k]=str_replace($word[$j],
"<span class='highlight'>{$word[$j]}</span>",$lighted[$j]);
}
return $lighted[$num];
}
else{
$lighted=str_replace($word,"<span class='highlight'>{$word}</span>"
,$content);
return $lighted;
}
}
<input type="radio" id="id1" name="name1" value="1"> <label for="id1">radio1</LABEL>
<link rel="stylesheet" type="text/css" media="screen" href="style.php">
みたいに読み込ませる、と。
phpマニュアルに、printfで%s,%dなどに代入する引数の順番を変えたり、何度も代入したりできる便利な用法が載っていますが、エラーが出てうまく使えませんでした。 $format = 'The %2$s contains %1$d monkeys';printf($format, $num, $location);結局問題は、""ダブルクオーテーションを使っていたので、その中で$記号をエスケープしていなかったことでした。%1\$s,%2\$dなどとしてやると、無事順番入れ替えや同じ引数を複数回使ったりできました。よかった。 ■追記:%1\$d と書くべきを、%\$1dと言う風に$を数字の前に書いてエラーも出てました。 書き順注意。 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>dechexの応用による、色彩テーブル</title>
<style type="text/css">
table{border-collapse:collapse;}
td{width:60px;text-align:center;padding:0;}
</style>
</head>
<body>
<table>
<?php
//純色で、数値を上げていく。
for($i=0;$i<256;$i=$i+16){
$R0=sprintf ("<td bgcolor='#0%s0000'>#0%s0000</td>",dechex($i),dechex($i));
$G0=sprintf ("<td bgcolor='#000%s00'>#000%s00</td>",dechex($i),dechex($i));
$B0=sprintf ("<td bgcolor='#00000%s'>#00000%s</td>",dechex($i),dechex($i));
$R =sprintf ("<td bgcolor='#%s0000'>#%s0000</td>" ,dechex($i),dechex($i));
$G =sprintf ("<td bgcolor='#00%s00'>#00%s00</td>" ,dechex($i),dechex($i));
$B =sprintf ("<td bgcolor='#0000%s'>#0000%s</td>" ,dechex($i),dechex($i));
if($i<16){echo "<tr>".$R0.$G0.$B0."</tr>";}
else{echo "<tr>".$R,$G,$B."</tr>";}
}
//混色で、二色目の数値を上げていく。
for($i=0;$i<256;$i=$i+16){
$RG0=sprintf ("<td bgcolor='#ff0%s00'>#ff0%s00</td>",dechex($i),dechex($i));
$GB0=sprintf ("<td bgcolor='#00ff0%s'>#00ff0%s</td>",dechex($i),dechex($i));
$RB0=sprintf ("<td bgcolor='#0%s00ff'>#0%s00ff</td>",dechex($i),dechex($i));
$RG =sprintf ("<td bgcolor='#ff%s00'>#ff%s00</td>",dechex($i),dechex($i));
$GB =sprintf ("<td bgcolor='#00ff%s'>#00ff%s</td>",dechex($i),dechex($i));
$RB =sprintf ("<td bgcolor='#%s00ff'>#%s00ff</td>",dechex($i),dechex($i));
if($i<16){echo "<tr>".$RG0.$GB0.$RB0."</tr>";}
else{echo "<tr>".$RG.$GB.$RB."</tr>";}
}
?>
</table>
</body>
</html><?php
$img = imagecreatetruecolor(400, 400);
$bg=imagecolorallocate($img,0,0,0);
$r2=150;
$s=1;$v=1;
function toH($a){
$result=round((round($a,3)*255));
return $result;
}
for($h=0;$h<360;$h++){
$y0=$r2*(sin(deg2rad($h)));
$x0=$r2*(cos(deg2rad($h)));
$y=$y0+200;
$x=$x0+200;
$hi = ( $h / 60 ) % 6;
$f = ( $h / 60 ) - $hi;
$p = $v * ( 1 - $s );
$q = $v * ( 1 - $f * $s );
$t = $v * ( 1 - ( 1 - $f ) * $s );
if($hi == 0){$r = $v;$g = $t;$b = $p;}
elseif($hi == 1){$r = $q;$g = $v;$b = $p;}
elseif($hi == 2){$r = $p;$g = $v;$b = $t;}
elseif($hi == 3){$r = $p;$g = $q;$b = $v;}
elseif($hi == 4){$r = $t;$g = $p;$b = $v;}
elseif($hi == 5){$r = $v;$g = $p;$b = $q;}
//else{$r = $v;$g = $v;$b = $v;}
$col=imagecolorallocate($img,toH($r),toH($g),toH($b));
$col0=imagecolorallocate($img,0,0,0);
imagefilledarc($img,200,200,300,300,$h,$h+1,$col
,IMG_ARC_PIE);
imagefilledellipse($img,200,200,200,200,$col0);
}
header("Content-Type: image/png");
imagepng($img);
?><?php
$img = imagecreatetruecolor(400, 400);
$bg=imagecolorallocate($img,0,0,0);
$r2=150;
$s=1;$v=1;
function toH($a){
$result=round((round($a,3)*255));
return $result;
}
for($h=0;$h<360;$h++){
$y0=$r2*(sin(deg2rad($h)));
$x0=$r2*(cos(deg2rad($h)));
$y=$y0+200;
$x=$x0+200;
$hi = ( $h / 60 ) % 6;
$f = ( $h / 60 ) - $hi;
$p = $v * ( 1 - $s );
$q = $v * ( 1 - $f * $s );
$t = $v * ( 1 - ( 1 - $f ) * $s );
if($hi == 0){$r = $v;$g = $t;$b = $p;}
elseif($hi == 1){$r = $q;$g = $v;$b = $p;}
elseif($hi == 2){$r = $p;$g = $v;$b = $t;}
elseif($hi == 3){$r = $p;$g = $q;$b = $v;}
elseif($hi == 4){$r = $t;$g = $p;$b = $v;}
elseif($hi == 5){$r = $v;$g = $p;$b = $q;}
//else{$r = $v;$g = $v;$b = $v;}
$col=imagecolorallocate($img,toH($r),toH($g),toH($b));
imagefilledellipse($img, $x, $y, 50, 50, $col);
}
header("Content-Type: image/png");
imagepng($img);
?>
<?php
echo "<table cellpadding='1' border='1'>";
echo "<tr><th>H</th><th>R</th><th>G</th><th>B</th></tr>";
for($h=0;$h<360;$h++){
$s=1;$v=1;
$hi = ( $h / 60 ) % 6;
$f = ( $h / 60 ) - $hi;
$p = $v * ( 1 - $s );
$q = $v * ( 1 - $f * $s );
$t = $v * ( 1 - ( 1 - $f ) * $s );
if($hi == 0){
$r = $v;$g = $t;$b = $p;
}
elseif($hi == 1){
$r = $q;$g = $v;$b = $p;
}
elseif($hi == 2){
$r = $p;$g = $v;$b = $t;
}
elseif($hi == 3){
$r = $p;$g = $q;$b = $v;
}
elseif($hi == 4){
$r = $t;$g = $p;$b = $v;
}
elseif($hi == 5){
$r = $v;$g = $p;$b = $q;
}
else{
$r = $v;$g = $v;$b = $v;
}
echo "<tr><th>".$h."</th><td>".toHex($r)
."</td><td>",toHex($g),"</td><td>"
.toHex($b)."</td></td>";
}
echo "</table>";
function toHex($a){
$result=round((round($a,3)*255));
return $result;
}
?>参考にさせていただいたページ。<?php
header('Content-type: image/png');
$image=imagecreate(400,400);
imagecolorallocate($image,200,200,200);
$color=imagecolorallocate($image,0,0,0);
for($i=0;$i<361;$i++){
imagesetpixel($image,(200+(100*round(cos(deg2rad($i)),3))),(200+(100*round(sin(deg2rad($i)),3))),$color);
}
imagepng($image);
imagedestroy($image);
echo "<table border='1' cellspacing='0'>";
echo "<thead bgcolor='orange'><tr><th>角度</th><td>cos</td><td>sin</td></tr></thead>";
for($i=0;$i<361;$i=$i+30){
echo "<tr><th align='right'>{$i}</th><td>".round(cos(deg2rad($i)),3)."</td><td>".round(sin(deg2rad($i)),3)."</td></tr>";
}
echo "</table>";
echo deg2rad(45)."<br />"; echo deg2rad(180)."<br />"; echo deg2rad(270)."<br />";の値はそれぞれ、
半径の長さに等しい弧に対する中心角の大きさを一ラジアンという文を見つけましたが、そういうことなんですね。
echo M_PI."<br />"; echo (M_PI/8)."<br />"; echo M_PI_4."<br />"; echo M_PI_2;の結果は、
preg_replace("/(\(|().*(\)|))/","変えたよ",$str)
要するに、かっこの半角と全角の両方を|で認識させるようにして、半角かっこはもちろんバックスラッシュでエスケープして、それを半角かっこでくくってグループ化し、そのあとに閉じかっこも開きかっこと同様にグループ化して指定して、その間に.*を入れてカッコ内にどんな文字があってもOKになった、と。preg_replace("/(\(|().{5,30}(\)|))/","OK",$str);
みたいに、間にある文字数をだいたい指定してやれば、まあまあいけそうです。ちょっとアナログちっく。preg_replace("/(\(|().{5,10}(\)|))/u","OK",$str);
<html><head><style type="text/css"><!--table {font-size:0.8em;}-->
</style><body >
<?php
function deg2sixteen($deg){//10~15までをa~fに変換
switch($deg){case 10:$deg="a";break;case 11:$deg="b";break;case 12:$deg="c";break;case 13:$deg="d";break;case 14:$deg="e";break;case 15:$deg="f";break;}//i switch end
return $deg;}
echo "<table border='0' cellspacing='0' cellpadding='0'><tr>";//sell-paddingの値を0にする必要があった。
for($n=0;$n<4;$n++){//RGBそれぞれ2色ずつのテーブルをつくるループ
echo"<td><table border='0' cellspacing='0' cellpadding='0'>";
for($y=0;$y<16;$y++){//第一のカラーを0~15まで推移させる。
echo"<tr>\n";
if($n<2){$y2=15-$y;$yc=deg2sixteen($y2);}//この二行で上下を逆に。
else{$yc=deg2sixteen($y);}
for($x=0;$x<16;$x++){//第二のカラーを0~15まで推移させる。
if($n%2==0){$xa=15-$x;$xc=deg2sixteen($xa);}//この二行で左右を逆に。
else{$xc=deg2sixteen($x);}
$y2=$yc."0";$x2=$xc."0";$z2="00";//ここで二ケタに変換
switch($n){//ここでRGBから二色を選ぶ振り分け。
case 0:echo"<td bgcolor='#".$z2.$y2.$x2."'>#0".$yc.$xc."</td>\n";break;
case 1:echo"<td bgcolor='#".$x2.$y2.$z2."'>#0".$yc.$xc."</td>\n";break;
case 2:echo"<td bgcolor='#".$y2.$z2.$x2."'>#".$yc."0".$xc."</td>\n";break;
case 3:echo"<td bgcolor='#".$y2.$z2.$x2."'>#".$yc."0".$xc."</td>\n";break;
}//$n switch end
}//j for end
echo"</tr>\n";
}//i for end
echo"</table></td>";
if($n%2==1){echo"</tr><tr>";}
}//for n end
echo"</tr></table>";
echo "<table border='0' cellspacing='0' cellpadding='0'><tr>";//sell-paddingの値を0にする必要があった。
for($n=0;$n<4;$n++){//RGBそれぞれ2色ずつのテーブルをつくるループ
echo"<td><table border='0' cellspacing='0' cellpadding='0'>";
for($y=0;$y<16;$y++){//第一のカラーを0~15まで推移させる。
echo"<tr>\n";
if($n<2){$y2=15-$y;$yc=deg2sixteen($y2);}//この二行で上下を逆に。
else{$yc=deg2sixteen($y);}
for($x=0;$x<16;$x++){//第二のカラーを0~15まで推移させる。
if($n%2==0){$xa=15-$x;$xc=deg2sixteen($xa);}//この二行で左右を逆に。
else{$xc=deg2sixteen($x);}
$y2=$yc."0";$x2=$xc."0";$z2="00";//ここで二ケタに変換
switch($n){//ここでRGBから二色を選ぶ振り分け。
case 0:echo"<td bgcolor='#".$y2.$z2.$x2."'>#".$yc."0".$xc."</td>\n";break;
case 1:echo"<td bgcolor='#".$y2.$z2.$x2."'>#".$yc."0".$xc."</td>\n";break;
case 2:echo"<td bgcolor='#".$z2.$y2.$x2."'>#0".$yc.$xc."</td>\n";break;
case 3:echo"<td bgcolor='#".$z2.$y2.$x2."'>#0".$yc.$xc."</td>\n";break;
}//$n switch end
}//j for end
echo"</tr>\n";
}//i for end
echo"</table></td>";
if($n%2==1){echo"</tr><tr>";}
}//for n end
echo"</tr></table>";
?>
</body>
</html>
<?php
function deg2sixteen($deg){//10~15までをa~fに変換
switch($deg){
case 10:$deg="a";break;
case 11:$deg="b";break;
case 12:$deg="c";break;
case 13:$deg="d";break;
case 14:$deg="e";break;
case 15:$deg="f";break;
}//i switch end
return $deg;
}
for($n=0;$n<3;$n++){//RGBそれぞれに色ずつのテーブルをつくるループ
switch($n){
case 0:echo"赤Rと緑Gの混色図";break;
case 1:echo"緑Gと青Bの混色図";break;
case 2:echo"赤Rと青Bの混色図";break;
}
echo "<table border='0' cellspacing='0'>";
echo "<tr>";
for($i=0;$i<16;$i++){//第一のカラーを0~15まで推移させる。
$ic=$i;
$ic=deg2sixteen($ic);
echo"<tr>";
for($j=0;$j<16;$j++){//第二のカラーを0~15まで推移させる。
$jc=$j;
$jc=deg2sixteen($jc);
$ic2=$ic.$ic;$jc2=$jc.$jc;$zero="00";//ここで二ケタに変換
switch($n){//ここでRGBから二色を選ぶ振り分け。
case 0:echo"<td bgcolor='#".$ic2.$jc2.$zero."'>#".$ic.$jc."0</td>";break;
case 1:echo"<td bgcolor='#".$zero.$ic2.$jc2."'>#0".$ic.$jc."</td>";break;
case 2:echo"<td bgcolor='#".$ic2.$zero.$jc2."'>#".$ic."0".$jc."</td>";break;
}//$n switch end
}//j for end
echo"</tr>";
}//i for end
echo"</table><br>";
}//for n end
?> <?php
$r=100;
$img = imagecreate(500, 500);
$bg=imagecolorallocate($img,0,0,0);
for($i=0;$i<12;$i++){
$rad=deg2rad($i*30-90);
$c=$i*20+15;
$c2=255-$c;
$y=$r*sin($rad);
$x=$r*cos($rad);
$y2=$y+200;
$x2=$x+200;
$col=imagecolorallocate($img, $c2,$c, 0);
imagefilledellipse($img, $x2, $y2, 20, 20, $col);
}
header("Content-Type: image/png");
imagepng($img);
?>