Jump to content

MYSQL Integration....


davidknag

Recommended Posts

Okay so i have a flash game website. I want it so that i can list all the games in a category onto one page.

So, php selects the items with the category with mysql(for example 'a for arcade' http://url.com/select.php?cat=a) and then lists every game's name and a link to the url (by grabbing the id of the game and providing a link (http://url.com/play.php?id=1)

 

ex.png

This is the code i have so far.. :P

 

<?php
$con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con));

if(isset($_GET['cat'])){
     $query_var = $_GET['type']; // force it to be an integer

$query=" SELECT * FROM games WHERE category = '$query_var'";

     $result = mysqli_query($con, $query)or die(mysqli_error($con));
     }
}

?>
<html>
<head>
<title>
Test
</title>
</head>
<body>
?
</body>
</html>

Link to comment
Share on other sites

<?php
$con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con));

if(isset($_GET['cat'])){
     $query_var = $_GET['type']; // force it to be an integer

$query=" SELECT * FROM games WHERE category = '$query_var' ORDER BY title";

     $result = mysqli_query($con, $query)or die(mysqli_error($con));
     }
}

?>
<html>
<head>
<title>
Test
</title>
</head>
<body>
<?php
if(mysql_num_rows($result) >0) {
	while($r = mysql_fetch_assoc($result)) {
		echo '<a href="http://url.com/play.php?cat=true&type=' . $query_var . '&id=' . $r['id'] . '" title="' . $r['description'] . '">' . $r['title'] . '</a><br />';
	}
}
else {
	echo 'No games exist in this category!';
}
?>
</body>
</html>

 

Try this, it may need some tweaking.

Link to comment
Share on other sites

<?php
$con = mysqli_connect('localhost', 'root', 'root', 'games')or die(mysqli_error($con));

if(isset($_GET['cat'])){
     $query_var = $_GET['type']; // force it to be an integer

$query=" SELECT * FROM games WHERE category = '$query_var' ORDER BY title";

     $result = mysqli_query($con, $query)or die(mysqli_error($con));
     }
}

?>
<html>
<head>
<title>
Test
</title>
</head>
<body>
<?php
if(mysql_num_rows($result) >0) {
	while($r = mysql_fetch_assoc($result)) {
		echo '<a href="http://url.com/play.php?cat=true&type=' . $query_var . '&id=' . $r['id'] . '" title="' . $r['description'] . '">' . $r['title'] . '</a><br />';
	}
}
else {
	echo 'No games exist in this category!';
}
?>
</body>
</html>

 

Try this, it may need some tweaking.

 

oh, sorry i didn't tell you this before.

 

The urls for the game are all like "url.com/play.php?id=x"  x is the id from mysql

Link to comment
Share on other sites

So the thing is:

 

-Get the cat id

-query with sql to check if the category exists , if it does then echo all the results , if not go to errorpage

-when you click on the game you want to play then check if the id of the game exist , if it does then show the flash game , if not go to errorpage

 

so this is what i came up with:

 

The file that you will get all the results of each category game (select.php)

$dbhost  = 'localhost';
$dbname  = 'root';
$dbuser  = 'root';
$dbpass  = 'games';

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if (isset($_GET['cat'])){
  $cat = $_GET['cat'];
  $get_games = mysql_query("SELECT * FROM games WHERE category = '$cat'");
  $get_games_rows = mysql_num_rows($get_games);
  if ($get_games_rows>=1){
    while ($fetch = mysql_fetch_assoc($get_games)){
        $id = $fetch['id'];
        $title = $fetch['title'];
	echo "<a href='play.php?id=".id."'>Play ".$title."</a><br>";
    }
  }
  else
echo "Category does not exist";
   

}
else
     echo "Category is not set";

 

this one to show the game on play.php

$dbhost  = 'localhost';
$dbname  = 'root';
$dbuser  = 'root';
$dbpass  = 'games';

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if (isset($_GET['id'])){

$game_id = $_GET['id'];
$query = mysql_query("SELECT * FROM games WHERE id = '$game_id'");
$rows = mysql_num_rows($query);
if ($rows>=1){

	while($fetch = mysql_fetch_assoc($query)){
		$swfname = $fetch['swfname'];

	}

	//show the flash game with that name

}
else{

	echo "game does not exist";
}


}
else
echo "Game is not set.";

 

Correct me for any fault.

Link to comment
Share on other sites

Since the OP is already using mysqli, it would probably be a good idea to use mysqli functions for the remainder of the code.

 

Honestly, idc.. whats the difference?

 

So the thing is:

 

-Get the cat id

-query with sql to check if the category exists , if it does then echo all the results , if not go to errorpage

-when you click on the game you want to play then check if the id of the game exist , if it does then show the flash game , if not go to errorpage

 

so this is what i came up with:

 

The file that you will get all the results of each category game (select.php)

$dbhost  = 'localhost';
$dbname  = 'root';
$dbuser  = 'root';
$dbpass  = 'games';

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if (isset($_GET['cat'])){
  $cat = $_GET['cat'];
  $get_games = mysql_query("SELECT * FROM games WHERE category = '$cat'");
  $get_games_rows = mysql_num_rows($get_games);
  if ($get_games_rows>=1){
    while ($fetch = mysql_fetch_assoc($get_games)){
        $id = $fetch['id'];
        $title = $fetch['title'];
	echo "<a href='play.php?id=".id."'>Play ".$title."</a><br>";
    }
  }
  else
echo "Category does not exist";
   

}
else
     echo "Category is not set";

 

this one to show the game on play.php

$dbhost  = 'localhost';
$dbname  = 'root';
$dbuser  = 'root';
$dbpass  = 'games';

$connect = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

if (isset($_GET['id'])){

$game_id = $_GET['id'];
$query = mysql_query("SELECT * FROM games WHERE id = '$game_id'");
$rows = mysql_num_rows($query);
if ($rows>=1){

	while($fetch = mysql_fetch_assoc($query)){
		$swfname = $fetch['swfname'];

	}

	//show the flash game with that name

}
else{

	echo "game does not exist";
}


}
else
echo "Game is not set.";

 

Correct me for any fault.

 

might be errors here:

if ($get_games_rows>=1){

    while ($fetch = mysql_fetch_assoc($get_games)){

        $id = $fetch['id'];

        $title = $fetch['title'];

 

 

 

 

 

echo "<a href='play.php?id=".$id."'>Play ".$title."</a><br>";

 

brings me to:

  www.url/play.php?id=id

literally... id=id

hmmph :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.