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 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

Help with php/sql

  • 30-10-2013 6:55pm
    #1
    Registered Users, Registered Users 2 Posts: 454 ✭✭


    Hi,

    I'm working on a server side high-score table for a game, which feeds the players name and their score into a c# script.

    Have a basic implementation working, but I want to add a character after name and score ("/" would do nicely), so I can split up the returned string and present it in a more appealing way. Can anyone show me how to do this? I've attached the code below, thanks.
    <?php
        // Send variables for the MySQL database class.
        $database = mysql_connect('123.456.789.012', 'username', 'password') or die('Could not connect: ' . mysql_error());
        mysql_select_db('xyz_scores') or die('Could not select database');
     
        $query = "SELECT * FROM `scores` ORDER by `score` DESC LIMIT 5";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());
     
        $num_results = mysql_num_rows($result);  
     
        for($i = 0; $i < $num_results; $i++)
        {
             $row = mysql_fetch_array($result);
             echo $row['name'] . "\t" . $row['score'] . "\n";
        }
    ?>
    


Comments

  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    echo $row['name'] . "\t" . $row['score'] . "/\n";
    

    I have only just started back to PHP, but I think '/' does not need to be escaped, so you can just add it where you want as it is. In you example I just put it in before the newline, '\n'.

    Reference


  • Registered Users, Registered Users 2 Posts: 454 ✭✭Kilgore__Trout


    Thanks, will give it a try.


  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    Keep in mind that the mysql_* functions are deprecated, and will stop working in a later release.


  • Registered Users, Registered Users 2 Posts: 2,483 ✭✭✭SweetCaliber


    oscarBravo wrote: »
    Keep in mind that the mysql_* functions are deprecated, and will stop working in a later release.

    Ah you got there before me. :p

    Yeah MySQL functions are no longer, MySQLi and PDO are in now.

    If you haven't a clue of PDO your best to stick with MySQLi which isn't too different to MySQL, example below:

    [PHP]
    <?php
    $db = mysqli_connect('host', 'user', 'pass', 'name');

    $query = mysqli_query($db, "INSERT QUERY HERE");
    ?>
    [/PHP]

    And so on.. So it's not too different.

    PDO is completely different though. It's much more to learn but on the other hand its really handy once you get the hang of it.

    [PHP]
    <?php

    $db = new PDO('mysql:host=host;dbname=name', 'username', 'password');

    try {
    $query = $db->query('INSERT QUERY HERE');
    } catch(PDOException $e) {
    die($e->getMessage());
    }

    ?>
    [/PHP]

    Sorry if I went off on a tangent! Just letting you know :P


  • Registered Users, Registered Users 2 Posts: 6,289 ✭✭✭Talisman


    I would definitely recommend using PDO for database access - it makes things far simpler to manage and you are less likely to develop bad habits. NetTuts Tutorial


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 454 ✭✭Kilgore__Trout


    Thanks guys, got it working now. Is replacing the depreciated functions just a matter of adding 'i' to the end of each mysql? Have pretty much 0 knowledge of php and sql.


Advertisement