PHP & MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

PHP & MySQL

Post by tomsace »

Hi,
I have started my own games website. I have created the design etc... Now I have come to the point where I need to use MySQL.
I have created a database which is at 'tomsace_games' and then the game details are stored under the table 'games'.
I can connect to the database, I can insert into the database but now I cannot figure out how to use my details I have stored.

Please could anyone give me an example of using the following details:
id, title, description, instructions, day, month, year, category.

I just cant make them show up on my website. I use a unique ID to keep track of the games so the ID is just a single number.
All the rest are just game details etc..

All I want is to visible see the results shown on a blank web page.

Thanks alot.
Tom.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: PHP & MySQL

Post by deejay »

Have you already made a call to the database. So therefore have these details store in a $row.
By this I mean you'll have something like this

Code: Select all

 
$query = "SELECT * FROM yourDataTable ";
$rst = mysql_query(query);
 
 while( $row = mysql_fetch_array($rst)){
 
 
then you can just dip in to your $row as an array and use your details like this -

Code: Select all

 
echo $row['title'];
will print your title.

Hope that helps
Last edited by deejay on Sun Apr 12, 2009 6:36 pm, edited 1 time in total.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP & MySQL

Post by tomsace »

Hey,
Thanks alot for the help, but I can't get my connection to the db to work properly with this.
I need to connect to the database 'tomsace_games' and use the table 'games'.
Sorry to sound like a newbie but I'm not good with php.

Tom.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP & MySQL

Post by McInfo »

http://www.php.net/manual/en/mysql.examples-basic.php

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Mon Jun 14, 2010 12:57 pm, edited 1 time in total.
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: PHP & MySQL

Post by deejay »

what code have you written and what error are you getting?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: PHP & MySQL

Post by tomsace »

Hi, I managed to connect to the database nicely and display mysql records where I want them.

But now I need to know how to manage my results properly.
I have my results set up inside a table and after every say 7 or 8 results shown I would need a </tr><tr> break between them. What would be the best way about doing this?

This is my code I currently have after connecting to the database:

Code: Select all

// Performing SQL query
$query = 'SELECT * FROM games';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
 
// Printing results in HTML
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
 
        echo "<center><table cellpadding=0 cellspacing=0 border=0 width=85>
              <tr><td><img src=images/redthumbtop.gif></td></tr><tr><td align=center bgcolor=#be1313>";
        echo "<a href=games/$line[idname].php><img src=games/image/$line[idname].jpg border=0></a></td></tr>
              <tr><td align=center bgcolor=#be1313><a href=games/$line[idname].php>";
        echo $line['title']."\n</a>";
        echo "</td></tr><tr><td><img src=images/redthumbbot.gif></td></tr></table>";
 
}
 
// Closing connection
mysql_close($link);
?>
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP & MySQL

Post by McInfo »

How about something like this?

table.php

Code: Select all

<html>
<head><title>Games Table</title></head>
<body>
 
<?php
// Connects to the database ($db)
require_once 'db_connect.php';
 
// Gets a list of fields from the games table
$fields = array();
if ($result = mysql_query('EXPLAIN `games`', $db)) {
    while ($row = mysql_fetch_assoc($result)) {
        $fields[] = $row['Field'];
    }
}
 
// Gets a list of games data from the games table
$games = array();
if ($result = mysql_query('SELECT * FROM `games`', $db)) {
    while ($row = mysql_fetch_assoc($result)) {
        $games[] = $row;
    }
}
 
/*
 * The extra div centers the table for Internet Explorer.
 * The table style centers the table for Firefox
 * and restores the text alignment.
 */
?>
<div style="text-align: center;">
    <table cellspacing="0" cellpadding="4" border="1"
           style="margin: 0 auto; text-align: left;">
 
<?php
if (0 < count($fields)) {
    // Echos a table row for field names
    echo "\t\t<tr>\n";
    foreach ($fields as $field) {
        // Echos a heading for each field
        echo "\t\t\t<th>$field</th>\n";
    }
    echo "\t\t</tr>\n";
    if (0 < count($games)) {
        foreach ($games as $game) {
            // Echos a table row for each game
            echo "\t\t<tr>\n";
            foreach ($fields as $field) {
                // Echos a cell for each game field
                echo "\t\t\t<td>$game[$field]</td>\n";
            }
            echo "\t\t</tr>\n";
        }
    }
}
?>
 
    </table>
</div>
 
</body>
</html>
db_connect.php

Code: Select all

<?php
// Comment the next line when you have changed the settings
die('Please modify the settings in db_connect.php to match your server.');
 
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
 
define('DB_NAME', 'test_games_site');
 
$db = mysql_pconnect(DB_HOST, DB_USER, DB_PASS)
    or die('Could not connect to MySQL server.');
 
mysql_select_db(DB_NAME)
    or die('Could not connect to MySQL database.');
?>
Edit: This post was recovered from search engine cache.
Post Reply