rghollenbeck Posted October 25, 2010 Share Posted October 25, 2010 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); ?> 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! Richard Hollenbeck Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/ Share on other sites More sharing options...
elmas156 Posted October 25, 2010 Share Posted October 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126320 Share on other sites More sharing options...
rghollenbeck Posted October 25, 2010 Author Share Posted October 25, 2010 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 . . ." Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126341 Share on other sites More sharing options...
litebearer Posted October 25, 2010 Share Posted October 25, 2010 a missing semi-colon $row = mysql_fetch_row($sql); Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126345 Share on other sites More sharing options...
rwwd Posted October 25, 2010 Share Posted October 25, 2010 [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 Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126346 Share on other sites More sharing options...
rghollenbeck Posted October 25, 2010 Author Share Posted October 25, 2010 I originally did try that. I noticed the missing semicolon and put it in. But that produced this error: "Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in . . ." :'( Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126350 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2010 Share Posted October 25, 2010 Nowhere in the code do you actually execute the query. mysql_select_db(quiz); $sql = 'SELECT QuestionID, MAX(QuestionID) FROM Questions'; $result = mysql_query($sql); // executes the query $row = mysql_fetch_row($result); $ID=$row[0]; echo $ID; Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126368 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2010 Share Posted October 25, 2010 I would also recommend some quotes around 'quiz' in your mysql_select_db() so that php doesn't waste time trying to find a defined constant by that name, then trying it as a string. Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126373 Share on other sites More sharing options...
rghollenbeck Posted October 25, 2010 Author Share Posted October 25, 2010 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. Time to go open that heavy manual! Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126382 Share on other sites More sharing options...
rwwd Posted October 25, 2010 Share Posted October 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216805-newbie-question/#findComment-1126389 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.