A
A
AVXNWNK2016-10-11 18:26:38
PHP
AVXNWNK, 2016-10-11 18:26:38

How to organize MySQL query result in PHP (OOP)?

Hello. In general, I am writing a script for adding/editing/deleting/displaying results from a MySQL database. I am writing it in PHP 7 using OOP.
I ran into a problem - results are not displayed from the database, writes an error for incorrect Foreach arguments and insufficient arguments in the MySQL_fetch_array function. Please help me to solve this problem. The addition works.
Sources:
fetch.php

<?php

## Fetcher engine 0.9.444

class contentFetch {
  public static $element, $info;
  private static $row, $result;
  public static function addContent() {
    $connectMysqli = new connectMysqli();
    self::$element = $_REQUEST['element'];
    if (ini_get("magic_quotes_gpc"))
      $element = array_map('stripslashes', $element);
    $connectMysqli->mysqli_qw('
      INSERT INTO '.TABLENAME.' SET name=?, text=?',
      $element['name'], $element['text']
    ) or die(mysql_error());
    Header("Location: {$_SERVER['SCRIPT_NAME']}?".time());
    exit();
  }

  public static function deleteContent() {
      mysqli_qw('
        DELETE FROM '.TABLENAME.' WHERE id=?
      ', $delete) or die(mysql_error());
  }

  public static function resultContent() {
    $connectMysqli = new connectMysqli();
    $connectMysqli->mysqli_qw('
      SELECT *, UNIX_TIMESTAMP(stamp) AS stamp
      FROM '.TABLENAME.'
      ORDER BY stamp DESC
    ') or die(mysql_error());
    for (self::$info = array(); self::$row = mysqli_fetch_array(self::$result); self::$info[] = self::$row);
  }
}

?>

mysqli_qw.php
<?php
class connectMysqli {
  private static $connect;
  public function __construct() {
    self::$connect = mysqli_connect("localhost", "name", "passwd", "db_name");
  }
  /*public function __destruct() {
    mysqli_close(self::$connect);
  }*/
  public static function mysqli_qw() {
    $args = func_get_args();
    $conn = NULL;
    if(is_resource($args[0])) $conn = array_shift($args);
    $query = call_user_func_array(array("self", "mysqli_make_qw"), $args);
    return $conn!==null? mysqli_query($query, $conn) : mysqli_query(self::$connect, $query);
  }
  public static function mysqli_make_qw() {
    $args = func_get_args();
    $tmpl =& $args[0];
    $tmpl = str_replace("%", "%%", $tmpl);
    $tmpl = str_replace("?", "%s", $tmpl);
    foreach ($args as $i=>$v) {
      if(!$i) continue;
      if (is_int($v)) continue;
      $args[$i] = "'".mysqli_real_escape_string(self::$connect, $v)."'";
    }
    for ($i=$c=count($args)-1; $i<$c+20; $i++)
      $args[$i+1] = "UNKNOWN_PLACEHOLDER_$i";
    return call_user_func_array("sprintf", $args);
  }
}
?>

plugin_list.php
<?php

## Plugin list 0.9.444

foreach (contentFetch::$info as contentFetch::$element) {
  require_once("./plugin_list.html");
}

?>

plugin_list.html
<!-- Plugin list 0.9.444 -->

<b>
  <?=date("d.m.Y", contentFetch::$element['stamp'])?>
  <?=htmlspecialchars(contentFetch::$element['name'])?>
</b>
<a href="<?=$_SERVER['SCRIPT_NAME']?>?delete=<?=contentFetch::$element['id']?>">
  [delete]</a>
<blockquote>
  <?=nl2br(htmlspecialchars(contentFetch::$element['text']))?>
</blockquote>

index.php
<?php

## indexpage

define("TABLENAME", "subd");
require_once("./mysqli_qw.php");
$connectMysqli = new connectMysqli();
$connectMysqli->mysqli_qw('
  CREATE TABLE IF NOT EXISTS '.TABLENAME.' (
    id    INT AUTO_INCREMENT PRIMARY KEY,
    stamp TIMESTAMP,
    name  VARCHAR(60),
    text  TEXT
  )
');
require_once("./fetch.php");
$contentFetch = new contentFetch();
require_once("./plugin_fetch.html");
require_once("./plugin_list.php");
require_once("./plugin_list.html");
if (@$_REQUEST['doAdd']) {
  $contentFetch->addContent();
}
if ($delete = @$_REQUEST['delete']) {
  $contentFetch->deleteContent();
}
$contentFetch->resultContent();
?>

Thank you in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question