Forum Moderators: coopster

Message Too Old, No Replies

Connection problem

         

egibberate

7:46 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi,
beginner question, can anyone point out what is causing the error:-

Warning: mysql_query(): Access denied for user 'ODBC'@'localhost' (using password: NO) in d:\program files\easyphp1-7\www\inputcosts.php on line 64

Warning: mysql_query(): A link to the server could not be established in d:\program files\easyphp1-7\www\inputcosts.php on line 64

these errors are repeated for every line that contains the code:-

$result = mysql_query ($query);

The include file (opendb.php) is;

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'mypassword';
$dbname = 'mydatabase';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

Could it be something to do with the way I have used the $conn variable from the include file in my page?
(I realise my newbie code has no validation but this will be my next target for learning)
Here is the page & thank you for any kind help:-

<HTML>
<BODY>
<h2>Add new record</h2>
<STYLE>
.error {padding: 10px; color: #cc0000; font-weight: bold;}
</STYLE>
<?
// has form been submitted?
if ($_POST) {
$msg = "";
foreach($_POST as $k => $v) {
$v = trim($v) ;
$$k = $v ;
if ($v=="") {
$msg = "please fill in all fields";
}
}
// if all data is there, build the query
$strd = "-";
$dt = $yea.$strd.$mon.$strd.$day;
if ($msg=="") {
$insert = "INSERT INTO spending
(amount, card_number, company, date, category, sub_category)
VALUES ('$am', '$cn', '$com', '$dt', '$cat', '$scat')";
include 'opendb.php';
echo 'Connected successfully';
//execute the query and check for success
if (!mysql_query($insert, $conn)) {
//--------------------------------the $conn variable is here
$msg = "Error inserting data";
} else {
$msg = "Record successfully added";
// set vars to "" for next form input
$am = $cn = $com = $yea = $mon = $day =$cat = $scat = "";
}
}
//print error or success messages
echo "<div class=\"error\">$msg</div>";
// if not submitted, create blank vars for form inputs
} else {
$am = $cn = $com = $yea = $mon = $day = $cat = $scat = "";
}
?>
<FORM METHOD="post" ACTION="<? echo
$_SERVER['PHP_SELF'] ?>">
<!--set up the table-->
<TABLE BORDER="1" CELLPADDING="5">
<TR><TD><a href="http://localhost/domindex.php">Home</a></TD></TR>
<TR>
<TH>amount</TH>
<TH>card_number / telebank</TH>
<TH>company</TH>
<TH>year</TH>
<TH>month</TH>
<TH>day</TH>
<TH>category</TH>
<TH>sub_category</TH>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="am" VALUE="<?
echo $am ?>" /></TD>
<TD>
<?
$query="SELECT card_number,id FROM card_number";
$result = mysql_query ($query);
//-----------------------------------------------error line
echo "<select name='cn'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[card_number]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT company,id FROM company ORDER BY company ASC";
$result = mysql_query ($query);
//------------------------------------------------error line
echo "<select name='com'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[company]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT year FROM years ORDER BY year ASC";
$result = mysql_query ($query);
//-------------------------------------------------error line
echo "<select name='yea'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[year]>$nt[year]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT month FROM months ORDER BY month ASC";
$result = mysql_query ($query);
-----------------------------------------------------error line
echo "<select name='mon'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[month]>$nt[month]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT day FROM days ORDER BY day ASC";
$result = mysql_query ($query);
----------------------------------------------------error line etc. etc.
echo "<select name='day'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[day]>$nt[day]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT category,id FROM category";
$result = mysql_query ($query);
echo "<select name='cat'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[category]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT sub_category,id FROM sub_category";
$result = mysql_query ($query);
echo "<select name='scat'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[sub_category]</option>";
}
echo "</select>";
?>
</TD>
</TR>
<!--Close table-->
</TABLE>
<INPUT TYPE="submit" VALUE="Add to database" />
<INPUT TYPE="reset" VALUE="Cancel" />
</FORM>
</BODY>
</HTML>

eelixduppy

7:51 pm on Nov 25, 2008 (gmt 0)



You are not connecting to the db correctly. Your password isn't correct. Check your db credentials.

egibberate

8:11 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Thank you for your help, however
I can get it to work ok if I add this line at the beginning of the code for each drop down list

$conn = mysql_connect('localhost','root','mypassword');
mysql_select_db('mydatabase');

but I guess this defeats the point of include opendb

therefore this works ok:-

<?
$conn = mysql_connect('localhost','root','mypassword');
mysql_select_db('mydatabase');
$query="SELECT card_number,id FROM card_number";
$result = mysql_query ($query);
echo "<select name='cn'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[card_number]</option>";
}
echo "</select>";
?>

but I guess is not the right way to go about this?

eelixduppy

8:13 pm on Nov 25, 2008 (gmt 0)



Actually, now that I look at it, it looks as though your opendb.php file isn't being included correctly. Make sure you have the correct path to that file.

SarK0Y

8:32 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi, egibberate.
try so:
$conn = mysql_connect('$dbhost', '$dbuser', '$dbpass') or die ('Error connecting to mysql');

egibberate

8:34 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



I'm guessing that you don't mean actually type the path to the file like this in the code for the page?
(Sorry, the manual I'm learning from isn't clear on this)

include 'D:\Program Files\EasyPHP1-7\www\opendb.php';

egibberate

8:46 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi SarKOY,
but this line already exists in my include file opendb

eelixduppy

8:54 pm on Nov 25, 2008 (gmt 0)



Either your include path [us3.php.net] isn't correct or there is something wrong with your path. You can use the full path as you have above, or a relative path. This is the only thing right now I can see that could be going wrong.

egibberate

10:20 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Thanks eelixduppy, I have now found the correct path for include files thanks to the link you supplied & I'm very grateful for it! It works fine if I use the 'include opendb' in two places in the page. Is this the correct way? I've left out the 'include closedb' file till I can get this sorted:-

<HTML>
<BODY>
<h2>Add new record</h2>
<STYLE>
.error {padding: 10px; color: #cc0000; font-weight: bold;}
</STYLE>
<?
// has form been submitted?
if ($_POST) {
$msg = "";
foreach($_POST as $k => $v) {
$v = trim($v) ;
$$k = $v ;
if ($v=="") {
$msg = "please fill in all fields";
}
}
// if all data is there, build the query
$strd = "-";
$dt = $yea.$strd.$mon.$strd.$day;
if ($msg=="") {
$insert = "INSERT INTO costs
(amount, card_number, company, date, category, sub_category)
VALUES ('$am', '$cn', '$com', '$dt', '$cat', '$scat')";
//execute the query and check for success
include 'D:\Program Files\EasyPHP1-7\php\pear\opendb.php';
echo 'Connected successfully';
if (!mysql_query($insert, $conn)) {
$msg = "Error inserting data";
} else {
$msg = "Record successfully added";
// set vars to "" for next form input
$am = $cn = $com = $yea = $mon = $day =$cat = $scat = "";
}
}
//print error or success messages
echo "<div class=\"error\">$msg</div>";
// if not submitted, create blank vars for form inputs
} else {
$am = $cn = $com = $yea = $mon = $day = $cat = $scat = "";
}
?>
<FORM METHOD="post" ACTION="<? echo
$_SERVER['PHP_SELF'] ?>">
<!--set up the table-->
<TABLE BORDER="1" CELLPADDING="5">
<TR><TD><a href="http://localhost/domindex.php">Home</a></TD></TR>
<TR>
<TH>amount</TH>
<TH>card_number / telebank</TH>
<TH>company</TH>
<TH>year</TH>
<TH>month</TH>
<TH>day</TH>
<TH>category</TH>
<TH>sub_category</TH>
</TR>
<TR>
<TD><INPUT TYPE="text" NAME="am" VALUE="<?
echo $am ?>" /></TD>
<TD>
<? include 'D:\Program Files\EasyPHP1-7\php\pear\opendb.php';
$query="SELECT card_number,id FROM card_number";
$result = mysql_query ($query);
echo "<select name='cn'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[card_number]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT company,id FROM company ORDER BY company ASC";
$result = mysql_query ($query);
echo "<select name='com'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[company]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT year FROM years ORDER BY year ASC";
$result = mysql_query ($query);
echo "<select name='yea'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[year]>$nt[year]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT month FROM months ORDER BY month ASC";
$result = mysql_query ($query);
echo "<select name='mon'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[month]>$nt[month]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT day FROM days ORDER BY day ASC";
$result = mysql_query ($query);
echo "<select name='day'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[day]>$nt[day]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT category,id FROM category";
$result = mysql_query ($query);
echo "<select name='cat'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[category]</option>";
}
echo "</select>";
?>
</TD>
<TD>
<?
$query="SELECT sub_category,id FROM sub_category";
$result = mysql_query ($query);
echo "<select name='scat'>
<option value=''></option>";
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[sub_category]</option>";
}
echo "</select>";
?>
</TD>
</TR>
<!--Close table-->
</TABLE>
<INPUT TYPE="submit" VALUE="Add to database" />
<INPUT TYPE="reset" VALUE="Cancel" />
</FORM>
</BODY>
</HTML>

eelixduppy

5:19 am on Nov 26, 2008 (gmt 0)



Take both includes out from where they are and put them at the top of the script. It seems like they are both within conditionals and therefore may or may not be included depending on other conditions in the script.

egibberate

8:45 pm on Nov 26, 2008 (gmt 0)

10+ Year Member



That fixed it eelixduppy! Thank you so much for getting me there. I very much appreciate your time & effort, much obliged.