Jump to content

newbie question


rghollenbeck

Recommended Posts

Hello,  I'm new to PHP/MySQL.  Here's the situation: I have a simple little database called, "quiz."  (I want to keep it simple until I figure out what I'm doing.)  This database has one user, me, and two tables: Questions (QuestionID, QuestionText), and Answers (QuestionID, OptionText)  QuestionID is set to autoincrement.  I have one record so the biggest QuestionID is 1.

 

Here is the php file:

 

<?php

$link = mysql_connect('PathToMySQL', 'MyUsername', 'MyPassword');

if (!$link) {

  die('Could not connect: ' . mysql_error());

}

echo 'Connected!';

mysql_select_db(quiz);

$sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions;';

 

// everything works great until this point

// but I want the store the data into a variable so the

// answer form will know what QuestionID to assign to the answers.

// So I tried the following:

 

echo $sql;

 

// but that just echoed the query itself, not the result.

// I got: Connected!SELECT "QuestionID, MAX(QuestionID) FROM Questions;"

// I was expecting to see: "Connected!1"

 

// close database

mysql_close($link);

?>

 

:confused:

 

I need to capture the value of QuestionID to pass that to the answers.  How do I do that?

 

Thank you all for this forum, and for any answers.  That PHP manual is thick and complex.  I always hate being a newbie.  I have been in Newbieville for several languages now; this is no different.  I can say from experience that the good news is that it gets better!

 

:D

 

Richard Hollenbeck

Link to comment
Share on other sites

This is the way I prefer to do it:

 

<?php

$sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions;';
$row = mysql_fetch_row($sql)

$result1 = $row[0]; // this would set the result to a variable

// If you wanted to list all results of many, you would use a while loop

 

Hope this helps.

Link to comment
Share on other sites

Thanks for your reply.

 

Trying this gives me an error.  First the code, then the error:

 

mysql_select_db(quiz);

$sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions;';

 

$row = mysql_fetch_row($sql)

$ID=$row[0];

echo $ID;

 

now the error:

Parse error: syntax error, unexpected T_VARIABLE in /(path)/on line 13

 

Line 13 is, "$ID=$row[0];"

 

If I eliminate this and simply  "echo $row[0]" I get this error:

 

"Parse error: syntax error, unexpected T_ECHO in . . ."

Link to comment
Share on other sites

[EDIT:] Yep, I need to learn to type quicker!! Lightbearer got there first!!

 

<?php

$sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions';
$row = mysql_fetch_row($sql);//<missing the semi colon here

$result1 = $row[0]; // this would set the result to a variable

// If you wanted to list all results of many, you would use a while loop

 

Yep, that code had a missing semi-colon on the mysql_query() function.  No need to put a semi colon inside the sql statement either, as php will do this for you!

 

Try it now, and the code that @elmas156 supplied will work..

 

Rw

Link to comment
Share on other sites

That did it!  I'm so new at PHP and MySQL that I didn't even realize that I needed to execute the query.  All my SQL experience is with MS-Access and that doesn't really need to be executed because everything is GUI.  You execute a query by double clicking on the icon. or calling it from a VBA routine.

 

Many thanks for all you your help (from everyone.)  Now I can get started building my forms, etc.  No sense building a fancy interface if the most basic disfunctionality is not first fixed.

 

:rtfm:

 

Time to go open that heavy manual!

Link to comment
Share on other sites

I never even noticed it, though we all structure things differently I suppose, I just looked at the most obvious thing which was the missing colon.

 

You can spend hours looking through the manual, the best way is to learn by mistakes, then reference the manual when something goes wrong that you can't figure out, that's when it becomes interesting -> then it becomes frustrating -> then you want to throw the laptop through the window...

 

Have fun,

Rw

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.