Home>

I want to create a bulletin board with PHP and mySQL.
Even if DB connection is possible, information in the DB cannot be acquired and displayed.
How can I edit the bulletin board thread list?

<? php
class sqlClass {

  private $stmt = [];
  private $default = null;
  private $name;
// getter
  public function get_name ($name) {
    return $this->name;
  }
// setter
  public function set_name ($name) {
    $this->name = $name;
  }
// array getter
  public function get ($key, $default = null) {
    if (array_key_exist ($key, $this->stmt)) {
      return $this->stmt [$key];
    }
    return $default;
  }
// array setter
  public function set ($key, $value) {
    $this->stmt [$key] = $value;
  }

// PDO generation
  public function dbconnect () {
    try {
    $db = "mysql";
    $host = "localhost";
    $dbname = "bb_db";
    $user = "bb_user";
    $pass = "bbpass";
    $charset = "";
    $dbh = new PDO ("mysql: host = localhost;dbname = bb_db", bb_user, bbpass);
    echo "connection ok";
    } catch (Exception $e) {
    }
  }
// SQL for title list
  public function selTitleList () {
    try {
      $this->dbconnect ();
      echo "OK";
      $sql = "SELECT titleList.idTitle, titleList.title, COUNT (thread.idName) AS cntRes FROM titleList, thread WHERE titleList.idTitle = thread.idTitle group by titleList.idTitle";
      $stmt = $dbh->query ("$sql");
      $this->set ("$stmt");
    } catch (PDOException $e) {}
  }
  // TODO: (I'll do it later) insert-select
// SQL for thread creation
  public function newThread () {
    try {
      $this->dbconnect ($title, $name, $trip, $email, $date, $pass, $article);
      $trip = $this.trip ();
      echo "OK";
      $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";
      $stmt = $dbh->prepare ($sql);
      $stmt->execute ();
      $data = array ();
      $count = $stmt->rowCount ();
      while ($row = $stmt->fetch (PDO :: FETCH_ASSOC)) {
        $data [] = $row;
      }
      if (array_key_exist ($data, $this->stmt)) {
        return $this->stmt [$data];
      }
      return $default;
    } catch (PDOException $e) {
      echo $e->getMessage ();
      echo "NG";
    }
  }
// In-thread display
  public function selThread () {
    try {
      $dbh = new $this->dbconnect ();
      echo "OK";
      $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 ");
      $this->set ('$spl');
    } catch (Exception $e) {
    }
  }

// Create trip
  public function trip () {
    $tripkey = '#istrip';// Character string for password (with #)
    $tripkey = substr ($tripkey, 1);
    $salt = substr ($tripkey. 'H.', 1, 2);
    $salt = preg_replace ('/[^\.- z] /', '.', $salt);
    $salt = strtr ($salt, ':;<=>? @ [\\] ^ _ `', 'ABCDEFGabcdef');
    $trip = crypt ($tripkey, $salt);
    $trip = substr ($trip, -10);
    $trip = '◆'. $trip;
    echo $trip;
  }
}
?>


It is mySQL configuration
titleList
--int idTitle (primary key)
--String title

thread
--int idTitle
--int idName (primary key by combining idTitle and idName)
--String article
--String name
--String trip
--Date date
--String password
--String email

<? php
  require_once ('functions.php');
  $data = new sqlClass ();
  $data->selThread ();
  $data->get ('$sql');
?>
<html>
<head>
<title>Thread</title>
</head>
<body>
  <b><? php echo $data ['thread.idTitle']. ":". $data ['titleList.title'];?></b>
  <? php foreach ($data as $row):?>
    <hr>
    <? php echo $row ['thread.idName']. ":". $row ['thread.name']. $row ['thread.trip']. "/". $row ['thread.date ']. "<br>". $row [' thread.article ']?>
  <? php endforeach;?>
  <hr>
<p><a href = "res_new.php? id =<? php echo $id;?>">Write</a></p>
</body>
</html>

I want to manage with sqlClass as much as possible.
In conclusion, nothing is done, so progress is not good.


Addition of 08-17
I think that if you try to prepare multiple files from the beginning, you will not understand any more
I edited the title list to be displayed in one file.
I get an error.
Can you tell me what caused it?

<? php
ini_set ('display_errors', 1);
class tesCls {
  // PDO generation
  function dbconnect () {
    try {
      $db = "mysql";
      $host = "localhost";
      $dbname = "bb_db";
      $user = "bb_user";
      $pass = "bbpass";
      $charset = "";
      $dbh = new PDO ($db. ': host ='. $host. ';dbname ='. $dbname. ','. $user. ','. $pass);
      echo "connection ok";
    } catch (Exception $e) {
    }
  }
  // SQL for title list
  function selTitleList () {
    try {
      $this->dbconnect ();
      $dbh->setAttribute (PDO :: ATTR_ERRMODE, PDO :: ERRMODE_EXCEPTION);
      $sql = "SELECT titleList.idTitle, titleList.title, COUNT (thread.idName) AS cntRes FROM titleList, thread WHERE titleList.idTitle = thread.idTitle group by titleList.idTitle";
      $stmt = $dbh->prepare ($sql);
      $stmt->execute ();
      $data = array ();
      $count = $stmt->rowCount ();
      while ($row = $stmt->fetch (PDO :: FETCH_ASSOC)) {
        $data [] = $row;
      }
      var_dump ($count);
      var_dump ($data);
    } catch (Exception $e) {
    }
  }

  // Create trip
  function trip () {
    $tripkey = '#istrip';// Character string for password (with #)
    $tripkey = substr ($tripkey, 1);
    $salt = substr ($tripkey. 'H.', 1, 2);
    $salt = preg_replace ('/[^\.- z] /', '.', $salt);
    $salt = strtr ($salt, ':;<=>? @ [\\] ^ _ `', 'ABCDEFGabcdef');
    $trip = crypt ($tripkey, $salt);
    $trip = substr ($trip, -10);
    $trip = '◆'. $trip;
    echo $trip;
  }
  / *
  int idTitle //
  String title
  // int cntArticle // ++
  int idTitle //
  String article
  int idName
  String name
  String trip
  Date date
  String password
  String email
  % Y /% m /% e% k:% i:% s% f
  * /
}
$test = new tesCls ();
$test->selTitleList ();
echo "<table>". \ n. "
<tr>
  <th>id</th>
  <th>title</th>
  <th>cnt</th>
</tr>";
foreach ($data as $row) {
  echo "<tr>";
  echo "<td>". $row ['idTitle']. "</td>";
  echo "<td>". $row ['title']. "</td>";
  echo "<td>". $row ['cntRes']. "</td>";
  echo "<tr>";
}
echo "</table";
 ?>
Notice: Undefined variable: dbh in /Applications/MAMP/htdocs/bb/local-bb/test.php on line 25
Fatal 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
  • Answer # 1

    It's strange overall, so it feels like I'm asked what is wrong.
    Please ask questions after squashing at least typo.
    It is better to identify the problem location by preparing a debugging environment and checking variable transitions.

  • Answer # 2

    How far have you been debugging?
    Whether the expected value is in the variable at the expected location or output with var_dump (),
    Turn on the error display or check the error log to check for errors.

    When something goes wrong, the problem is solved based on the premise and hypothesis that "this is right".
    First, find out how far you go as expected and identify the cause.

    Tell me a hint.
    Even if a variable is entered as it is within single quotation marks, the variable will not be expanded and will only be interpreted as such a string. If you enclose it in double quotes, it will be expanded, but if there are only variables, there is no point in enclosing it.

  • Answer # 3

    Title list has been acquired.
    The problem is

    // Title list SQL
      function selTitleList () {
        try {
          $this->dbconnect ();



          $dbh = $this->dbconnect ();
    Was not described, and the problem was that the connection could not be passed afterwards.

    Although it is a part of the source of the subsequent list display

    $stmt = $dbh->query ("
            SELECT
              titleList.idTitle,
              titleList.title,
              COUNT (thread.idName) AS cntRes
            FROM titleList, thread
            WHERE
              titleList.idTitle = thread.idTitle
            group by titleList.idTitle
            ");
          $stmt->execute ();
          $users = $stmt->fetchAll (PDO :: FETCH_ASSOC);
          foreach ($users as $value) {


    It was possible to display by describing in this way.

    Thank you very much.

Related articles