lundi 27 juin 2016

read data from mysql rows and generate qr code

good day everyone, I have got a problem I just cant solve so I am really hoping I can get some assistance with it. I am trying to read data from mysql rows and generate qr code that is encoded with the users data that can be scanned

This is my code:

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

          include "qrlib.php";

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

          //if(isset($_POST['submit'])){

          $sql = "SELECT * FROM user";
          $result = $conn->query($sql);

          function generate_code(){

          //set it to writable location, a place for temp generated PNG files
          $PNG_TEMP_DIR =                 dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

          //html PNG location prefix
          $PNG_WEB_DIR = 'temp/';

          //include "pdf.php";

          //ofcourse we need rights to create temp dir
          if (!file_exists($PNG_TEMP_DIR))
          mkdir($PNG_TEMP_DIR);


          $filename = $PNG_TEMP_DIR.'test.png';

          //processing form input
          //remember to sanitize user input in real-life solution !!!
          $errorCorrectionLevel = 'L';
          if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H')))
          $errorCorrectionLevel = $_REQUEST['level'];    

          $matrixPointSize = 4;
          if (isset($_REQUEST['size']))
          $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10);


           if (isset($_REQUEST['data'])) { 

          //it's very important!
         if (trim($_REQUEST['data']) == '')
         die('data cannot be empty!');

         // user data
         $filename =   $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';


         QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2);    

         } else {    

          //default data
          // echo 'You can provide data in GET parameter: <a href="?                                        data=like_that">like that</a><hr/>';    
          QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel,        $matrixPointSize, 2);    

         }    
         }

        if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
        echo "name: " . $row["name"]. " - email: " . $row["email"]. " " . $row["phone"]. "<br>" ;

        generate_code();
        }
       } else {
       echo "0 results";
      }

      //}
      $conn->close();

?>

Aucun commentaire:

Enregistrer un commentaire