shadow_soldier Posted March 27, 2008 Share Posted March 27, 2008 I'm trying to make a documentation area for my site and to get the data from a MySQL database I use the following function: function getDocument($docname) { // connect to database mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("clientarea") or die(mysql_error()); // get data $sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'"); $gotdoc = mysql_fetch_assoc($sql); // echo result echo($gotdoc[$docname]); // close db con mysql_close(mysql_connect("localhost", "root", "root")); } However, when I run the script by running <? getDocument('testdoc'); ?> I get this error: Notice: Undefined index: testdoc in /Applications/MAMP/htdocs/projects/Clients/modules/system/areacore.php on line 105 However, another similar function works correctly: function getData($data) { // cookie info $aca = $_COOKIE['aca']; $acap = $_COOKIE['acap']; // connect to database mysql_connect("localhost", "root", "root") or die(mysql_error()); mysql_select_db("clientarea") or die(mysql_error()); // get data $sql = mysql_query("SELECT $data FROM users WHERE username='$aca'"); $gotinfo = mysql_fetch_assoc($sql); // echo result echo($gotinfo[$data]); mysql_close(mysql_connect("localhost", "root", "root")); } I've checked all the MySQL details and other things that could go wrong. Also, if I disable error reporting in php.ini, it just doesnt work at all. Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 can we see which line is 105 Unidentified indexes errors come from attempting to use a variable before its deceleration. So odd are you have getDoc($_POST['testdoc']) or something and the value testdoc isn't being passed via post. Quote Link to comment Share on other sites More sharing options...
shadow_soldier Posted March 27, 2008 Author Share Posted March 27, 2008 This is line 105: echo($gotdoc[$docid]); in the getDocument() function. I'm not passing the variable for getDocument() using $_POST. Should I be? Like I said, it works fine for getData(). EDIT: Sorry made a typo. Line 105 is this: echo($gotdoc[$docname]); Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 what it is saying is your array of $gotdoc is lacking the index called "testdoc" which is the value of the variable $docname. its exactly as i said it is just your array is $_POST/GET but an array you created. Quote Link to comment Share on other sites More sharing options...
shadow_soldier Posted March 27, 2008 Author Share Posted March 27, 2008 I understand the problem now, but I'm not sure how to fix it; what would be the best course of action? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 I understand the problem now, but I'm not sure how to fix it; what would be the best course of action? make sure you are using a defined index in your array before trying to call it since the error is in calling the function it be advantageous to see how u called it. Quote Link to comment Share on other sites More sharing options...
shadow_soldier Posted March 27, 2008 Author Share Posted March 27, 2008 I'm not using any arrays ??? I just called the function like this (areacore.php is included above): <? getDocument('testdoc'); ?> My other functions are all called in the same way. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted March 27, 2008 Share Posted March 27, 2008 well your error is on 105 and what u said is on 105 is echoing out part of an array so you are lying to me. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 Your problem comes from these lines: <?php $sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'"); $gotdoc = mysql_fetch_assoc($sql); // echo result echo($gotdoc[$docname]); ?> In the select statement you are saying to get the field "doc", but you are trying to get what ever is passed to the function in the variable $docname, if that's not "doc", you have a problem. Change the select statement to: <?php $sql = mysql_query("SELECT $docname FROM spprtdocs WHERE docname='$docname'"); ?> Which doesn't really make sense, or change the whole block to <?php $sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'"); $gotdoc = mysql_fetch_assoc($sql); // echo result echo($gotdoc['doc']); ?> Ken Quote Link to comment Share on other sites More sharing options...
shadow_soldier Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks kenrbnsn, that worked perfectly Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 27, 2008 Share Posted March 27, 2008 Which solution worked? Ken Quote Link to comment Share on other sites More sharing options...
shadow_soldier Posted March 27, 2008 Author Share Posted March 27, 2008 This one: <?php $sql = mysql_query("SELECT doc FROM spprtdocs WHERE docname='$docname'"); $gotdoc = mysql_fetch_assoc($sql); // echo result echo($gotdoc['doc']); ?> Thanks again Quote Link to comment 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.