0

Trying to access a MySQL database on my machine. It's on port 1338.

This is the PHP file:

<!DOCTYPE html>

<html>

<head>

</head>


<body>

    <table>
    <?php

        $connection = mysql_connect("localhost:1338","root","password"); //connects to db
        $selected = mysql_select_db("site_updatelog"); //selects the db to be used
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }

        $sql = "SELECT comments, version, datetime FROM changeLog ORDER BY id DESC"; //form the query to be executed on the db
        $result = mysql_query($sql, $connections) or die (mysql_error()); //execute the query, or die if there was an error

        while($row = mysql_fetch_array($result)){
            //Display the table of results
            echo "<tr><td>". $row['version'] ."</td><td>". $row['comments'] ."</td><td>". $row['datetime'] ."</td></tr>";
        }

        mysql_close($connection);
     ?>
</table>


</body>

</html>

This is the output:

"; } mysql_close($connection); ?>
". $row['version'] ."   ". $row['comments'] ."  ". $row['datetime'] ."

Been messing around for quite a while and it still wont work. Any ideas?

2
  • Do not use mysql_* it is depricated . Use PDO Commented Nov 24, 2012 at 14:51
  • Tried both of these and the output was still the same. I am using that MySQL 5.5 utility for this as well. Commented Nov 24, 2012 at 15:05

3 Answers 3

1

change mysql_query($sql, $connections) with mysql_query($sql, $connection)

Sign up to request clarification or add additional context in comments.

1 Comment

are you sure this is the exact example you have in your script? please give me all the code you have
1

Probably it is because you have unescaped characters in your data which you are echoing - try to wrap your $row['comments'], $row['version'] and $row['datetime'] to htmlspecialschars

Comments

0

Using the code below only dies and reports error if (!$connection)

$connection = mysql_connect("localhost:1338","root","password"); //connects to db
        $selected = mysql_select_db("site_updatelog"); //selects the db to be used
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }

Try using

$connection = mysql_connect("localhost:1338","root","password"); //connects to db
        //Check whether connection was successful
        if(!$connection){
            die('Could not connect to database: ' . mysql_error()); //Stop further execution of php script and show the error that occured
        }
        $selected = mysql_select_db("site_updatelog",$connection); //selects the db to be used
        if (!$db_selected) {
            die ("Can\'t use db : " . mysql_error());
        }
        $sql = "SELECT comments, version, datetime FROM changeLog ORDER BY id DESC"; //form the query to be executed on the db
        $result = mysql_query($sql) ;

        while($row = mysql_fetch_assoc($result)){
            //Display the table of results
            echo "<tr><td>". $row['version'] ."</td><td>". $row['comments'] ."</td><td>". $row['datetime'] ."</td></tr>";
        }

        mysql_close($connection);

This throws errors before running query. Also changed mysql_fetch_array() to mysql_fetch_assoc() as mysql_fetch_array() requires 2 parameters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.