How can I fix this code?

KMason015

New Member
Messages
2
Reaction score
0
Points
1
Good evening! i'm currently trying to run this PHP code on my site. the intended thing is that it reads from a phpMyAdmin database, and I can't even tell atm if it has connected properly or not. how can i fix this to get a result other than a blank screen?
<html>
<head>
<title>Query All Games from Database</title>
<body>
<?

@ $db = mysql_pconnect("HOSTNAME","USERNAME","PASSWORD"); //actual details of this omitted for obvious reasons.

if (!$db)
{
echo "ERROR: Could not connect to database. Please try again later.";
exit;
}

mysql_select_db("DATABASENAME");

$query = "select * from titles";

$result = mysql_query($query);
$num_results = mysql_num_rows($result);

echo "<p>Number of games found: ".$num_results."</p>";

for ($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
echo "<p>";
echo htmlspecialchars( stripslashes($row["gameid"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["gametitle"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["console"]));
echo "<br>";
echo htmlspecialchars( stripslashes($row["comments"]));
echo "<br>";
echo "</p>";

}

?>

</body>
</html>
 

appgetmgr

New Member
Messages
18
Reaction score
0
Points
1
You will have to rewrite that to pdo (or something more modern) Here is something to get you started. This will connect to your db. Change as needed

PHP:
    $host         = '127.0.0.1';
    $db           = 'dbname';
    $user         = 'dbuser';
    $pass         = 'dbpass';
    $port         = "3306";
    $charset     = 'utf8mb4';
    $table         = 'dbtable';

    $options = [
        \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
        \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
        \PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $conn = "mysql:host=$host;dbname=$db;charset=$charset;port=$port";
    try {
         $con = new \PDO($conn, $user, $pass, $options);
    } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }

Then use $conn to manipulate your php/sql. sounds like there is no support in the support forum for scripts, so hopefully this will show up and give you a start.

Also, I see you are using "<? " I dont know if short tags are turned on, however I would change all those to "<?php" to avoid issues
 

KMason015

New Member
Messages
2
Reaction score
0
Points
1
Oh, right.. i forgot i had put this question in, to be honest. it has been a while since I did this, and since then, I was able to eventually figure out a solution, but thank you for the help regardless! this was for a website i had to build pretty much last minute for a final (since my previous work on Infinityfree was lost during the server migration) that has since been taken down for inactivity, but this solution is still helpful, so thank you!
 

appgetmgr

New Member
Messages
18
Reaction score
0
Points
1
You are welcome. Anything help in the future let me know. I wasn't sure how far to go with solution, as the previous text says that they do not for help in script. I wasn't sure if that applied to community members or just the staff alone. Anyways the thread was still open and it wasn't much of a solution so I thought I'd toss one in. Glad it worked out have a great week
 
Top