0

Will someone please explain to me and assist to convert my old code into the new MySQLi format? I would greatly appreciate it.

This is what my current code:

<html>
    <head>
        <title>Last 10 Results</title>
    </head>
    <body>
        <table>
        <thead>
            <tr>
                <td>Id</td>
                <td>Name</td>
            </tr>
        </thead>
        <tbody>
        <?php
            $connect = mysql_connect("localhost","root", "root");
            if (!$connect) {
                die(mysql_error());
            }
            mysql_select_db("apploymentdevs");
            $results = mysql_query("SELECT * FROM demo LIMIT 10 ORDER BY Id");
            while($row = mysql_fetch_array($results)) {
            ?>
                <tr>
                    <td><?php echo $row['Id']?></td>
                    <td><?php echo $row['Name']?></td>
                </tr>

            <?php
            }
            ?>
            </tbody>
            </table>
    </body>
</html>

Again I appreciate the assistance.

This is what I have done ( I am so sorry I am such a noob )

                    <?php
                        // Database details
                        $dbhost = 'localhost';
                        $dbuser = '#';
                        $dbpass = '#';
                        $dbname = '#';
                        $db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
                        if($db->connect_errno > 0){
                            die('Unable to connect to database [' . $db->connect_error . ']');
                        }
                        $results = mysqli_query("SELECT * FROM formdata LIMIT 10 ORDER BY id");
                        while($row = mysqli_fetch_array($results)) {
                        ?>
                            <tr>
                                <td><?php echo $row['ID']?></td>
                                <td><?php echo $row['FullName']?></td>
                                <td><?php echo $row['Mobile']?></td>
                                <td><?php echo $row['Email']?></td>
                                <td><?php echo $row['Province']?></td>
                            </tr>

                        <?php
                        }
                        ?>  
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Erik Thiart
  • 381
  • 6
  • 17

2 Answers2

2

Update this line:

$results = mysqli_query("SELECT * FROM formdata LIMIT 10 ORDER BY id");

to

$results = mysqli_query($db, "SELECT * FROM formdata ORDER BY id LIMIT 10");
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
1

The problem here, is that you haven't passed the connection to your query

$results = mysqli_query($db, "SELECT 
                        ^^^^

Read the documentation:

Unlike mysql_, it must be passed as a parameter.

Then make sure your query isn't failing.


Edit:

Footnote:

As caught by NaijaProgrammer in a comment to the OP, LIMIT goes after ORDER BY and was a complete oversight on my part.

"Suggestions: 1. check your table names. 2. Run the code in PhpMyAdmin. 3. Put the ORDER BY statement before the LIMIT statement. – NaijaProgrammer"

Reference for SELECT:

Example from the manual:

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

I would like to point out though that, you're using a lowercase here for the "id" ORDER BY id and that shouldn't be an issue for the ORDER BY.

However, there is a difference between $row['ID'] and $row['id']. If your database is configured to be case sensitive, then you will need to write the ID in lowercase.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    when someone says "I'm such a noob", many times, they don't fully grasp the official PHP manual, which is the reason they probably came to SO in the first place. Then also, they usually would prefer the code fully written out for them. I'm speaking from experience. – NaijaProgrammer Sep 16 '15 at 14:01
  • @NaijaProgrammer It's understandable. I too was a newbie once, and I didn't know what to Google for. We're talking many years ago of course ;-) – Funk Forty Niner Sep 16 '15 at 14:02
  • @NaijaProgrammer I noticed your answer got a downvote :( that was NOT me, I can assure you of that. I even got one too. – Funk Forty Niner Sep 16 '15 at 14:03
  • @Fred-ii- not to worry, getting a downvote shows that someone at least read my post, I'm cool with that. – NaijaProgrammer Sep 16 '15 at 14:04
  • @NaijaProgrammer I like your style ;-) – Funk Forty Niner Sep 16 '15 at 14:07
  • @Fred-ii-, I suppose it's time for me to say "I like your style ;-)" – NaijaProgrammer Sep 16 '15 at 14:46
  • @NaijaProgrammer thanks ;-) glad to see the OP accepted your answer, *cheers*. I'm just doing a few more edits in regards to clarification and a few other things I spotted in the OP's question/code. – Funk Forty Niner Sep 16 '15 at 14:59