PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
So I fixed my server connection issues and have been diligently working on the user content. Login and everything works, I can greet the user, and everything in my USERS database works (their personal info and how many points they have).
What i want to do now is display HOW they got those points. I have a db with the following
tasknum (will auto_increment)
username
date
task (what they did)
points
notes
So I want to pull all of the tasknums from the database for username and display them.. and the other things as well... Ive been taking a stab at this, but have only done arrays for displaying pictures and am having issues reusing the code.. can anyone help me solve this?
$display= mysql_query("SELECT * FROM `points` WHERE `username`=".$username.");
if(!$display) { print("We do not have a task recorded you in the database."); } else {
$row = mysql_fetch_array($display);
if(!empty($row['tasknum'])){
$output.='$date $task $points $notes';
print($output);
} else {
print("You have no tasks recorded on record.");
}
}
Is this just a test project of yours? If not, I'd recommend you seriously reconsider the database design.
If it is, cool.
Either way, your code sniplet has a small problem with your first variable definition. It is missing a double quote at the end. You are also missing single quotes around the username variable.
In other words, it should read like this:
$display= mysql_query("SELECT * FROM `points` WHERE `username`= '" . $username . "')";
if(!$display) { print("We do not have a task recorded you in the database."); } else {
$row = mysql_fetch_array($display);
if(!empty($row['tasknum'])){
$output.='You have no tasks recorded on record.';
print($output);
} else {
print("$date $task $points $notes");
}
}
does it affect my table at all that I have 2 databases, 'users' and 'points' and username is a field on both, but one default one that is called is from users.
$display= mysql_query("SELECT * FROM `points` WHERE `username`= '" . $username . "')";
if(!$display) { print("We do not have a task recorded you in the database."); } else {
$row = mysql_fetch_array($display);
if(!empty($row['tasknum'])){
$output.='You have no tasks recorded on record.';
print($output);
} else {
$output.=$row['date'] . $row['task'] . $row['points'] . $row['notes'];
}
}
This is the code on a different page to call the variables.. the call to the different page works for my users table.. this is the points table
The reason your not seeing the error because your display_errors configuration is set to false. Therefore, when a parse error occurs no messages are displayed (not good for development, but should be done in production). You can either modify your php.ini or add a php_flag display_errors true in an .htaccess file.
Finally, take a very close look where you put that double quote in your query.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin@yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
ok i kept debugging and now it comes up.. i am left with this error
Notice: Undefined variable: output in /hermes/bosweb01/b141/sl.nhforhealthcare/public_html/usermenu.php on line 49
You have no tasks recorded on record.
<?
$display= mysql_query("SELECT * FROM `points` WHERE `username`= '" . $username . "'");
if(!$display) { print("We do not have a task recorded for you in the database."); } else {
$row = mysql_fetch_array($display);
if(!empty($row['tasknum'])){
$output.='You have no tasks recorded on record.';
print($output);
} else {
$output.=$row['date'] . $row['task'] . $row['points'] . $row['notes'];
}
}
?>
$display= mysql_query("SELECT * FROM `points` WHERE `username`= '" . $username . "'");
if(!$display) { print("We do not have a task recorded for you in the database."); } else {
$row = mysql_fetch_array($display);
if(!empty($row['tasknum'])){
echo 'You have no tasks recorded on record.';
} else {
$output.=$row['date'] . $row['task'] . $row['points'] . $row['notes'];
print($output);
}
}
You've got a few things going on in there that aren't quite right. You should be showing the first message based on "!$display", because that will tell you if the query failed or succeeded, not whether you actually have any tasks recorded. You got the notice because you are concatenating something to $option when $option had not yet been defined. See if the below code (with modifications) helps at all. I noticed inconsistent use of echo and print. Best to be consistent than to mix them up, in my opinion.
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin@yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
As for your problem, SBukoski already covered the rest.. although I forgot to mention you probably should be passing $username through mysql_real_escape_string to protect against SQL injection.
Although, in the future do consider it is against our forum rules to bump your threads within 24 hours. But do familiarize yourself with the edit button to avoid bumping.