0

I'm trying to read a text file using PHP. After I read the content I want to match the content with an existing table in mysql.

My text file has this content

           :mh2045

The file is a .txt file

I use the following php code to read the text file

            <?php
               $file =         file_get_contents("file.txt");
              ?>

How do I use the contents of $file to compare with the field "vehicleid" of table vehicle info in mysql

this is the php code for selecting records after comparing with content in $file

      <?php
       $servername = "localhost";
       $username = "root";
       $password = "password";
       $dbname = "sample";
       $file=file_get_contents("file.txt");
       @var =$file;

         // Create connection
          $conn = new mysqli($servername, $username, $password,             $dbname);
         // Check connection
         if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
         } 

      $sql = "SELECT *
                   FROM vehicleinfo
                   WHERE vehicleid =    (SELECT vehicleid 
                                      FROM vehicleinfo
                                       WHERE owner= @var)";
       $result = $conn->query($sql);

      if ($result->num_rows > 0) {
       // output data of each row
     while($row = $result->fetch_assoc()) {
              echo "id: " . $row["vehicleid"]. " - Name: " .              $row["name"]. " " . $row["owner"]. "<br>";
 }
   } else {
   echo "0 results";
        }
               $conn->close();       
3
  • From where did you get @var? Commented Jan 26, 2015 at 5:34
  • @var seems to be the Problem. For Variables in PHP you use $. @ is for Error Handling Commented Jan 26, 2015 at 5:35
  • vardump() $file, u will see ur variable content, and will find out how to use it. Commented Jan 26, 2015 at 5:35

2 Answers 2

1

For Variables in PHP use $ not @. @ is for Error Handling. With a . you can connect Strings, ist a bad Practice to write the Var into a String. And also use ' rather then " for Strings. That makes your proccesing quicker, becouse the Compiler doesn't look vor Variables in the String.

$file = file("file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$sql = 'SELECT *
                   FROM vehicleinfo
                   WHERE vehicleid =    (SELECT vehicleid 
                                      FROM vehicleinfo
                                       WHERE owner= '. $file .')';
Sign up to request clarification or add additional context in comments.

6 Comments

now it doest compare owner's value in the database with the content of $file which seems to be an array of string says zero result altough there is a record in the vehicle info table where owner = content of $file
Do you get an Error Mesg? You Query might be wrong, try 'SELECT * FROM vehicleinfo WHERE owner = $file
$sql = "SELECT * FROM vehicleinfo WHERE owner = ' . $file[0] . ')"; i tried this it says again trying to get non object property
if i use $file instead of $file[0] it says tring to convert string into array
Do you have Access to phpmyAdmin or similar? Try Your Query in there to make sure you Query is not the Problem
|
0

Get rid of the @var line and use following SQL query:

$file = file("file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$sql = "SELECT *
                   FROM vehicleinfo
                   WHERE vehicleid =    (SELECT vehicleid 
                                      FROM vehicleinfo
                                       WHERE owner= ". $file[0] .")";

5 Comments

I tried doing it,it says,"trying to get property of non object in D :\wamp\www\display.php
insert vardump($file); and tell us what it says
string 'mh2045' (length=6)
the content of the text file is string
then you dont Need the[0] behind the $file in the SQL Query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.