-1

I have checked other questions with the same title but can't fix this. I have connected to the database though because the login works and I can display the user's username. Here is the relevent code, the second part is what's causing the error, thanks:

<?php require 'connection.php' ?>
<?php
session_start();
?>


<content>
                <?php
                //Select all from the categories table and order by the  category title in ascending order (ASC)
                $sql = "SELECT * FROM categories ORDER BY category_title ASC";

                //$res = the result of the above query or die if error
                $res = mysql_query($sql) or die (mysql_error());

                if (mysql_num_rows($res) > 0) {

                } else {
                    "<p>No categories availible</p>";
                }


                ?>

                </content>
gogo123
  • 1
  • 1

1 Answers1

2

Here is an example, for the visual of it.

<?php
    $dbname = 'so_gibberish';
    $dbuser = 'GuySmiley';
    $dbpass = 'MyPassword';
    $dbhost = 'localhost';
    $link = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
    mysql_select_db($dbname) or die("Could not open the db '$dbname'");

    $test_query = "SHOW TABLES FROM $dbname";
    $result = mysql_query($test_query);
    ...
    ...
    mysql_close($link);

However, go with PDO

It is less sickly, full of robustness, and safer to say the least for binding:

Again, just a visual.

<?php
    $theNum=102;    // get from user, this is hard-coded
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=so_gibberish;charset=utf8', 'GuySmiley', 'MyPassword');
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $stmt = $dbh->prepare('SELECT id,smin,smax from ranges7 
            WHERE smin <= :theNum AND smax >= :theNum');
        $stmt->bindParam(':theNum', $theNum, PDO::PARAM_INT);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            // one row we hope
            $data = "id: ".$row['id'] . ", smin: " . $row['smin'] . ", smax: " . $row['smax'] . "\n";
            print $data;
        }
        $stmt = null;
        // PDO closes connection at end of script

    } catch (PDOException $e) {
        echo 'PDO Exception: ' . $e->getMessage();
        exit();
    }
?>
Drew
  • 24,851
  • 10
  • 43
  • 78