Page 1 of 1
PHP & MySQL
Posted: Sat Apr 11, 2009 1:46 pm
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.
Re: PHP & MySQL
Posted: Sat Apr 11, 2009 1:57 pm
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 -
will print your title.
Hope that helps
Re: PHP & MySQL
Posted: Sat Apr 11, 2009 6:25 pm
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.
Re: PHP & MySQL
Posted: Sat Apr 11, 2009 6:35 pm
by McInfo
http://www.php.net/manual/en/mysql.examples-basic.php
Edit: This post was recovered from search engine cache.
Re: PHP & MySQL
Posted: Sun Apr 12, 2009 6:37 pm
by deejay
what code have you written and what error are you getting?
Re: PHP & MySQL
Posted: Mon Apr 13, 2009 9:31 am
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);
?>
Re: PHP & MySQL
Posted: Mon Apr 13, 2009 1:49 pm
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.