Jump to content

Undefined Index


shadow_soldier

Recommended Posts

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.

Link to comment
Share on other sites

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

 

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.