View Full Version : Text Based Mafia Game - Need Help

02-17-2007, 09:44 AM
ok, So I am starting a Text Based mafia game. I guess you are also thinking that I am some helpess litle child who doesnt have a clue about coding and just wants to make a game that only about 2 friends will play and then the hosting would be closed down.

Well, your right about one part. I cant code it. I have somewhat knowledge in PHP, but its very very basic knowledge. Of course I am willing to learn. I have a lot more dedication than others, and hey the slogan of my forums is "Dedication is the key to success!" So yer..

I have some scripts, already made, I uploaded them to my server, and created a database with all privaleges, and set it all up. I managed to connect to the Database, but...yes but..Every page I go onto..Its messed up.. I was told to put @ before each mysql_num_rows command..I know thats wrong, and that it only supresses the error..and doesnt fix it..

So, Let me show you an example of what I mean.

After registering on the game, I DO recieve the activation email, but I can not activate the account. Here is the error from Register.PHP

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/register.php on line 53

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/register.php on line 54

Here is the code from those lines in register.php

$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);

Now when I click the link in my email, to activate the account, which has been set to the correct URL, I get this:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/activate.php on line 15

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/activate.php on line 25
Your account could not be activated!

The code inside activate.php is this:

LINE 15:

$act = mysql_num_rows($above);

LINE 25:

$doublecheck = mysql_num_rows($sql_doublecheck);

As you can see, its obviously something to do with MySQL. I have no idea what though. I think we can saefly say it will be easier to sort this MySQL error on each page, rather than start a whole new source. I will be mass editing this source with new crimes etc, but I wont be doing that unless I can actually regsiter, log in, and actually play the game.

I would appreciate any help that you can offer. Thanks.


02-17-2007, 02:40 PM
All the errors you posted are occurring because the mysql_query() function calls are failing. The mysql_query() function calls are failing for any of the following reasons -

1) A syntax error, typo, or wrongly named table or column in the query string that is preventing mysql from executing the query
2) No database is selected
3) No connection to the mysql server

You are not the only one with a "mafia" game that is experiencing basic errors. Either whoever created the code being given out knew less than you about writing code with error checking and reporting, or this code is being given out by an instructor with built in errors as a test to see how well his students have been paying attention in class.

Well written code must always test and take appropriate action for both successful and failed calls to any function. Take the case of the mysql_query() function calls in the code you are dealing with. Here is the definition from the PHP programming reference manual -

Return Values
For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. Every call to this function in anybodies code should check for a FALSE result. If the function call failed, the query was not executed and the code should take the appropriate action. Typically, the code should output a message to the user.

The typical method to do this for mysql function calls that return a mysql_error, would be to add the following or die('Message to be output' . mysql_error()) to the end of the function call.

//Change a typical query statement from something like this -
$result = mysql_query($query);

//to this -
$result = mysql_query($query) or die('Query Failed: ' . mysql_error());Adding this error checking and reporting to the mysql_query() statements will pin down why they are failing. If they are failing due to no database being selected or no connection to a mysql server, you can pin down why the mysql_select_db() and mysql_connect() statements are failing using the same error checking logic -

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password') or die ('Could not connect to mysql server: ' . mysql_error());
$db_selected = mysql_select_db('foo', $link) or die('Could not select database: ' . mysql_error());A shorter answer - after security (which should be at the top of everyones list when writing a program), every program must contain a maximum of error checking and reporting so that you know what it is doing when it does not do what you expect. Programs that don't have any error checking and reporting result in "why doesn't my code work" posts on Forums like this one.

Edit: And if a program does not have error checking logic to tell you why it is not working, how is someone in a programming help Forum supposed to be able to tell why it is not working???

02-17-2007, 06:49 PM
Well, bascially, I felt that it would be a lot faster to sort out all of the mysql issues, rather than code it again from scratch. Are you saying you cant help me at all? I dont really know MySQL nor PHP very well.

Of course im willing to learn, Im very dedicated to coding and website development.

Please help me out mate. I just wanna make this work out.

http://www.pcgrounds.com/mafia <--- Incase you can help

02-17-2007, 07:08 PM
In order to find what is causing each of the errors you posted, you must add error checking and reporting to the mysql_query() statements. Once you provide information on why the queries are failing, someone can help you solve the problems.

There are multiple possible reasons for the errors, and in fact some of them could be caused by one possibility and some of them by another.

Without specific information about what is going on in the program, no one can provide specific help.

02-18-2007, 02:11 PM
Look, I dont have any knowledge of MySQL or PHP, I dont know where to put those codes. I searched the document for the codes, but no luck..

Maybe there is somewhere I can learn PHP and MySQL?

Im a serious beginner to PHP and MySQL, I only know HTML a bit...

02-18-2007, 04:20 PM
The author of the code you are trying to use did an incredible dis-service by releasing code that was intended to be used by others, in any number of differently configured PHP/mysql environments, without it having, apparently, any error checking and error reporting.

I realize this does not help you find or fix your existing problems, but here is a partial list of possible reasons for those errors. Any one of these can cause all the errors you posted -

1) There is no mysql server present or running
2) The hostname, db user name, or db password being used for the mysql server is incorrect
3) The config file for your game does not exist or does not have the proper entries for the mysql hostname, db user name, or db password
4) The database does not exist or is not named the same as in the game's config file
5) A user/password was not created for the database
6) The user for the database does not have the proper permissions
7) The tables in the database don't exist or the tables don't have names matching the games config file or coding
8) The game was written assuming register_globals are on and variables that get used in the mysql_query are not being set
9) The game was written using depreciated long_array variable names that are not being set
10) The game's config file could not be created or read because of a file permissions problem

If I or anyone else here would spend a little more time, we could probably add at least 6 - 12 more reasons to this list that could be causing the error messages you have posted.

In programming, any symptom/error can have multiple causes and any problem can produce multiple symptoms. Troubleshooting involves backtracking from the point where the error was detected until you find the cause of the error. Doing this requires an understanding of the programming language, its syntax/language constructs, and functions.

Most of the problems on the list above are detectable by program logic, but it would require the author of that program to have included the necessary error detection and error reporting code. Since he did not do this, you don't have any indication of which one of the possible areas to begin looking in to find the problem. Other than the error messages being output by PHP and that these are due to the mysql_query() statements failing, you and the people reading this don't know and can only guess what is causing each of the errors.

In my post above, I gave a generic coding change that will help troubleshoot and pin down what is causing the errors you posted -

//Change a typical query statement from something like this -
$result = mysql_query($query);

//to this -
$result = mysql_query($query) or die('Query Failed: ' . mysql_error()); So start by identifying the mysql_query(...) statements in the code, prior to where the errors are reported and change them to include the mysql_error() statement.

Also, locating the mysql_select_db() and mysql_connect() statements in the code and adding the error checking and reporting code I gave above will help pin down if the problems are caused with connecting to and selecting a database.

02-18-2007, 04:51 PM
1-6 are done, 7-10 are not.

$result = mysql_query($query);

I was not able to find this in the document, so how can I change it?

02-18-2007, 05:05 PM
It is possible that the code is using a database object CLASS function instead.

Post 10 or so lines of code prior to one of the lines giving an error.

02-18-2007, 07:05 PM
Ok, I did the following just now.

I replaced the MySQL Database information in db_connect.php with the following information so that an error can be defined:

$dbh=mysql_connect ("localhost", "xtr3me_mafia", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("xtr3me_mafiaxtr3me");

Now, it seems to connect to the database, because it loads the page, instead of displaying an error instantly.

However, when I go to register, when I click "create" after entering details, the email "IS" sent to me, but I cant activate.

So, you ask for 10 or so lines prior to the error line? ok.


Error Lines - 53 and 54.

The errors given?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/register.php on line 53

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/register.php on line 54

Lines 43 - 54

if (strlen($reg_username) <= 3 || strlen($reg_username) >= 20){
$message= "Username must be over 3 and under 20";
}elseif (strlen($reg_username) > 3 || strlen($reg_username) < 20){

$sql_email_check = mysql_query("SELECT email FROM users
WHERE email='$email' AND status='Alive'");
$sql_username_check = mysql_query("SELECT username FROM users
WHERE username='$reg_username'");

$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);

As you can see, its the mysql_num_rows that are having a little issue. I was told to add "@" before the error, but no..That only supresses it.. So..Next up.


The errors Given?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/activate.php on line 15

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/xtr3me/public_html/mafia/activate.php on line 25
Your account could not be activated!

Please note that this is after I have clicked the link in the email. Which should link me to this page saying that my account has been activated, but instead I get this. So the code?

Lines 1 - 25

include 'includes/db_connect.php';
echo "<link href=includes/in.css rel=stylesheet type=text/css> ";
$id = $_REQUEST['id'];
$code = $_REQUEST['code'];
$ref= $_REQUEST['ref'];

$id = strip_tags($id);
$ref = strip_tags($r);

$above = mysql_query("SELECT activated FROM users WHERE id='$id' AND password='$code' AND activated='1'");
$act = mysql_num_rows($above);
if ($act > "0"){
echo "<strong><font color=red>Your account has already been activated!</font></strong>";


$up = mysql_query("UPDATE users SET activated='1' WHERE id='$id' AND password='$code'");

$sql_doublecheck = mysql_query("SELECT * FROM users WHERE id='$id' AND password='$code' AND activated='1'");
$doublecheck = mysql_num_rows($sql_doublecheck);

As you can see, once again, Its the mysql_num_rows that are causing the problem.

How do you suggest I define the problem in the activate.php and register.php files? I understand the config one, but I dont understand what to add in the files to show the error. I am sorry but my PHP knowledge is low..

02-18-2007, 07:09 PM
Sorry for double post, but I think I know the problem. When I uploaded the files, I didnt create any sql queries, or tables..Wouldnt I need those?

02-18-2007, 07:32 PM
Could be, that would be item #7 on my list above.

Most well written applications include installation instructions and an installation script that sets everything up. We don't know anything beyond what you have posted about what script you have or what you have done up to this point.

02-18-2007, 07:34 PM
I found a BLANKsql.sql file in the folder.

IM gonna run it in PHPMYADMIN now. Ill let you know how it goes.