Newbie Help

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
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Newbie Help

Post by NeoPuma »

Hi there,
I'm really really new to PHP and seriously want to learn, so I was wondering if you guys could help me. I want a simple, connect to db, get info, display info, close db connection, kinda thing.

For example, using the really purpose:
LIST OF CHEATS
A
B
C
D
...
M
N
O

Goto Page M (On m.php - all the games in the database begining with 'M' Will be loaded alphabetically and shown in a table (as in a table not in MySQL table).
goto Page m.php?MarioSuperKart (I don't even know how to do that to be honest :(. On here, All cheats in the Mario Super Kart section in the MySQL is loaded and shown. For example:
Speed Up
Pause Game and press: A, A, B, Y
New Characters
On Menu and press: Y, Y, Y, B)

Any help will be great for a n00bie like me!

-Neo
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post by Illusionist »

I would suggest to start witht he basics. Look over some tutorials and try a few thigns, and then if you need help, come back and ask questions.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

http://www.php.net/mysql is a good start. shows you how to connect to mysql with php, and how to retrieve data..

the MySQL Manual (both on their site and the manual included with your distro ) is also another nice place.

hotscripts.com has some scripts that should give you an understanding of how such scripts are made...

and finally, might want to invest in a book ( if you are a book learner ).

hope this helps. welcome to php
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

Ok, Well looking at a sample code seems all pretty easy, the samples bellow:

Code: Select all

<?php
/* Connecting, selecting database */
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
   or die("Could not connect : " . mysql_error());
echo "Connected successfully";
mysql_select_db("my_database") or die("Could not select database");

/* Performing SQL query */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) &#123;
   echo "\t<tr>\n";
   foreach ($line as $col_value) &#123;
       echo "\t\t<td>$col_value</td>\n";
   &#125;
   echo "\t</tr>\n";
&#125;
echo "</table>\n";

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
?>
The Thing i dont get is:

Code: Select all

/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) &#123;
   echo "\t<tr>\n";
   foreach ($line as $col_value) &#123;
       echo "\t\t<td>$col_value</td>\n";
   &#125;
   echo "\t</tr>\n";
&#125;
echo "</table>\n";
And what would

Code: Select all

/* Free resultset */
mysql_free_result($result);
do?

-Neo
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

Ok, never mind that now but ty any one who looked :P

Now I need help with this:
DB = themn
Table = tbl_nintendo
Table2= tbl_xbox
Table3= tbl_ps2

In each table there would be parts (A, B, C, D, E etc)
If they selected E and there was no data, how could I put an error? And if there was more data (game names) in there, and they selected one, how would it get the cheats for that game (Your really deep into the db now).

Once I know this part - I've completed what I wanted to do :)

-Neo
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post by HaVoC »

This is what I would do. As inefficient as this is, I would do it.
Make a table for say N64.

So console_n64 was the table name.
The fields are id,letter,name,cheats

Make the ID field auto_increment for counting purposes. It is always good to know how many cheats you have.
Ok so. Say you put in the game Harvest Moon.

ID ---- LETTER------NAME ----------------Cheats
1 ---- H ----------- Harvest Moon 64 --- BLAH BLAH BLAH

Ok so then you would have something like this.

<a href="n64.php?letter=H">H</a> Then on this H page you do this.

Code: Select all

<?php
$query = "SELECT * FROM console_n64 WHERE letter = '.$_GET['letter'].' ORDER BY id DESC LIMIT 0, 50"; 


$result = MySQL_query($query);
While( $rows = MySQL_fetch_assoc($result) ) {
$name = $rows['name'];
echo "<a href="n64game.php?game={$name}">{$name}</a>\n";
}
?>
Now that code would list all the games with the letter chosen in the n64 table. On the actual page to list the cheats you would do the same thing but the query changes from
WHERE letter = '.$_GET['letter'].'
to
WHERE name = '.$_GET['name'].'

and you would add $cheats = $rows['cheats']; after the $name declaration. And you would take out the link and put
echo $name;
echo "<br>{$cheats}\n";

<b>Please note there's most likely errors :(</b>

Another thing. If you're using phpMyAdmin, an idea you could do to learn about queries is to click the sql tab and have some fun. Learn how to select a specific game using SQL code. Trust me. It pays off :roll:
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

ty - I'll give it a go!

-Neo
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

Ok, trying to re-create the tables now (like you said), And I get this:

SQL-query :

CREATE TABLE `tbl_cheats_gc` (`ID` INT NOT NULL AUTO_INCREMENT, `Letter` TEXT NOT NULL, `Game` TEXT NOT NULL, `Cheat` TEXT NOT NULL)

MySQL said:


Incorrect table definition; There can only be one auto column and it must be defined as a key

What does the error mean exactly and how can i fix it (sorry I'm really new :P)?

-Neo
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

ok forget that, now what about this (sorry!)
What about counting how many H's are in the Letter colum?

-Neo
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post by HaVoC »

Whenever you add a game to the database the id field auto_increments by 1. It'll count for you.
NeoPuma
Forum Commoner
Posts: 26
Joined: Sat Mar 06, 2004 12:18 pm
Location: South Wales, UK
Contact:

Post by NeoPuma »

Ye ok, but thats the ammount of cheats, on my site I got:
Letter Number of cheats
A        EMPTY

where it says empty, I want the PHP to check how many A's are in the Letter field, and then display the number there

-Neo
User avatar
HaVoC
Forum Commoner
Posts: 83
Joined: Sat Feb 07, 2004 7:20 am
Location: Smiths Falls, CA

Post by HaVoC »

To do that, do this lol

Code: Select all

<?php
$a = "SELECT * FROM console_n64 WHERE letter = a;

$result = MySQL_query($a);
$number = 0;
While( $rows = MySQL_fetch_assoc($result) ) { 
$number = $number + 1;
echo "A [ {$number} ]\n";
} 
?>
I'm not entirely sure that would work. What $number does is it increases by 1 for every record that starts with a. Coding isn't good without an imagination and common sense. So think outside the box.
Post Reply