0

I am trying to query some data from my table in mysql by reading a txt file in php.

Please Note that I tried with mysqli and got no result ! below is my code:

$dbhost = "localhost";
$dbutilisateur = "root";
$dbpasse = "";
$dbnom = "test";

$file= file_get_contents("List.txt");
$data = $file;


$connexion = new  PDO('mysql:host=' . $dbhost . ';dbname=' . $dbnom, 
$dbutilisateur, $dbpasse);

if(isset($_GET['action'])){


$sql = 'SELECT * FROM servers
            WHERE cpu_manufacturer = $data';


$req_prepare= $connexion->prepare($sql);

$req_prepare->execute();

$result = $req_prepare->fetchAll(PDO::FETCH_ASSOC);




$contenu = '';

$contenu .= '

   <table class="table table-bordered">

    <tr>

     <th width="26,66%">cpu_manufacturer</th>

     <th width="26,66%">server_location</th>

     <th width="26,66%">provider_name</th>

    </tr>

';

if($req_prepare->rowCount() > 0)
    {
        foreach($result as $row)

    {
        $contenu .= '

        <tr>

 <td style="background:whitesmoke">'.$row["cpu_manufacturer"].'</td>

 <td style="background:whitesmoke"">'.$row["server_location"].'</td>

 <td style="background:whitesmoke"" >'.$row["provider_name"].'</td>


        </tr>
    ';
    }
 }

 else

    {

    $contenu .= '

     <tr>

     <td align="center">No data found!</td>

     </tr>

   ';

   }

  $contenu .= '</table>';

  echo $contenu;


}

Below is the content of text file:

cpu_manufacturer: AMD server_location: USA provider_name: AAAA

Your response will be greatly appreciated!

Saeid57
  • 1
  • 3
  • you are right, but there is no good answer from that question. mine is reading data from a txt file first .that's why I posted mine again here! my query itself working just fine in phpmyadmin . but dont know why in my php code gives this PDO statement error.... – Saeid57 May 13 '17 at 00:36
  • What is the ***exact query*** that "works fine" in phpMyAdmin? – FKEinternet May 14 '17 at 10:33

1 Answers1

0

Are you trying to get your data from the text file or from the database?

As written, your query evaluates to

SELECT * FROM servers
        WHERE cpu_manufacturer = (SELECT cpu_manufacturer FROM servers
        WHERE cpu_manufacturer= cpu_manufacturer: AMD server_location: USA provider_name: AAAA)

Even if you enclosed your data from the text file in single quotes as a text value needs to be in a query, you still won't get anything from the database - your query is very badly constructed.

FKEinternet
  • 1,050
  • 1
  • 11
  • 20
  • well, I am reading the data first from txt file and put them in my query to fetch the data from my table. let say if cpu_manufacturer is "AMD" then it will check my table to fetch rows so on and so forth.... – Saeid57 May 13 '17 at 00:30