Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Php getting data from a database

  • 20-02-2015 07:29PM
    #1
    Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭


    I was just wondering what is php's pdo equivalent of java's resultset.getString?
    I'd like to be able to seperate column values and give them a different css styling and am finding it tricky.
    Here's an example of what I mean that I'd like to be able to do in php but it's in java
    ResultSet rs = stmt.executeQuery(query);
            while (rs.next()) {
                String name = rs.getString("name");
                String description = rs.getString("description");
                double price = rs.getDouble("price");
               
            }
    


Comments

  • Registered Users, Registered Users 2 Posts: 1,456 ✭✭✭FSL


    Here is an equivalent using mysql and php

    <?php
    $query = "Query string";
    $result=mysql_query($query) or die(mysql_error("Query Failed!"));
    $num=mysql_numrows($result);
    $i=0;
    While ($i < $num) {
    $name=mysql_result($result,$i,"name");
    $description=mysql_result($result,$i,"description");
    $price=mysql_result($result,$i,"price");
    $i++;
    }
    ?>


  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    Something like:
    <?php
    try {
       $stmt = $db->prepare("SELECT * FROM foo");
    	$stmt->execute();
    } catch ( PDOException $ex ) {
    	echo "An Error occured when retrieving commenta: " . $ex->getMessage ();
    }
    
    if ($stmt->rowCount() < 1) {
       echo 'Nothing returned';
    } else {
       while ($row = $stmt->fetch()) {
          echo '<div class="cls1">First col '
               .htmlspecialchars($row['foo'], ENT_HTML5, 'UTF-8', false)
               .'</div>';
          echo '<div class="cls2">'
               .htmlspecialchars($row['bar'], ENT_HTML5, 'UTF-8', false)
               .'</div>';
       }
    }
    %>
    


  • Registered Users, Registered Users 2 Posts: 7,151 ✭✭✭Talisman


    By default the PDO fetch statement returns the data in two arrays. One is an associative array with the field names as keys, the other as a numerical array. You can get an improvement in the performance by specifying the style you want to use as a parameter in the fetch statement.

    Modification to bpmurray's code above:
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    

    You can read about the other fetch style options in the PHP.net documentation: PDOStatement::fetch.


  • Registered Users, Registered Users 2 Posts: 1,559 ✭✭✭quinnd6


    Brilliant thanks very much lads.


  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    Actually, I forgot to mention - you need to create your db connection first. This is very similar to the JDBC mechanism:
    // Link to the database server
    $db = new PDO ( 'mysql:host=localhost;dbname=mydatabase;charset=UTF-8', 'root', 'passw0rd' );
    
    // Attributes, e.g. if an error occurs, we want to catch it as an "exception"
    $db->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    


  • Advertisement
Advertisement