trying to learn php

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
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

trying to learn php

Post by newtoophp »

I am developing (attempting to develop) a stepped base quiz type php/database webpage.
I have a database setup and can connect to the table in the database. ( That is huge for me :lol: )

Now I want to be able for quiz takers to enter in an password that will load/show a particular quiz question.
So for example, if they have completed a questions, they are given the passsword of the next one that they enter into a text box.

Then when they submit the password correctly they are given the question that corresponds to that identifyer.

My database has several columns in it:
question_id question_pass question_text question_ans


So basically when they enter in a given password they get the correct question, if they answer it correctly they are given the next passoword...

Any hints or ideas would be appreciated:

Thanks


Below is just what I have so far to connect and display one question.....

Code: Select all

//connection check to database and database welcome information
                      mysql_connect("localhost", "user", "password") or die(mysql_error());   
                      echo "Connection to  Server...<br/>";    
                      mysql_select_db("quiztest") or die(mysql_error());   
                      echo "Database Active!<br/>";
                      
                      
                      // Collects data from quiztest  "questions" table data
                    $data = mysql_query("SELECT * FROM questions") 
                    or die(mysql_error()); 
 
                      
                    // puts the "questions" info into the $info array 
                        $info = mysql_fetch_array( $data );
 
                    
                    // Print out the contents of the entry 
                    Print "<br><b>Question1:</b><br> ".$info[question_text'] . " ";
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: trying to learn php

Post by koen.h »

Great you are starting with PHP with something like that. What I recommend is to look at the following page:

http://us3.php.net/manual/en/ref.mysql.php

I know it's the reference manual and it's not an entertaining read. But you'll learn a lot of looking at the few pages that correspond with the functions you are using. The examples are really good to get starting. Eg look at how they query (mysql_query) and how they generate a result set from that.
newtoophp
Forum Newbie
Posts: 6
Joined: Mon Sep 08, 2008 2:53 pm

Re: trying to learn php

Post by newtoophp »

Looked over some of that information Thank You.
I can understand some, but not all. Obviously I am still missing major parts of PHP.

Will I need to do a query then fetch the row type thing...?


Wondering... :?
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: trying to learn php

Post by koen.h »

Create a query first:

$sql = 'SELECT * FROM question';

Then you'll want to execute that query:

$result = mysql_query($sql);

What 'mysql_query($query)' returns is a resource. We can't do much with that so we'll have to do another step: pass it to a function that is designed to create something more useable out of this resource, like mysql_fetch_assoc. It's a good idea to first check if we have any results before we start to use them:

if (!$result) {
// no results
}
else {
// so below
}

If there are results we can proceed with our function mysql_fetch_assoc:

$data = mysql_fetch_assoc($result)

Note that mysql_fetch_assoc() only returns data from one row at once. So if you want all rows you can use a loop:

while ($row = mysql_fetch_assoc($result)) {
$row["question"];
}
Post Reply