Hello guys. I am trying to build a games website using PHP for database to store game information. I'm trying to learn how to fetch ID information. For example:

www.blabla.com/games.php?id=12345'

As you can see, right after games.php, there is '?id=12345'. Anyhow, when a user clicks or types this link, that user should be directed to the game based on the ID written at the end of the link. So basically I'm trying to figure out how to fetch a row of information per ID in mysql database. So when a user enters the games.php page, they should see that the ID shows the Title of the game, the SWF flash file to play, and the Image Icon of that particular game.

Overall, this would be the scene of a user trying to play my games on my site:

1. User types: www.blabla.com/games.php?id=12345

2. User enters games.php through a new window and the ID found from the link fetches Title, SWF game and Image Icon based on the ID written in the link.

<html>fldGameName</html>
========================
| |
| |
| |
|<html>Flash Game executed|
| (fldSwfFile)</html> |
| |
| |
| |
==========================
<html>fldImageIcon</html>


The best example I can give you guys if nobody understands, is to visit www.games68.com as this is what I am trying to execute on my website. If you click on their games, they have games.php?id=12345 as their URL's.


This is the code I so far have on my page but does not work AT ALL:
===================================================================
<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games");
$game_id = $_GET;
$sql = "SELECT * FROM games WHERE id='$game_id' LIMIT 1";
$result = mysql_query($sql, $conn) or die(mysql_error());
$number_of_results = mysql_num_rows($result);

while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC)) {

$id = $result_array;
$title = $result_array;
$iconsmall = $result_array; // Example: game1234.png
$swf = $result_array; // Example: game1234.swf
$iconsmall_file_location = 'http://www.blabla.com/games/images/' . $iconsmall;
$swf_file_location = 'http://www.blabla.com/games/' .$swf;

echo '<a href="http://www.blabla.com/final.php?id=' . $id . '"></a>';
echo '<h1>' . $title . '</h1>';
echo '<img src="' . $iconsmall_file_location . '" width="50" height="50" border="0" />';
echo '<object width="550" height="400">';
echo '<param name="movie" value="' . $swf_file_location . '">';
echo '<embed src="' . $swf_file_location . '" width="550" height="400">';
echo '</embed>';
echo '</object>';

}
?>
===================================================================

Another PHP expert has told me that there is no problem with the code, it must be how I uploaded the PHP file. Moreover, I was told that keeping 'echo '<a href="http://www.blabla.com/final.php?id=' . $id . '"></a>';' in the same page of the script would result going back to the same PAGE, which is final.php, and wouldn't make any sense when fetching ID.


I am really confused. Can anybody BUILD another example or a similar example like www.games68.com and how they FETCH their games through ID? I'm beginning to think the script above does not work at all. I just need another form of script that will access my database through ID by link, and fetch a row of information to display Title, SWF flash and Image per ID of a game.

Any help would be appreciated. Thanks.

Member Avatar for diafol

Here's something you could try:

<?php
//include mysql connection details

if(isset($_GET['id'])){
   $id = mysql_real_escape_string($_GET['id']);
}else{
   $id = 234; //default game if none chosen - you can set this to a random no as well- but need to check this against DB.
}

$rs = mysql_query("...query to get all relevant data for game ... WHERE id = $id");
if(mysql_num_rows($rs) > 0){
  $data = mysql_fetch_array($rs);
  echo buildOutput($data);
}else{
  $rs = mysql_query("...query to get first record in table...");
  if(mysql_num_rows($rs) > 0){
     $data = mysql_fetch_array($rs);
     echo buildOutput($data);
  }else{
     echo "Could not load any flash games at this time";
  }
}
?>

The buildOutput function could go something like this:

function buildOutput($d){
  $output = "<object width=\"550\" height=\"400\">\n\t<param name=\"movie\" value=\"{$d['swf_file_location']}\">\n\t<embed src=\"{$d['swf_file_location']}\" width=\"550\" height=\"400\"></embed>\n</object>"; 
  return $output;
}

I haven't used all your sql fields, but that's the approach I'd use.


}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.