Simple form of mysqli result required









up vote
0
down vote

favorite












I am mysqli upgrading from mysql. I know how to use basic connect and query statements. But I can't get the result statement to work. All the solutions on the net I have found blind me with science. I would like to have "mysql_result" working in mysqli.



$alltb = "SELECT * FROM mytable";
$alltbd=mysqli_query($conned, $alltb);
$num_all=mysqli_num_rows($alltbd);
mysqli_close($conned);
$i = 0;
while($i < $num_all)
$ttt=mysql_result($alltbd,$i,"ttt");
$sss=mysql_result($alltbd,$i,"sss");
print $ttt . " and " . $sss;
$i++;



Any help greatly appreciated. I don't want to use things like fetch, for each or array. I would like to keep it simple.



Garry Jones
Sweden










share|improve this question



























    up vote
    0
    down vote

    favorite












    I am mysqli upgrading from mysql. I know how to use basic connect and query statements. But I can't get the result statement to work. All the solutions on the net I have found blind me with science. I would like to have "mysql_result" working in mysqli.



    $alltb = "SELECT * FROM mytable";
    $alltbd=mysqli_query($conned, $alltb);
    $num_all=mysqli_num_rows($alltbd);
    mysqli_close($conned);
    $i = 0;
    while($i < $num_all)
    $ttt=mysql_result($alltbd,$i,"ttt");
    $sss=mysql_result($alltbd,$i,"sss");
    print $ttt . " and " . $sss;
    $i++;



    Any help greatly appreciated. I don't want to use things like fetch, for each or array. I would like to keep it simple.



    Garry Jones
    Sweden










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am mysqli upgrading from mysql. I know how to use basic connect and query statements. But I can't get the result statement to work. All the solutions on the net I have found blind me with science. I would like to have "mysql_result" working in mysqli.



      $alltb = "SELECT * FROM mytable";
      $alltbd=mysqli_query($conned, $alltb);
      $num_all=mysqli_num_rows($alltbd);
      mysqli_close($conned);
      $i = 0;
      while($i < $num_all)
      $ttt=mysql_result($alltbd,$i,"ttt");
      $sss=mysql_result($alltbd,$i,"sss");
      print $ttt . " and " . $sss;
      $i++;



      Any help greatly appreciated. I don't want to use things like fetch, for each or array. I would like to keep it simple.



      Garry Jones
      Sweden










      share|improve this question















      I am mysqli upgrading from mysql. I know how to use basic connect and query statements. But I can't get the result statement to work. All the solutions on the net I have found blind me with science. I would like to have "mysql_result" working in mysqli.



      $alltb = "SELECT * FROM mytable";
      $alltbd=mysqli_query($conned, $alltb);
      $num_all=mysqli_num_rows($alltbd);
      mysqli_close($conned);
      $i = 0;
      while($i < $num_all)
      $ttt=mysql_result($alltbd,$i,"ttt");
      $sss=mysql_result($alltbd,$i,"sss");
      print $ttt . " and " . $sss;
      $i++;



      Any help greatly appreciated. I don't want to use things like fetch, for each or array. I would like to keep it simple.



      Garry Jones
      Sweden







      php mysqli






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 4:04









      Sascha Frinken

      9971015




      9971015










      asked Nov 9 at 19:38









      Garry Jones

      187




      187






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You can do mysqli_fetch_all for example:



          $query = "SELECT * FROM mytable";

          $sqlResult = mysqli_query($conned, $query);

          $allRecords = mysqli_fetch_all($sqlResult, MYSQLI_ASSOC);

          mysqli_close($conned);

          foreach($allRecords as $record)
          $ttt = $record['ttt'];
          $sss = $record['sss'];
          print $ttt . " and " . $sss;






          share|improve this answer






















          • Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
            – Garry Jones
            Nov 9 at 20:00






          • 1




            SELECT * FROM mytable LIMIT 5
            – Alex
            Nov 9 at 20:01

















          up vote
          0
          down vote













          MySQL and MySQLi are PHP database extensions implemented by using PHP extension framework. PHP database extensions are used to write PHP code for accessing the database. They expose database API to provide interfaces to use database functions.



          MySQL extension is deprecated and will not be available in future PHP versions. It is recommended to use the MySQLi extension with PHP 5.5 and above.



          MySQL:



          • MySQL extension added in PHP version 2.0. and deprecated as of PHP
            5.5.0.

          • MySQL provides the procedural interface.


          • MySQL extension does not support stored procedure.


          • MySQL extension lags in security and other special features,

            comparatively.


          • Transactions are handled by SQL queries only.

          • Extension directory: ext/mysql.

          MySQLi



          • MySQLi extension added in PHP 5.5 and will work on MySQL 4.1.3 or
            above.

          • MySQLi supports prepared statements.

          • MySQLi provides both procedural and object-oriented interface.

          • MySQLi supports store procedure.

          • MySQLi extension is with enhanced security and improved debugging.

          • MySQLi supports transactions through API.

          • Extension directory: ext/mysqli.

          For MySQLi installation, click here : http://php.net/manual/en/mysqli.installation.php



          Note:



          Though MySQL extension is deprecated, for backward compatibility it will be available. But do not use if you are starting something new and recommend to migrate the older from mysql to mysqi extension.



          Other MySQLi Advantages



          • MySQLi function mysqli_query() allows to enforce error prone queries
            and prevents bugs like SQL injection.

          • Using MySQLi data fetch, we can get buffered or unbuffered based on
            server resource size.

          • MySQLi API allows executing multiple queries with single expression
            using multi_query() function.

          You can connect your application to database by two ways :



          1. Connection with the object oriented way

          2. Connection with the procedural way

          Example (MySQLi Procedural)



          <?php
          $servername = "localhost";
          $username = "username";
          $password = "password";
          $dbname = "myDB";

          // Create connection
          $conn = mysqli_connect($servername, $username, $password, $dbname);
          // Check connection
          if (!$conn)
          die("Connection failed: " . mysqli_connect_error());


          $sql = "SELECT id, firstname, lastname FROM MyGuests";
          $result = mysqli_query($conn, $sql);

          if (mysqli_num_rows($result) > 0)
          // output data of each row
          while($row = mysqli_fetch_assoc($result))
          echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

          else
          echo "0 results";


          mysqli_close($conn);
          ?>


          Example (MySQLi Object-oriented)



          <?php
          $servername = "localhost";
          $username = "username";
          $password = "password";
          $dbname = "myDB";

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


          $sql = "SELECT id, firstname, lastname FROM MyGuests";
          $result = $conn->query($sql);

          if ($result->num_rows > 0)
          echo "<table><tr><th>ID</th><th>Name</th></tr>";
          // output data of each row
          while($row = $result->fetch_assoc())
          echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";

          echo "</table>";
          else
          echo "0 results";

          $conn->close();
          ?>


          Select Data With PDO (+ Prepared Statements)



          <?php
          echo "<table style='border: solid 1px black;'>";
          echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

          class TableRows extends RecursiveIteratorIterator
          function __construct($it)
          parent::__construct($it, self::LEAVES_ONLY);


          function current()
          return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";


          function beginChildren()
          echo "<tr>";


          function endChildren()
          echo "</tr>" . "n";



          $servername = "localhost";
          $username = "username";
          $password = "password";
          $dbname = "myDBPDO";

          try
          $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
          $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
          $stmt->execute();

          // set the resulting array to associative
          $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
          foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
          echo $v;


          catch(PDOException $e)
          echo "Error: " . $e->getMessage();

          $conn = null;
          echo "</table>";
          ?>





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232262%2fsimple-form-of-mysqli-result-required%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You can do mysqli_fetch_all for example:



            $query = "SELECT * FROM mytable";

            $sqlResult = mysqli_query($conned, $query);

            $allRecords = mysqli_fetch_all($sqlResult, MYSQLI_ASSOC);

            mysqli_close($conned);

            foreach($allRecords as $record)
            $ttt = $record['ttt'];
            $sss = $record['sss'];
            print $ttt . " and " . $sss;






            share|improve this answer






















            • Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
              – Garry Jones
              Nov 9 at 20:00






            • 1




              SELECT * FROM mytable LIMIT 5
              – Alex
              Nov 9 at 20:01














            up vote
            1
            down vote













            You can do mysqli_fetch_all for example:



            $query = "SELECT * FROM mytable";

            $sqlResult = mysqli_query($conned, $query);

            $allRecords = mysqli_fetch_all($sqlResult, MYSQLI_ASSOC);

            mysqli_close($conned);

            foreach($allRecords as $record)
            $ttt = $record['ttt'];
            $sss = $record['sss'];
            print $ttt . " and " . $sss;






            share|improve this answer






















            • Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
              – Garry Jones
              Nov 9 at 20:00






            • 1




              SELECT * FROM mytable LIMIT 5
              – Alex
              Nov 9 at 20:01












            up vote
            1
            down vote










            up vote
            1
            down vote









            You can do mysqli_fetch_all for example:



            $query = "SELECT * FROM mytable";

            $sqlResult = mysqli_query($conned, $query);

            $allRecords = mysqli_fetch_all($sqlResult, MYSQLI_ASSOC);

            mysqli_close($conned);

            foreach($allRecords as $record)
            $ttt = $record['ttt'];
            $sss = $record['sss'];
            print $ttt . " and " . $sss;






            share|improve this answer














            You can do mysqli_fetch_all for example:



            $query = "SELECT * FROM mytable";

            $sqlResult = mysqli_query($conned, $query);

            $allRecords = mysqli_fetch_all($sqlResult, MYSQLI_ASSOC);

            mysqli_close($conned);

            foreach($allRecords as $record)
            $ttt = $record['ttt'];
            $sss = $record['sss'];
            print $ttt . " and " . $sss;







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 22:01

























            answered Nov 9 at 19:48









            Alex

            14.1k11736




            14.1k11736











            • Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
              – Garry Jones
              Nov 9 at 20:00






            • 1




              SELECT * FROM mytable LIMIT 5
              – Alex
              Nov 9 at 20:01
















            • Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
              – Garry Jones
              Nov 9 at 20:00






            • 1




              SELECT * FROM mytable LIMIT 5
              – Alex
              Nov 9 at 20:01















            Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
            – Garry Jones
            Nov 9 at 20:00




            Thanks. If I just wanted to run it on 5 values I would use while($i < 5) what is the eqiv of that?
            – Garry Jones
            Nov 9 at 20:00




            1




            1




            SELECT * FROM mytable LIMIT 5
            – Alex
            Nov 9 at 20:01




            SELECT * FROM mytable LIMIT 5
            – Alex
            Nov 9 at 20:01












            up vote
            0
            down vote













            MySQL and MySQLi are PHP database extensions implemented by using PHP extension framework. PHP database extensions are used to write PHP code for accessing the database. They expose database API to provide interfaces to use database functions.



            MySQL extension is deprecated and will not be available in future PHP versions. It is recommended to use the MySQLi extension with PHP 5.5 and above.



            MySQL:



            • MySQL extension added in PHP version 2.0. and deprecated as of PHP
              5.5.0.

            • MySQL provides the procedural interface.


            • MySQL extension does not support stored procedure.


            • MySQL extension lags in security and other special features,

              comparatively.


            • Transactions are handled by SQL queries only.

            • Extension directory: ext/mysql.

            MySQLi



            • MySQLi extension added in PHP 5.5 and will work on MySQL 4.1.3 or
              above.

            • MySQLi supports prepared statements.

            • MySQLi provides both procedural and object-oriented interface.

            • MySQLi supports store procedure.

            • MySQLi extension is with enhanced security and improved debugging.

            • MySQLi supports transactions through API.

            • Extension directory: ext/mysqli.

            For MySQLi installation, click here : http://php.net/manual/en/mysqli.installation.php



            Note:



            Though MySQL extension is deprecated, for backward compatibility it will be available. But do not use if you are starting something new and recommend to migrate the older from mysql to mysqi extension.



            Other MySQLi Advantages



            • MySQLi function mysqli_query() allows to enforce error prone queries
              and prevents bugs like SQL injection.

            • Using MySQLi data fetch, we can get buffered or unbuffered based on
              server resource size.

            • MySQLi API allows executing multiple queries with single expression
              using multi_query() function.

            You can connect your application to database by two ways :



            1. Connection with the object oriented way

            2. Connection with the procedural way

            Example (MySQLi Procedural)



            <?php
            $servername = "localhost";
            $username = "username";
            $password = "password";
            $dbname = "myDB";

            // Create connection
            $conn = mysqli_connect($servername, $username, $password, $dbname);
            // Check connection
            if (!$conn)
            die("Connection failed: " . mysqli_connect_error());


            $sql = "SELECT id, firstname, lastname FROM MyGuests";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0)
            // output data of each row
            while($row = mysqli_fetch_assoc($result))
            echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

            else
            echo "0 results";


            mysqli_close($conn);
            ?>


            Example (MySQLi Object-oriented)



            <?php
            $servername = "localhost";
            $username = "username";
            $password = "password";
            $dbname = "myDB";

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


            $sql = "SELECT id, firstname, lastname FROM MyGuests";
            $result = $conn->query($sql);

            if ($result->num_rows > 0)
            echo "<table><tr><th>ID</th><th>Name</th></tr>";
            // output data of each row
            while($row = $result->fetch_assoc())
            echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";

            echo "</table>";
            else
            echo "0 results";

            $conn->close();
            ?>


            Select Data With PDO (+ Prepared Statements)



            <?php
            echo "<table style='border: solid 1px black;'>";
            echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

            class TableRows extends RecursiveIteratorIterator
            function __construct($it)
            parent::__construct($it, self::LEAVES_ONLY);


            function current()
            return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";


            function beginChildren()
            echo "<tr>";


            function endChildren()
            echo "</tr>" . "n";



            $servername = "localhost";
            $username = "username";
            $password = "password";
            $dbname = "myDBPDO";

            try
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
            $stmt->execute();

            // set the resulting array to associative
            $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
            foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
            echo $v;


            catch(PDOException $e)
            echo "Error: " . $e->getMessage();

            $conn = null;
            echo "</table>";
            ?>





            share|improve this answer
























              up vote
              0
              down vote













              MySQL and MySQLi are PHP database extensions implemented by using PHP extension framework. PHP database extensions are used to write PHP code for accessing the database. They expose database API to provide interfaces to use database functions.



              MySQL extension is deprecated and will not be available in future PHP versions. It is recommended to use the MySQLi extension with PHP 5.5 and above.



              MySQL:



              • MySQL extension added in PHP version 2.0. and deprecated as of PHP
                5.5.0.

              • MySQL provides the procedural interface.


              • MySQL extension does not support stored procedure.


              • MySQL extension lags in security and other special features,

                comparatively.


              • Transactions are handled by SQL queries only.

              • Extension directory: ext/mysql.

              MySQLi



              • MySQLi extension added in PHP 5.5 and will work on MySQL 4.1.3 or
                above.

              • MySQLi supports prepared statements.

              • MySQLi provides both procedural and object-oriented interface.

              • MySQLi supports store procedure.

              • MySQLi extension is with enhanced security and improved debugging.

              • MySQLi supports transactions through API.

              • Extension directory: ext/mysqli.

              For MySQLi installation, click here : http://php.net/manual/en/mysqli.installation.php



              Note:



              Though MySQL extension is deprecated, for backward compatibility it will be available. But do not use if you are starting something new and recommend to migrate the older from mysql to mysqi extension.



              Other MySQLi Advantages



              • MySQLi function mysqli_query() allows to enforce error prone queries
                and prevents bugs like SQL injection.

              • Using MySQLi data fetch, we can get buffered or unbuffered based on
                server resource size.

              • MySQLi API allows executing multiple queries with single expression
                using multi_query() function.

              You can connect your application to database by two ways :



              1. Connection with the object oriented way

              2. Connection with the procedural way

              Example (MySQLi Procedural)



              <?php
              $servername = "localhost";
              $username = "username";
              $password = "password";
              $dbname = "myDB";

              // Create connection
              $conn = mysqli_connect($servername, $username, $password, $dbname);
              // Check connection
              if (!$conn)
              die("Connection failed: " . mysqli_connect_error());


              $sql = "SELECT id, firstname, lastname FROM MyGuests";
              $result = mysqli_query($conn, $sql);

              if (mysqli_num_rows($result) > 0)
              // output data of each row
              while($row = mysqli_fetch_assoc($result))
              echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

              else
              echo "0 results";


              mysqli_close($conn);
              ?>


              Example (MySQLi Object-oriented)



              <?php
              $servername = "localhost";
              $username = "username";
              $password = "password";
              $dbname = "myDB";

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


              $sql = "SELECT id, firstname, lastname FROM MyGuests";
              $result = $conn->query($sql);

              if ($result->num_rows > 0)
              echo "<table><tr><th>ID</th><th>Name</th></tr>";
              // output data of each row
              while($row = $result->fetch_assoc())
              echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";

              echo "</table>";
              else
              echo "0 results";

              $conn->close();
              ?>


              Select Data With PDO (+ Prepared Statements)



              <?php
              echo "<table style='border: solid 1px black;'>";
              echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

              class TableRows extends RecursiveIteratorIterator
              function __construct($it)
              parent::__construct($it, self::LEAVES_ONLY);


              function current()
              return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";


              function beginChildren()
              echo "<tr>";


              function endChildren()
              echo "</tr>" . "n";



              $servername = "localhost";
              $username = "username";
              $password = "password";
              $dbname = "myDBPDO";

              try
              $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
              $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
              $stmt->execute();

              // set the resulting array to associative
              $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
              foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
              echo $v;


              catch(PDOException $e)
              echo "Error: " . $e->getMessage();

              $conn = null;
              echo "</table>";
              ?>





              share|improve this answer






















                up vote
                0
                down vote










                up vote
                0
                down vote









                MySQL and MySQLi are PHP database extensions implemented by using PHP extension framework. PHP database extensions are used to write PHP code for accessing the database. They expose database API to provide interfaces to use database functions.



                MySQL extension is deprecated and will not be available in future PHP versions. It is recommended to use the MySQLi extension with PHP 5.5 and above.



                MySQL:



                • MySQL extension added in PHP version 2.0. and deprecated as of PHP
                  5.5.0.

                • MySQL provides the procedural interface.


                • MySQL extension does not support stored procedure.


                • MySQL extension lags in security and other special features,

                  comparatively.


                • Transactions are handled by SQL queries only.

                • Extension directory: ext/mysql.

                MySQLi



                • MySQLi extension added in PHP 5.5 and will work on MySQL 4.1.3 or
                  above.

                • MySQLi supports prepared statements.

                • MySQLi provides both procedural and object-oriented interface.

                • MySQLi supports store procedure.

                • MySQLi extension is with enhanced security and improved debugging.

                • MySQLi supports transactions through API.

                • Extension directory: ext/mysqli.

                For MySQLi installation, click here : http://php.net/manual/en/mysqli.installation.php



                Note:



                Though MySQL extension is deprecated, for backward compatibility it will be available. But do not use if you are starting something new and recommend to migrate the older from mysql to mysqi extension.



                Other MySQLi Advantages



                • MySQLi function mysqli_query() allows to enforce error prone queries
                  and prevents bugs like SQL injection.

                • Using MySQLi data fetch, we can get buffered or unbuffered based on
                  server resource size.

                • MySQLi API allows executing multiple queries with single expression
                  using multi_query() function.

                You can connect your application to database by two ways :



                1. Connection with the object oriented way

                2. Connection with the procedural way

                Example (MySQLi Procedural)



                <?php
                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDB";

                // Create connection
                $conn = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$conn)
                die("Connection failed: " . mysqli_connect_error());


                $sql = "SELECT id, firstname, lastname FROM MyGuests";
                $result = mysqli_query($conn, $sql);

                if (mysqli_num_rows($result) > 0)
                // output data of each row
                while($row = mysqli_fetch_assoc($result))
                echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

                else
                echo "0 results";


                mysqli_close($conn);
                ?>


                Example (MySQLi Object-oriented)



                <?php
                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDB";

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


                $sql = "SELECT id, firstname, lastname FROM MyGuests";
                $result = $conn->query($sql);

                if ($result->num_rows > 0)
                echo "<table><tr><th>ID</th><th>Name</th></tr>";
                // output data of each row
                while($row = $result->fetch_assoc())
                echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";

                echo "</table>";
                else
                echo "0 results";

                $conn->close();
                ?>


                Select Data With PDO (+ Prepared Statements)



                <?php
                echo "<table style='border: solid 1px black;'>";
                echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

                class TableRows extends RecursiveIteratorIterator
                function __construct($it)
                parent::__construct($it, self::LEAVES_ONLY);


                function current()
                return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";


                function beginChildren()
                echo "<tr>";


                function endChildren()
                echo "</tr>" . "n";



                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDBPDO";

                try
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
                $stmt->execute();

                // set the resulting array to associative
                $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
                echo $v;


                catch(PDOException $e)
                echo "Error: " . $e->getMessage();

                $conn = null;
                echo "</table>";
                ?>





                share|improve this answer












                MySQL and MySQLi are PHP database extensions implemented by using PHP extension framework. PHP database extensions are used to write PHP code for accessing the database. They expose database API to provide interfaces to use database functions.



                MySQL extension is deprecated and will not be available in future PHP versions. It is recommended to use the MySQLi extension with PHP 5.5 and above.



                MySQL:



                • MySQL extension added in PHP version 2.0. and deprecated as of PHP
                  5.5.0.

                • MySQL provides the procedural interface.


                • MySQL extension does not support stored procedure.


                • MySQL extension lags in security and other special features,

                  comparatively.


                • Transactions are handled by SQL queries only.

                • Extension directory: ext/mysql.

                MySQLi



                • MySQLi extension added in PHP 5.5 and will work on MySQL 4.1.3 or
                  above.

                • MySQLi supports prepared statements.

                • MySQLi provides both procedural and object-oriented interface.

                • MySQLi supports store procedure.

                • MySQLi extension is with enhanced security and improved debugging.

                • MySQLi supports transactions through API.

                • Extension directory: ext/mysqli.

                For MySQLi installation, click here : http://php.net/manual/en/mysqli.installation.php



                Note:



                Though MySQL extension is deprecated, for backward compatibility it will be available. But do not use if you are starting something new and recommend to migrate the older from mysql to mysqi extension.



                Other MySQLi Advantages



                • MySQLi function mysqli_query() allows to enforce error prone queries
                  and prevents bugs like SQL injection.

                • Using MySQLi data fetch, we can get buffered or unbuffered based on
                  server resource size.

                • MySQLi API allows executing multiple queries with single expression
                  using multi_query() function.

                You can connect your application to database by two ways :



                1. Connection with the object oriented way

                2. Connection with the procedural way

                Example (MySQLi Procedural)



                <?php
                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDB";

                // Create connection
                $conn = mysqli_connect($servername, $username, $password, $dbname);
                // Check connection
                if (!$conn)
                die("Connection failed: " . mysqli_connect_error());


                $sql = "SELECT id, firstname, lastname FROM MyGuests";
                $result = mysqli_query($conn, $sql);

                if (mysqli_num_rows($result) > 0)
                // output data of each row
                while($row = mysqli_fetch_assoc($result))
                echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";

                else
                echo "0 results";


                mysqli_close($conn);
                ?>


                Example (MySQLi Object-oriented)



                <?php
                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDB";

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


                $sql = "SELECT id, firstname, lastname FROM MyGuests";
                $result = $conn->query($sql);

                if ($result->num_rows > 0)
                echo "<table><tr><th>ID</th><th>Name</th></tr>";
                // output data of each row
                while($row = $result->fetch_assoc())
                echo "<tr><td>".$row["id"]."</td><td>".$row["firstname"]." ".$row["lastname"]."</td></tr>";

                echo "</table>";
                else
                echo "0 results";

                $conn->close();
                ?>


                Select Data With PDO (+ Prepared Statements)



                <?php
                echo "<table style='border: solid 1px black;'>";
                echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>";

                class TableRows extends RecursiveIteratorIterator
                function __construct($it)
                parent::__construct($it, self::LEAVES_ONLY);


                function current()
                return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";


                function beginChildren()
                echo "<tr>";


                function endChildren()
                echo "</tr>" . "n";



                $servername = "localhost";
                $username = "username";
                $password = "password";
                $dbname = "myDBPDO";

                try
                $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $stmt = $conn->prepare("SELECT id, firstname, lastname FROM MyGuests");
                $stmt->execute();

                // set the resulting array to associative
                $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
                foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
                echo $v;


                catch(PDOException $e)
                echo "Error: " . $e->getMessage();

                $conn = null;
                echo "</table>";
                ?>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 4:31









                Hemant Kumar

                291116




                291116



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232262%2fsimple-form-of-mysqli-result-required%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo