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
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Php getting data from a database

  • 20-02-2015 7:29pm
    #1
    Registered Users Posts: 1,558 ✭✭✭


    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: 6,198 ✭✭✭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 Posts: 1,558 ✭✭✭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