-2

I'm getting a "Syntax error or access violation: 1064" error

This is the error message I got :

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

This my code :

 <?php
    
    require "connection.php";
    require "connection1.php";
    echo "<hr>";
    error_reporting(E_ALL);

    /*
    $servername = "web06";
    $username = "anon";
    $password = "aiy7aethak";
    $dbname = "anon";
    */

    
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "anon";
    $dbname2 ="anon2";
        
        try {
        
        //exportation dump de la BDD Test en .sql

        exec ("/Applications/MAMP/Library/bin/mysqldump --user={$username} --password={$password} --host={$servername} $dbname > Test.sql");

        //importation dump de la BDD Test.sql

        exec ("/Applications/MAMP/Library/bin/mysql --user=$username --password=$password --host=$servername $dbname2 < Test.sql");


        $req =$conn->query("SHOW TABLES");
        $rows = $req->fetchAll();
        
        $fields = array();

            foreach ($rows as $key => $row) {

                echo $row["Tables_in_test"].'<br>'.'<br>';
                $fields[] =$row["Tables_in_test"];
                
                $table = $row["Tables_in_test"];

                $req1 =$conn->query("SHOW FIELDS FROM ".$table);
                $rows1 = $req1->fetchAll();

                $fields1 = array();

                    foreach ($rows1 as $key1 => $row1) {

                        echo $row1["Field"].'<br>'.'<br>';
                        $fields1[] =$row1["Field"];
            
                        $req2 = $conn->query("SELECT * FROM ".$table);
                        $rows2 = $req2->fetchAll();

                        var_dump($rows2);
                        $fields2 = array();
                        $fields2[] =$rows2["nom"];



                        /*
                        echo $rows2["prenom"]."<br>";
                        echo $rows2["nom"]."<br>";
                        echo $rows2["numId"]."<br>";
                        echo $rows2["adresse"]."<br>";
                        echo $rows2["sexe"]."<br>";

                        $fields2 = array();
                        */

                    }
                            
            }     
            
   
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    


    ?>

And this is the connection code :

<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "anon";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", 
$username,$password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connection reussi"."<br>";
    }
catch(PDOException $e)
     {
    echo ("Errror" . $e->getMessage());
   }
    ?>

Please help me

Hirhoz
  • 1
  • Do not use SHOW statements, query to INFORMATION_SCHEMA system views instead. Also you must take into account that some tables names may need in quoting. – Akina May 30 '22 at 04:42
  • First of all you need to get rid of this useless try catch stuff. With that we don't even know which query returns the error – Your Common Sense May 30 '22 at 05:11

0 Answers0