Page 1 of 2

how to make 'games.php?id='

Posted: Sat Apr 09, 2011 6:08 pm
by Netroxy
Hello friends. I am trying to build a game website and I am taking a look at other game websites on how they get users who visit their websites to access their games. For example, one game website would get users to click on a link to access a game information, for example 'http://www.blabla.com/games.php?id=12345'. So you click on that link, and what displays is the Name of the Game, the Image Icon of the game, the SWF file of the game, and the Date the game was posted on. All this information is displayed under one page, that is, games.php. They use some kind of code in games.php to extract the ID of a certain row in the mysql database to fetch the details and display them in the games.php page. You can tell because you see 'games.php?id=12345'. It seems like they programmed in PHP to fetch information based on ID number in the link in the address bar, and the rest of the information from the ID number would be displayed in games.php. Moreover, I thought of building a database of my own with the table called 'games' and putting my own information such as the information below:

fldID || fldGameName || fldGameIcon || fldSwfFile || fldNumPlays ||
===================================================================================================================================================================
34356 || Ultimate Sonic Game || http://www.bla.com/games/images/ultimsonic.gif || http://www.bla.com/games/swf/ultimsonic.swf || 0 ||
45789 || Mouse Game || http://www.bla.com/games/images/mousegame.gif || http://www.bla.com/games/swf/mousegame.swf || 0
21356 || Tetris || http://www.bla.com/games/images/tetris.gif || http://www.bla.com/games/swf/tetris.swf || 0 ||

Where:
fldID = ID of the game
fldGameName = The name of the game
fldGameIcon = The image icon of the game in .gif or .jpeg
fldSwfFile = The SWF file executed for users to play
fldNumPlays = A counter to add up the number of users that played the game

Anyhow, this is an example of the database I made on phpMyAdmin. For each game, I set their information by ID, GameName, GameIcon, SwfFile and Number of Plays (a counter to show how many people played each game). My problem is, I DONT KNOW HOW TO RETREVIE this information inside my games.php page. I don't even know how to get ID number with my games.php and turn it into games.php?id=12345. Is anybody familiarized on how to retrieve data through ID by link?. For example making my website retrieve game data like this 'http://www.blabla.com/games.php?id=12345' and displaying the name of the Game, displaying the Image Icon of that game, and executing the SWF flash file of the game, and displaying counter just by accessing ID through the link?

Thanks.

Re: how to make 'games.php?id='

Posted: Sat Apr 09, 2011 7:15 pm
by mecha_godzilla
Capturing a value from a URL like

http://www.mydomain.com/games.php?id_1234

is as easy as

Code: Select all

$game_id = $_GET['id'];
though bear in mind that you need to validate that value before you insert it into a database query (of which more in a moment...)

To use your value in a MySQL query you then just need to do this:

Code: Select all

SELECT * FROM games WHERE id = '$game_id' LIMIT 1;
As mentioned, you need to validate the value captured from the URL to make sure that it's in the right format and isn't being used to try and break your query or compromise your script. If you're just starting out, make sure you take a look at mysql_real_escape_string and other functions like is_numeric to learn how to sanitise and validate these values.

If you're working the other way - generating the URLs - then you need to retrieve all your game IDs from the database, loop through them and then output a link containing the ID. As an example:

Code: Select all

$sql = "SELECT id, title FROM games ORDER BY title ASC";
$result = mysql_query($sql, $conn) or die(mysql_error());
$number_of_results = mysql_num_rows($result);

if ($number_of_results != 0) {

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

$id = $result_array['id'];
$title = $result_array['title'];

echo '<a href="http://www.mydomain.com/games.php?id=' . $id . '">' . $title . '</a><br />';

}

} else {

echo 'Sorry, no games were found!';

}
Also, when you're creating your database values don't include the full file path - you can generate this from your script and you only have to update it once for all the file paths to be correct.

HTH,

Mecha Godzilla

Re: how to make 'games.php?id='

Posted: Sat Apr 09, 2011 7:47 pm
by Netroxy
Is it possible to put all that into the brackets starting with <?php and ending in ?>. I'm really sorry I'm totally new with PHP. I realized that making separate pages for each game would consume so much time and a waste of hard disk space so I have no choice but to learn PHP and mysql from experienced people. Im understanding the code your writing though, as I have seen a few examples but not similar to what you've written. So after fetching ID information, I would assume that you placed '$id = $result_array['id'];', and '$title = $result_array['title'];' to retrieve ID and Title correct? Would this also apply to retrieving a SWF link to be executed into a flash box to play the game? Like '$swf = $result_array['swf'];', and that placed inside the HTML code of the embedded source of HTML script?


I can draw a little picture for better understanding:

Webpage link would look like this:
==============================
http://www.blabla.com/games.php?id_1234

After fetching information from ID from the link, result would show up in games.php as:


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


The result of this page is due to the ID that was found by the link 'games.php?id_1234, and the fldGameName was placed in the Text field, fldSwfFile is executed in the Flash box, and fldImageIcon is displayed under neath the flash game.

From what you wrote, I am understanding that after retrieving the ID from the link at games.php?id_1234, the information should be brought out to the screen. How do you import those information into HTML in order to be displayed as the example I wrote above? Can the SWF link found in fldSwfFile from the database be executed within the HTML script like:

<object width="550" height="400">
<param name="movie" value="$swf = $result_array['swf']">
<embed src="$swf = $result_array['swf']" width="550" height="400">
</embed>
</object>

As you can see I placed a similar script you wrote into the embedded flash source. I would assume this is completely wrong.

Re: how to make 'games.php?id='

Posted: Sat Apr 09, 2011 8:13 pm
by mecha_godzilla
Ok...here's the PHP:

Code: Select all

<?php
$game_id = $_GET['id'];

$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);

if ($number_of_results != 0) {

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

$id = $result_array['id'];
$title = $result_array['title'];
$graphic = $result_array['graphic']; // Example: game1234.png
$swf = $result_array['swf']; // Example: game1234.swf

$graphic_file_location = 'http://www.mydomain.com/graphics/' . $graphic;
$swf_file_location = 'http://www.mydomain.com/swf/' .$swf;

echo '<h1>' . $title . '</h1>';
echo '<img src="' . $graphic_file_location . '" width="50" height="50" border="0" />';
echo '<br />';
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>';

} else {
echo 'Game not found!';
}

?>
There are easier ways to output HTML but start with the basics first :) Going with a database-driven solution is the only way to keep projects like yours manageable so this is definitely the way to go.

One final point, make sure you understand about single and double quotes - when you declare something in single quotes the value won't be interpreted, whereas in double quotes it will. So when we declare

Code: Select all

$sql = "SELECT * FROM games WHERE id='$game_id' LIMIT 1";
PHP converts this (or "expands" it) into

Code: Select all

$sql = "SELECT * FROM games WHERE id='1234' LIMIT 1";
Two other points:

It's good practice to surround any values you send in a query string with single quotes even if the string works without it, so

Code: Select all

SELECT * FROM games WHERE id='1234' LIMIT 1

SELECT * FROM games WHERE id=1234 LIMIT 1
will both work and are functionally the same but with

Code: Select all

SELECT * FROM games WHERE id='Krustys Super Funhouse' LIMIT 1

SELECT * FROM games WHERE id=Krustys Super Funhouse LIMIT 1
the second query will give you a spurious error because MySQL doesn't think that "Super" and "Funhouse" are valid query statements.

The reason why I echo out the HTML code like I have done is because HTML attributes are declared using double quotes and this causes problems when you're trying to output them from PHP. Compare the following two lines of code to see how this is done with single and double quotes and decide for yourself which one would be easier to maintain:

Code: Select all

echo '<img src="' . $graphic_file_location . '" width="50" height="50" border="0" />';

echo "<img src=\"$graphic_file_location\" width=\"50\" height=\"50\" border=\"0\" />";
I should add that there's no right or wrong answer here, but I prefer the first approach because it's so much easier to see what's going on if your text editor supports code highlighting (as the DevNetwork Forums code lister does).

HTH,

M_G

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 7:51 am
by Netroxy
Wow I can finally reply. I apologies for not responding sooner as it seemed like DevNetwork had a problem posting my response to you. It kept showing errors.

Say for example a user clicks on a link such as the following:

'http://www.mydomain.com/games.php?id_12345'
'http://www.mydomain.com/games.php?id_43256'
'http://www.mydomain.com/games.php?id_48938'

Will these work if a user clicks or randomly types these link addresses into the link bar?

From what I am understanding, is that you made some kind of connection with all fields inside the database table using '$result_array'. Does this array somehow identify the ID from the link and automatically fetch the rest of the Information from that particular ID in the link? Also, on the last post, you didn't mention 'echo '<a href="http://www.blabla.com/games.php?id=' . $id . '">'</a><br />';'. I need this line in order to make the ID's work in the link correct?

This is the script I executed as I have added the connection 'host, username, password'. Would this be correct? I also added '<a href="http://www.blabla.com/games.php?id=' . $id . '">'</a><br />';'' with the rest of the echos bellow:

Code: Select all

<?php 
mysql_connect("host", "username", "password") or die
mysql_select_db("games") or die(mysql_error()); 
$game_id = $_GET['id'];
$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);

if ($number_of_results != 0) {

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

$id = $result_array['fldID'];
$title = $result_array['fldGameName'];
$graphic = $result_array['fldImageIcon']; // Example: game1234.png
$swf = $result_array['fldSwfSource']; // Example: game1234.swf

$graphic_file_location = 'http://www.blabla.com/games/icons' . $graphic;
$swf_file_location = 'http://www.blabla.com/swf/' .$swf;


echo '<a href="http://www.blabla.com/games.php?id=' . $id . '">'</a>
';
echo '<h1>' . $title . '</h1>';
echo '<img src="' . $graphic_file_location . '" width="50" height="50" border="0" />';
echo '
';
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>';

} else {
echo 'Game not found!';
}
?>

?>
Hence receiving error around the last lines which is 'else {echo 'Game not found!';'

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 3:27 pm
by mecha_godzilla
In answer to your questions:

1. The links will still work if the user clicks on them in your page or types them/bookmarks them in their browser. This facility also allows search engines to index your pages based on these addresses.

2. The $result_array value holds whatever information is returned by your database query and works exactly like an associative array. If you want to see what the format of the array is you can use vardump()

Code: Select all

vardump($result_array);
The only reason the result array returns the right values is because you've specified it in the database query based on the value you captured in the $_GET code.

3. I didn't include the code to generate all the links in my example because I assumed that you would be using this on a different page.

4. The code that you've posted has a problem with the syntax - where you have this section of code

Code: Select all

echo '<a href="http://www.blabla.com/games.php?id=' . $id . '">'</a>
';
echo '<h1>' . $title . '</h1>';
echo '<img src="' . $graphic_file_location . '" width="50" height="50" border="0" />';
echo '
';
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>';
you need to change it to

Code: Select all

echo '<a href="http://www.blabla.com/games.php?id=' . $id . '"></a>';
echo '<h1>' . $title . '</h1>';
echo '<img src="' . $graphic_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>';
because you have too many single quotes in there. If you use a text editor with highlighting (this could be Context for Windows or gedit for Linux) you would be able to see if there are any problems with the code.

5. You need to change the format for the database query to make it work with the example code I posted - where you have this line

Code: Select all

mysql_connect("host", "username", "password") or die
change it to

Code: Select all

$conn = mysql_connect("host", "username", "password");
HTH,

M_G

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 3:56 pm
by Netroxy
I received error: Parse error: syntax error, unexpected T_STRING on line 3

=============================
<?php
$conn = mysql_connect("host", "username", "password")
mysql_select_db("games") or die(mysql_error());
$game_id = $_GET['id'];
$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);
if ($number_of_results != 0) {
while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $result_array['fldID'];
$title = $result_array['fldTitle'];
$iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
$swf = $result_array['fldSwfSrc']; // 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>'

} else {
echo 'Game not found!';
}
?>
=============================

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 4:21 pm
by mecha_godzilla
The first line in your code is missing a semi-colon - you need to change

$conn = mysql_connect("host", "username", "password")

to

$conn = mysql_connect("host", "username", "password");

HTH,

M_G

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 4:24 pm
by Netroxy
Semicolon worked. I now have Parse error: syntax error, unexpected T_STRING on on line 9

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 4:34 pm
by mecha_godzilla
Your code was missing another semi-colon, and the while() loop wasn't closed properly (you were missing a curly brace). Here is the revised code:

Code: Select all

<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games") or die(mysql_error());
$game_id = $_GET['id'];
$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);

if ($number_of_results != 0) {

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

        $id = $result_array['fldID'];
        $title = $result_array['fldTitle'];
        $iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
        $swf = $result_array['fldSwfSrc']; // 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>';
        
    }

} else {
    echo 'Game not found!';
}
?>
HTH,

M_G

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 4:44 pm
by Netroxy
I am still receiving syntax error STRING on Line 9. I'm looking at it and you did close all 4 brackets correctly on that line. I'm not sure what the problem in line 9 is.

======================
======================
<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games") or die(mysql_error());
$game_id = $_GET['id'];
$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);

if ($number_of_results != 0) {

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

$id = $result_array['fldID'];
$title = $result_array['fldTitle'];
$iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
$swf = $result_array['fldSwfSrc']; // 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>';

}

} else {
echo 'Game not found!';
}
?>

======================
======================

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 5:04 pm
by mecha_godzilla
I don't think you can use die() with mysql_select_db() - although I might be wrong. I've removed the if() test from the following code and amended the line with the mysql_select_db() code in it:

Code: Select all

<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games");
$game_id = $_GET['id'];
$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['fldID'];
    $title = $result_array['fldTitle'];
    $iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
    $swf = $result_array['fldSwfSrc']; // 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>';
        
}
?>
If this doesn't work, try commenting out sections of the code to see what happens, like this:

Code: Select all

<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games");
$game_id = $_GET['id'];
$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['fldID'];
    $title = $result_array['fldTitle'];
    $iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
    $swf = $result_array['fldSwfSrc']; // 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>';

*/
        
}
?>
Remember that you can't nest /* */ inside each other. If you still get an error then the problem is not to do with the commented-out code.

HTH,

M_G

Re: how to make 'games.php?id='

Posted: Sun Apr 10, 2011 8:45 pm
by Netroxy
Since my GET is game_id and not id, shouldn't it be this?

http://www.blabla.com/games.php?game_id=12345

Re: how to make 'games.php?id='

Posted: Mon Apr 11, 2011 3:09 pm
by mecha_godzilla
You can use game_id in your address if you want but it doesn't really matter - you might prefer to just use

http://www.blabla.com/games.php?id=12345

and then capture it using

$game_id = $_GET['id'];

to keep the URL short. You could also use

$game_id = $_GET['game_id'];

if you want your address to look like

http://www.blabla.com/games.php?game_id=12345

As long as the value in the $_GET statement matches the value you're using in your address, it doesn't matter what the name of the variable that you assign it to is.

HTH,

M_G

Re: how to make 'games.php?id='

Posted: Mon Apr 11, 2011 5:54 pm
by Netroxy
I am beginning to think that the code is correct, but the way I am uploading the PHP is wrong.

Should this entire script be written in my games.php? Or do I need 2 separate pages, one page to identify ID from URL, and another page to bring out the ID's results? I had another expert read up on the code, and he said if I keep ' echo '<a href="http://www.blabla.com/final.php?id=' . $id . '"></a>';' inside the script, it will only return to the same page, 'games.php' and it makes no sense.
==============================
==============================

Code: Select all

<?php
$conn = mysql_connect("host", "username", "password");
mysql_select_db("games");
$game_id = $_GET['id'];
$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['fldID'];
$title = $result_array['fldTitle'];
$iconsmall = $result_array['fldIconSmall']; // Example: game1234.png
$swf = $result_array['fldSwfSrc']; // 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/games.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>';
}
?>
==============================
==============================