PHPとmySQLで掲示板を作成したいのですが、
DB接続はできても、DB内の情報を取得し表示できません。
掲示板のスレッド一覧を表示するにはどう編集すればいいでしょうか?
php
1<?php 2class sqlClass { 3 4 5 private $stmt = []; 6 private $default = null; 7 8 private $name; 9 10// getter 11 public function get_name($name){ 12 return $this->name; 13 } 14 15// setter 16 public function set_name($name){ 17 $this->name = $name; 18 } 19 20// 配列getter 21 public function get($key, $default = null){ 22 if(array_key_exist($key, $this->stmt)){ 23 return $this->stmt[$key]; 24 } 25 return $default; 26 } 27 28// 配列setter 29 public function set($key,$value){ 30 $this->stmt[$key] = $value; 31 } 32 33 34// PDO生成 35 public function dbconnect(){ 36 try { 37 $db = "mysql"; 38 $host = "localhost"; 39 $dbname = "bb_db"; 40 $user = "bb_user"; 41 $pass = "bbpass"; 42 $charset = ""; 43 $dbh = new PDO("mysql:host=localhost;dbname=bb_db",bb_user,bbpass); 44 echo "接続ok"; 45 } catch (Exception $e) { 46 } 47 } 48 49// タイトル一覧用SQL 50 public function selTitleList(){ 51 try { 52 $this->dbconnect(); 53 echo "OK"; 54 $sql = "SELECT titleList.idTitle, titleList.title, COUNT(thread.idName) AS cntRes FROM titleList, thread WHERE titleList.idTitle = thread.idTitle group by titleList.idTitle"; 55 56 $stmt = $dbh->query("$sql"); 57 $this->set("$stmt"); 58 }catch(PDOException $e) {} 59 } 60 61 // TODO:(後で手をつける) insert-select 62// スレッド作成用SQL 63 public function newThread(){ 64 try { 65 $this->dbconnect($title, $name, $trip, $email, $date, $pass, $article); 66 $trip = $this.trip(); 67 echo "OK"; 68 69 $sql = "INSERT INTO thread (idTitle, idName, name, trip, email, date, password, article) VALUES (" .$idTitle .",". $idName .",". $name .",". $trip .",". $email .",". $date .",". $pass .",". $article .") WHERE thread.idTitle = titleList.idTitle"; 70 $stmt = $dbh->prepare($sql); 71 $stmt->execute(); 72 $data = array(); 73 $count = $stmt->rowCount(); 74 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 75 $data[] = $row; 76 } 77 if(array_key_exist($data, $this->stmt)){ 78 return $this->stmt[$data]; 79 } 80 return $default; 81 } catch (PDOException $e) { 82 echo $e->getMessage(); 83 echo "NG"; 84 } 85 } 86 87// スレッド内表示 88 public function selThread(){ 89 try { 90 $dbh = new $this->dbconnect(); 91 echo "OK"; 92 $sql = $dbh->prepare("SELECT thread.idTitle, titleList.title, thread.idName, thread.name, thread.trip, thread.email, thread.date, thread.article FROM titleList, thread WHERE titleList.idTitle = thread.idTitle group by titleList.idTitle"); 93 $this->set('$spl'); 94 } catch (Exception $e) { 95 } 96 } 97 98 99// トリップ作成 100 public function trip(){ 101 $tripkey = '#istrip'; //パスワードとする文字列(# 付き) 102 $tripkey = substr($tripkey, 1); 103 104 $salt = substr($tripkey . 'H.', 1, 2); 105 $salt = preg_replace('/[^.-z]/', '.', $salt); 106 $salt = strtr($salt, ':;<=>?@[\]^_`', 'ABCDEFGabcdef'); 107 108 $trip = crypt($tripkey, $salt); 109 $trip = substr($trip, -10); 110 $trip = '◆' . $trip; 111 112 echo $trip; 113 } 114 115} 116?>
mySQLの構成ですが
titleList
--int idTitle(主キー)
--String title
thread
--int idTitle
--int idName (idTitleとidNameを合わせることで主キー)
--String article
--String name
--String trip
--Date date
--String password
--String email
php
1<?php 2 3 require_once('functions.php'); 4 $data = new sqlClass(); 5 $data->selThread(); 6 $data->get('$sql'); 7 8?> 9 10<html> 11<head> 12<title>スレッド</title> 13</head> 14<body> 15 <b><?php echo $data['thread.idTitle'] .":". $data['titleList.title']; ?></b> 16 <?php foreach ($data as $row): ?> 17 <hr> 18 <?php echo $row['thread.idName'] .":". $row['thread.name'] . $row['thread.trip'] ."/". $row['thread.date'] ."<br>". $row['thread.article'] ?> 19 <?php endforeach; ?> 20 <hr> 21 22<p><a href="res_new.php?id=<?php echo $id; ?>">書き込み</a></p> 23 24</body> 25</html>
なるべくsqlClassで管理するようにしたいとおもっています。
結論として何もできていないので進捗はよくないです。
08-17追記
最初から複数ファイルを用意しようとすると、余計にわからなくなると思い
1つのファイルでタイトル一覧を表示するように編集しましたが
エラーが出ます。
何が原因か教えていただけませんか?
php
1<?php 2ini_set( 'display_errors', 1 ); 3 4class tesCls { 5 6 // PDO生成 7 function dbconnect(){ 8 try { 9 $db = "mysql"; 10 $host = "localhost"; 11 $dbname = "bb_db"; 12 $user = "bb_user"; 13 $pass = "bbpass"; 14 $charset = ""; 15 $dbh = new PDO($db.':host='.$host.';dbname='.$dbname.','.$user.','.$pass); 16 echo "接続ok"; 17 } catch (Exception $e) { 18 } 19 } 20 21 // タイトル一覧用SQL 22 function selTitleList(){ 23 try { 24 $this->dbconnect(); 25 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 26 $sql = "SELECT titleList.idTitle, titleList.title, COUNT(thread.idName) AS cntRes FROM titleList, thread WHERE titleList.idTitle = thread.idTitle group by titleList.idTitle"; 27 28 $stmt = $dbh->prepare($sql); 29 $stmt->execute(); 30 $data = array(); 31 $count = $stmt->rowCount(); 32 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 33 $data[] = $row; 34 } 35 var_dump($count); 36 var_dump($data); 37 } catch (Exception $e) { 38 } 39 } 40 41 42 43 // トリップ作成 44 function trip(){ 45 $tripkey = '#istrip'; //パスワードとする文字列(# 付き) 46 $tripkey = substr($tripkey, 1); 47 48 $salt = substr($tripkey . 'H.', 1, 2); 49 $salt = preg_replace('/[^.-z]/', '.', $salt); 50 $salt = strtr($salt, ':;<=>?@[\]^_`', 'ABCDEFGabcdef'); 51 52 $trip = crypt($tripkey, $salt); 53 $trip = substr($trip, -10); 54 $trip = '◆' . $trip; 55 56 echo $trip; 57 } 58 59 /* 60 int idTitle // 61 String title 62 //int cntArticle //++ 63 64 int idTitle // 65 String article 66 int idName 67 String name 68 String trip 69 Date date 70 String password 71 String email 72 73 %Y/%m/%e %k:%i:%s%f 74 */ 75} 76 77$test = new tesCls(); 78 79$test->selTitleList(); 80echo "<table>".\n." 81<tr> 82 <th>id</th> 83 <th>title</th> 84 <th>cnt</th> 85</tr>"; 86foreach ($data as $row) { 87 echo "<tr>"; 88 echo "<td>".$row['idTitle']."</td>"; 89 echo "<td>".$row['title']."</td>"; 90 echo "<td>".$row['cntRes']."</td>"; 91 echo "<tr>"; 92} 93echo "</table"; 94 ?>
err
1 2Notice: Undefined variable: dbh in /Applications/MAMP/htdocs/bb/local-bb/test.php on line 25 3 4Fatal error: Uncaught Error: Call to a member function setAttribute() on null in /Applications/MAMP/htdocs/bb/local-bb/test.php:25 Stack trace: #0 /Applications/MAMP/htdocs/bb/local-bb/test.php(79): tesCls->selTitleList() #1 {main} thrown in /Applications/MAMP/htdocs/bb/local-bb/test.php on line 25
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。