Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Php getting data from a database

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


    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");
               
            }
    


Welcome!

It looks like you're new here. Sign in or register to get started.

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: 6,682 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,560 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

Welcome!

It looks like you're new here. Sign in or register to get started.
Advertisement