Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 05/03/2023 in all areas

  1. What do you mean, "save a button"?
    1 point
  2. Then perhaps you should read the manual section entitled How to read a function definition (prototype). And the section entitled Callbacks / Callables. When you're new to something, you need to spend time on reading about the fundamentals. That way when you look at the detailed references you understand them. There's a lot to the PHP manual besides the function references for you to read and learn from.
    1 point
  3. $var is defined as a function parameter. I suspect that what you're trying to ask is what value it holds since you don't see it getting called anywhere. It'll be called by array_filter, and if you read the documentation for that function it tells you what the value of $var will be.
    1 point
  4. Also don't do this: SELECT id,domain,word,phrase from $tbl WHERE $col_1 = ? Because now it's easy for a hacker to do something like: ?col_1=1;DELETE FROM users WHERE id&input_1=1 Instead do something like: /** * @param array{col_1: string} $fields * @param string $operator Possible values AND, OR * @param int $limit Possible value between 0 and 100, if invalid defaults to 100 * @param int $offset * * @return array{items: array, total_items: int} */ function page_search(array $fields, string $operator, int $limit, int $offset = 0): array { $where = $values = []; $operator = in_array($operator, ['OR', 'AND']) ? $operator : 'AND'; $limit = 0 < $limit && $limit < 100 ? $limit : 100; $offset = 0 <= $offset ? $offset : 0; foreach ($fields as $field => $term) { switch ($field) { case 'col_1': $where[] = 'col_1 = ?'; $values[] = $term; break; // other fields you want to allow to search on } $result = [ 'items' => [], 'total_items' => 0, ]; if ([] === $where) { return $result; } $result['items'] = db_fetch_all('SELECT * FROM some_table WHERE ' . implode($operator, $where) . " LIMIT $offset, $limit", $values); if (count($result['items'])) { // only execute a count() query if we have a result $result['total_items'] = db_fetch_column('SELECT count(*) FROM some_table WHERE ' . implode($operator, $where), $values); } return $result; } By dividing your program into little pieces you reduce the cognitive load necessary to work on pieces of your application or to find and fix bugs. The same goes for the database. I see you use mysqli_connect in the script with the values hardcoded. Instead you should create a file that holds your configuration values: // bootstrap.php define('DB_HOST', 'localhost'); define('DB_USER', 'the_username'); define('DB_PASS', 'the_password'); define('DB_NAME', 'the_database'); define('DB_PORT', 3306); define('DB_CHARSET', 'utf8mb4'); define('EMERGENCY_EMAIL', 'some@email.com'); // in case of fatal errors // other configuration values Then in your database functions file: function db_connect() { static $connection; if (null === $connection) { $connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME, DB_PORT); if ($connection) { mysqli_set_charset($connection, DB_CHARSET); } } return $connection; } All of this creates building blocks you can use to build upon further. The next step would be to create a db_fetch_all function that uses db_connect to get the active DB connection etc...
    1 point
  5. You really don't need if statement or try/catch blocks and that is something a human pointed me out on another forum. I believe he's also on this forum as well. For example I have this function(method in OOP) $sql = 'SELECT * FROM gallery WHERE page =:page AND category =:category ORDER BY id DESC, date_added DESC LIMIT :perPage OFFSET :blogOffset'; $stmt = $this->pdo->prepare($sql); // Prepare the query: $stmt->execute(['page' => $page, 'perPage' => $perPage, 'category' => $category, 'blogOffset' => $offset]); // Execute the query with the supplied data: return $stmt->fetchAll(PDO::FETCH_ASSOC); No try/catch block, if statement as any errors are caught by exception. Something to read up on is this // Register the exception handler method set_exception_handler([$errorHandler, 'handleException']); here's a good link on it https://www.php.net/manual/en/function.set-exception-handler.php For debugging ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); and a good link in using PDO as in my opinion it is easier to implement and more versatile - https://phpdelusions.net/pdo Even the website writes " Although there are several error handling modes in PDO, the only proper one is PDO::ERRMODE_EXCEPTION. So, one ought to always set it this way, either by adding this line after creation of PDO instance, $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); or as a connection option, as demonstrated in the example above. And this is all you need for the basic error reporting"
    1 point
This leaderboard is set to New York/GMT-04:00
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.