Skip to main content
se añadieron 37 caracteres en el cuerpo
Origen Enlace
Roger Torné
  • 7.6k
  • 13
  • 20
  • 33

Buenos días actualmente poseo un error en mi pagina y no encuentro el motivo, quiero lograr que al crear un nuevo post este se encuentre de primer lugar que los anteriores por ser el mas nuevo, utilice: $sql = "SELECT * FROM $table ORDER BY id DESC";$sql = "SELECT * FROM $table ORDER BY id DESC"; pero me da el siguiente error

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title=? LIMIT 1' at line 1 in C:\xampp\htdocs\app\database\db.php:17 Stack trace: #0 C:\xampp\htdocs\app\database\db.php(17): mysqli->prepare('SELECT * FROM p...') #1 C:\xampp\htdocs\app\database\db.php(78): executeQuery('SELECT * FROM p...', Array) #2 C:\xampp\htdocs\app\helpers\validatePost.php(12): selectOne('posts', Array) #3 C:\xampp\htdocs\app\controllers\capitulos.php(18): validatePost(Array) #4 C:\xampp\htdocs\admin\capitulos\create.php(1): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\app\database\db.php on line 17

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title=? LIMIT 1' at line 1 in C:\xampp\htdocs\app\database\db.php:17 Stack trace: #0 C:\xampp\htdocs\app\database\db.php(17): mysqli->prepare('SELECT * FROM p...') #1 C:\xampp\htdocs\app\database\db.php(78): executeQuery('SELECT * FROM p...', Array) #2 C:\xampp\htdocs\app\helpers\validatePost.php(12): selectOne('posts', Array) #3 C:\xampp\htdocs\app\controllers\capitulos.php(18): validatePost(Array) #4 C:\xampp\htdocs\admin\capitulos\create.php(1): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\app\database\db.php on line 17

Buenos días actualmente poseo un error en mi pagina y no encuentro el motivo, quiero lograr que al crear un nuevo post este se encuentre de primer lugar que los anteriores por ser el mas nuevo, utilice: $sql = "SELECT * FROM $table ORDER BY id DESC"; pero me da el siguiente error

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title=? LIMIT 1' at line 1 in C:\xampp\htdocs\app\database\db.php:17 Stack trace: #0 C:\xampp\htdocs\app\database\db.php(17): mysqli->prepare('SELECT * FROM p...') #1 C:\xampp\htdocs\app\database\db.php(78): executeQuery('SELECT * FROM p...', Array) #2 C:\xampp\htdocs\app\helpers\validatePost.php(12): selectOne('posts', Array) #3 C:\xampp\htdocs\app\controllers\capitulos.php(18): validatePost(Array) #4 C:\xampp\htdocs\admin\capitulos\create.php(1): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\app\database\db.php on line 17

Buenos días actualmente poseo un error en mi pagina y no encuentro el motivo, quiero lograr que al crear un nuevo post este se encuentre de primer lugar que los anteriores por ser el mas nuevo, utilice: $sql = "SELECT * FROM $table ORDER BY id DESC"; pero me da el siguiente error

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title=? LIMIT 1' at line 1 in C:\xampp\htdocs\app\database\db.php:17 Stack trace: #0 C:\xampp\htdocs\app\database\db.php(17): mysqli->prepare('SELECT * FROM p...') #1 C:\xampp\htdocs\app\database\db.php(78): executeQuery('SELECT * FROM p...', Array) #2 C:\xampp\htdocs\app\helpers\validatePost.php(12): selectOne('posts', Array) #3 C:\xampp\htdocs\app\controllers\capitulos.php(18): validatePost(Array) #4 C:\xampp\htdocs\admin\capitulos\create.php(1): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\app\database\db.php on line 17

Origen Enlace

Necesito ayuda, quiero hacer al crear un nuevo post este se muestre de primer lugar pero poseo un error

Buenos días actualmente poseo un error en mi pagina y no encuentro el motivo, quiero lograr que al crear un nuevo post este se encuentre de primer lugar que los anteriores por ser el mas nuevo, utilice: $sql = "SELECT * FROM $table ORDER BY id DESC"; pero me da el siguiente error

Si alguien me puede ayudar se lo agradecería muchísimo.

CODE: db.php

<?php

session_start();
require('connect.php');

function dd($value)
{
    echo "<pre>", print_r($value, true), "</pre>";
    die();
}


function executeQuery($sql, $data)
{
    global $conn;
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    $stmt = $conn->prepare($sql);
    $values = array_values($data);
    $types = str_repeat('s', count($values));
    $stmt->bind_param($types, ...$values);
    $stmt->execute();
    return $stmt;
}


function selectAll($table, $conditions = [])
{
    global $conn;
    $sql = "SELECT * FROM $table ORDER BY id DESC";
    if (empty($conditions)) {
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
        return $records;    
    } else {
        // $sql = "SELECT * FROM $table WHERE username='ElVictox' AND admin=1";
        
        $i = 0;
        foreach ($conditions as $key => $value) {
            if ($i === 0){
                $sql = $sql . " WHERE $key=?";
                
            } else {
                $sql = $sql . " AND $key=?"; 
            }
            $i++;
        }

        $stmt = $conn->prepare($sql);
        $values = array_values($conditions);
        $types = str_repeat('s', count($values));
        $stmt->bind_param($types, $values);
        $stmt->execute();
        $records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
        return $records;
    }
}



function selectOne($table, $conditions)
{
    global $conn;
    $sql = "SELECT * FROM $table ORDER BY id DESC";
    
        $i = 0;
        foreach ($conditions as $key => $value) {
            if ($i === 0){
                $sql = $sql . " WHERE $key=?";
                
            } else {
                $sql = $sql . " AND $key=?"; 
            }
            $i++;
        }
    
        $sql = $sql . " LIMIT 1";
        $stmt = executeQuery($sql, $conditions);
        $records = $stmt->get_result()->fetch_assoc();
        return $records;
    }

function create($table, $data)
{
    global $conn;
    $sql = "INSERT INTO $table SET ";
      
    $i = 0;
    foreach ($data as $key => $value) {
            if ($i === 0){
                $sql = $sql . " $key=?";
                
            } else {
                $sql = $sql . ", $key=?"; 
            }
            $i++;
        }
    $stmt = executeQuery($sql, $data);
    $id = $stmt->insert_id;
    return $id;
    
}

function update($table, $id, $data)
{
    global $conn;
    $sql = "UPDATE $table SET ";
      
    $i = 0;
    foreach ($data as $key => $value) {
            if ($i === 0){
                $sql = $sql . " $key=?";
                
            } else {
                $sql = $sql . ", $key=?"; 
            }
            $i++;
        }
    
    $sql = $sql . " WHERE id=?";
    $data['id'] = $id;
    $stmt = executeQuery($sql, $data);
    return $stmt->affected_rows;
    
}


function delete($table, $id)
{
    global $conn;
    $sql = "DELETE FROM $table WHERE id=?";
      
    $stmt = executeQuery($sql, ['id' => $id]);
    return $stmt->affected_rows;
    
}

TABLE SQL:

image

ERROR:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE title=? LIMIT 1' at line 1 in C:\xampp\htdocs\app\database\db.php:17 Stack trace: #0 C:\xampp\htdocs\app\database\db.php(17): mysqli->prepare('SELECT * FROM p...') #1 C:\xampp\htdocs\app\database\db.php(78): executeQuery('SELECT * FROM p...', Array) #2 C:\xampp\htdocs\app\helpers\validatePost.php(12): selectOne('posts', Array) #3 C:\xampp\htdocs\app\controllers\capitulos.php(18): validatePost(Array) #4 C:\xampp\htdocs\admin\capitulos\create.php(1): include('C:\xampp\htdocs...') #5 {main} thrown in C:\xampp\htdocs\app\database\db.php on line 17