Web Hosting Talk







View Full Version : MySQL Problem - PLESE HELP!


HostFX-UK
01-28-2009, 12:17 AM
I am getting an error for this part of my code.
Can you tell me is there anything wrong with it?
The database is setup correctly as I have used it with my other code, this is an upgrade.
THIS IS THE ERROR I GET:
Warning: mysql_query() [http://www.mysql.com/doc]: Query was empty in /home/hostfxc/public_html/programming/sean/activate.php on line 34
Query was empty
Please help, below is the code:
include "includes/config.php";
$dblink = mysql_connect($sqlhost,$sqluser,$sqlpassword);
mysql_select_db($sqldb,$dblink);
$result = mysql_query($query) or die(mysql_error());
$query = mysql_query ("SELECT * FROM users WHERE username='$_POST[usrnme]'");
$result = mysql_fetch_array($result) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
{
echo $result['password'];
}
mysql_close($dblink);
}
Thanks

Core4Tech
01-28-2009, 12:26 AM
It's been a while since I've dealt with PHP, however swap order of the following lines....
$result = mysql_query($query) or die(mysql_error());
$query = mysql_query ("SELECT * FROM users WHERE username='$_POST[usrnme]'");
You are asking for the results of $query before you define it. I could be wrong, but I'm not thinking so.
Hope this helps.

HostFX-UK
01-28-2009, 12:33 AM
I then got this error:
Warning: mysql_query() [http://www.mysql.com/doc]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1 in /home/hostfxc/public_html/programming/sean/activate.php on line 35
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #4' at line 1
Warning: Unknown: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0

HostFX-UK
01-28-2009, 12:35 AM
Anymore ideas?

Core4Tech
01-28-2009, 12:49 AM
Wow. I must be tired, and not sure why I just noticed this. You have the following...
$query = mysql_query ("SELECT * FROM users WHERE username='$_POST[usrnme]'");
You have the variable $query set to a function with a "SELECT" statement. Try the following instead.
$query = "SELECT * FROM users WHERE username='$_POST[usrnme]';"
Now you will actually be passing the real query into the function mysql_query.

LifeSteala
01-28-2009, 12:53 AM
Hi there, try this:
include "includes/config.php";
$dblink = mysql_connect($sqlhost, $sqluser, $sqlpassword);
mysql_select_db($sqldb,$dblink);
$query = mysql_query("SELECT * FROM users WHERE username='" . $_POST[usrnme] . "'") or die ("Could not perform query. " . mysql_error());
while ( $row = mysql_fetch_array($result) ) {
echo $result['password'];
}
mysql_close($dblink);

HostFX-UK
01-28-2009, 01:08 AM
None of the above worked but I changed a few things my self, here is the new code:
include "includes/config.php";
$dblink = mysql_connect($sqlhost,$sqluser,$sqlpassword);
mysql_select_db($sqldb,$dblink);
$usrid=$_REQUEST[usrnme];
$query = "SELECT * FROM users WHERE username='$usrid';";
$result = mysql_query($query);
$row = mysql_fetch_array($result) or die(mysql_error());
{
echo $row['activcode'];
}
mysql_close($dblink);
}
Here is the error:
9f9wFEhbcf4GTde88
Warning: Unknown: 1 result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query() in Unknown on line 0
The code shown above the error is the result from the database, so it is connecting and getting the details, just giving me an error at the same time.
Thanks.

foobic
01-28-2009, 01:17 AM
The warning you're getting there can be ignored, turned off or fixed by following the advice in the warning message itself.
Much more important: sanitize your user input. Your code is just waiting to be exploited. http://en.wikipedia.org/wiki/SQL_injection

HostFX-UK
01-28-2009, 01:21 AM
I managed to turn it off :)
FIXED!!!!
Thank you everyone :D

HostFX-UK
01-28-2009, 01:29 AM
By the way it was the problem was that mysql.trace_mode needed to be turned off in the php.ini :)

HostFX-UK
01-28-2009, 01:36 AM
Okay, I have another problem.
The code below functions without any errors what so ever but simply does not update the mysql where it should change the 'active' to the value 'yes'. See below, thanks very much
include "includes/config.php";
$dblink = mysql_connect($sqlhost,$sqluser,$sqlpassword);
mysql_select_db($sqldb,$dblink);
$usrid=$_REQUEST[usrnme];
$query = "SELECT * FROM users WHERE username='$usrid';";
$result = mysql_query($query);
$row = mysql_fetch_array($result) or die(mysql_error());
{
if ($activcode1 == $row['activcode']) {
$usrid=$_REQUEST[usrnme];
$sql = "UPDATE users SET active='yes' WHERE username='$userid'";
echo "Account Active!";
} else {
echo "Incorrect code!";
}
}
}

HostFX-UK
01-28-2009, 04:50 AM
Can anybody help?

LifeSteala
01-28-2009, 05:34 AM
I added you to MSN.

foobic
01-28-2009, 05:40 AM
$usrid=$_REQUEST[usrnme];
$sql = "UPDATE users SET active='yes' WHERE username='$userid'";
Don't forget to read up on SQL injection. eg. what do you think would happen if someone entered the username ' OR 1;

tim2718281
01-28-2009, 06:37 AM
Yes, I can help.
When you have a piece of code delivering unexpected results,
precede the code with a few statements displaying the current values of any variables.
That will sometimes help you see the problem right away; but if it does not, when you post the results with your requests for help, people will have more to go on.