Web Hosting Talk







View Full Version : PHP RGP Game Error


spacehosting
01-01-2008, 09:26 PM
Hello,


I bought a RGP Mafia script.

The core seems to be ok, But when users try and register i get the following messages.


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 65

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 66
mysql_error



In my register.php on lines 65/66 it says.

$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)){
echo "Im sorry but there has been an error please read on..<br />";
if($email_check > 0){
print"Your email address has already been used by another gangster!";
unset($email);

Not sure what the problem is... i been told i need to create fields in my database table.

example Field : $reg_username

Please Help,
Thanks,
Adam

dollar
01-01-2008, 09:54 PM
Need to see a bit more of it to know what's actually going on. The error is telling you:

"Hey, you're asking me to get the number of rows from an SQL query, but the object you gave me that I need to count the rows of is invalid!"

As such you need to take a look at the query that should be above that code. Then take a look at your database and make sure everything is in place in the database so that the query will actually be a valid one.

spacehosting
01-01-2008, 10:23 PM
erm... confused

dollar
01-01-2008, 10:30 PM
Example:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM users');
$num_rows = mysql_num_rows($result);
echo "There's " . $num_rows . " rows!";


Now let's say that for some reason you didn't have the "name" field in the database. Or you didn't have user's table in your database. The variable $result would not be a valid result (as the SQL query would be invalid). Because it's an invalid result, you would not be able to count the number of rows.

It'd be akin to trying to count how many apples are in a bowl of oranges (poor example but the best I can do right now).

Does that make things any clearer?

spacehosting
01-01-2008, 11:37 PM
Still Confused..

dollar
01-01-2008, 11:39 PM
Still confused on what you're confused about... ;)

spacehosting
01-01-2008, 11:56 PM
Basicly,


Its giving me this error when a user tries to register.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 65

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 66
mysql_error

Says they are errors on line 65 and 66.

ATTACHED REGISTER.PHP Below


<?php
session_start();
include_once"includes/db_connect.php";
if (isset($_SESSION['username'])){
header("Location: /game/play.php");
exit();
}
$referal=$_GET['ref'];
$referal2=$_POST['ref2'];
$scriptcheck=$_POST['scriptcheck'];
$scriptcheck2=$_POST['scrip'];
$sc=rand(111,999);
if(!$referal2){
$referal2=0;
}

if ($_POST['Submit']){
// Define post fields into simple variables
$reg_username = $_POST['reg_username'];
$email = $_POST['email'];
$email1 = $_POST['email1'];
$location=strip_tags($_POST['location']);
$reg_username=trim($reg_username);
$today = gmdate('Y-m-d H:i:s');

$reg_username = stripslashes($reg_username);
$email = stripslashes($email);
$reg_username = strip_tags($reg_username);
$email = strip_tags($email);

if((!$reg_username) || (!$email) || (!$location)){
$message="Please fill in all fields";
}else{
if ($email != $email1){
$message="Emails do not match";
}elseif ($email == $email1){
if($scriptcheck != $scriptcheck2){
print"Script check wrong!";
}else{
if (ereg('[^A-Za-z]', $reg_username)) {
$message="Your username can only contain letters.";
}elseif (!ereg('[^A-Za-z]', $reg_username)) {

if (strlen($reg_username) <= 3 || strlen($reg_username) >= 40){
$message= "Username too big or small.";
}elseif (strlen($reg_username) > 3 || strlen($reg_username) < 40){

$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);
if(($email_check > 0) || ($username_check > 0)){
echo "Im sorry but there has been an error please read on..<br />";
if($email_check > 0){
print"Your email address has already been used by another gangster!";
unset($email);
}
if($username_check > 0){
print"Your desired username is already in use!";
unset($reg_username);
}


}else{
$ip = $_SERVER['REMOTE_ADDR'];
$random_password44=rand(1111111,99999999);
$time=time();
mysql_query("INSERT INTO `user_info` ( `id` , `username`)
VALUES (
'', '$reg_username')");
mysql_query("INSERT INTO `users` ( `id` , `username` , `password` , `money` , `regged` , `email` , `location` , `ip` , `r_ip`, `referal`)
VALUES ('', '$reg_username', '$random_password44', '10000', '$time', '$email', '$location', '$ip', '$ip', '$referal2')") or die(mysql_error);


$userid = mysql_insert_id();
// Let's mail the user!
$subject = "Modern Mobsters";
$message = "Thank you for registering at Modern Mobsters,
You can login to the game at www.modern-mobsters.com with the following details:
Username: $reg_username
Password: $random_password44

Please change your password onced you're logged in for your safety.
Thank you very much for registering
http://www.modern-mobsters.com staff.

Note: This is an automated email, please do not reply as you wont get response.";

mail($email, $subject, $message,
"From: Modern Mobsters<mailserver@modern-mobsters.com>");
echo "Your account has been registered! Check your email for your password!";
} }}}}}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
<link href="stylesheets/index.css" rel="stylesheet" type="text/css" />
<script language=JavaScript>
function so(dis)
{
for (i=0;i<dis.elements.length;i++){
if (dis.elements[i].type=='submit')
dis.elements[i].style.visibility='hidden';
}
if(fs==false){
fs=true;
return true;
}else
return false;
}
function goaway()
{
for(i=0;i<document.forms.length;i++)
document.forms[i].onsubmit = function() {return so(this);};
}
</script>
</head>
<body onLoad="goaway();">
<table border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td class="header">Modern Mobsters</td>
</tr>
<tr>
<td height="18" align="center"><div id="sectionLinks">
<a href="index.php">Home</a><a href="register.php">Register</a><a href="lostpass.php">Lost Password</a><a href="/screens.php">Screenshots</a><a href="story.php">Story Line </a></div></td>
</tr>
<tr>
<td height="300" class="main"><p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<form action="" method="post" name="form2" id="form2">
<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>
<td width="130" height="40" class="bodytext"> Username: </td>
<td width="57%"><input name="reg_username" type="text" id="reg_username" maxlength="40" /></td>
</tr>
<tr>
<td class="bodytext">E-mail:</td>
<td><input name="email" type="text" id="email" value="" /></td>
</tr>
<tr>
<td class="bodytext">Re-Type E-mail: </td>
<td><input name="email1" type="text" id="email1" value="" /></td>
</tr>
<tr>
<td height="20" class="bodytext">Starting Location </td>
<td><select name="location" class="ddm" id="location" >
<option value="England" selected="selected">England</option>
<option value="China">China</option>
<option value="France">France</option>
<option value="Holland">Holland</option>
<option value="USA">USA</option>
<option value="Germany">Germany</option>
<option value="Canada">Canada</option>
<option value="South Africa">South Africa</option>
</select></td>
</tr>
<tr>
<td height="20" class="bodytext"><p>Script Check: <b><? print"$sc"; ?></b></p>
</td>
<td><input name="scriptcheck" type="text" id="scriptcheck" value="" />
<input type="hidden" value="<? print"$sc"; ?>" name="scrip"></td>
</tr>
<tr>
<td height="20" class="bodytext">Referal (optional) </td>
<td>
<? if(!$referal){ ?>
<input name="ref2" type="text" id="ref2" value="<? print"$referal"; ?>" maxlength="40" />
<? }else{ ?>
<input name="ref2" type="text" id="ref2" value="<? print"$referal"; ?>" maxlength="40" READONLY>
<? } ?> </td>
</tr>
<tr>
<td>&nbsp;</td>
<td> <input name="Submit" type="submit" class="submit" value="Register!" /> </td>
</tr>
</table>
</form> </td>
</tr>
</table>
<center>
</td>
<td>&nbsp;</td>
</tr>
<tr>
<td height="14"></td>
</tr>
</center>
</body>
</html>


Thanks,
Adam

dollar
01-02-2008, 12:05 AM
I'm trying to teach you to fish instead of just giving you a piece of salmon ;)

If you followed my post you'd see I explained that the script is trying to get the number of rows for a query. If the query is invalid then it's not going to get a valid result. If it doesn't get a valid result it can't count the number of rows.

Right above the lines where the errors are you'll see these two lines:

$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'");

Those lines are queries to the database with SQL. The first one says:

"Give me the e-mail field from anybody in the user's table where their e-mail matches whatever is in $email and their status is active."

Now for some reason or another this is not returning a valid result.

Now one way of going about figuring out what the problem is would be to test the SQL by hand. Add a line like this somewhere towards the top of the script:

echo("SELECT email FROM users WHERE email='$email' AND status='Alive'");

This will echo the actual SQL that is being generated for that statement. Take whatever is echoed and run it by hand against your database and see what the result is. You can do this via commandline, a tool like Navicat MySQL, using PHPMyAdmin, etc...

spacehosting
01-02-2008, 12:58 AM
I get this...


SQL query: http://www.spacehosting.co.uk:2082/3rdparty/phpMyAdmin/themes/original/img/b_help.png (http://dev.mysql.com/doc/mysql/en/select.html)
SELECT email
FROM users
WHERE email = '$email'
AND STATUS = 'Alive'
LIMIT 0 , 30
MySQL said: http://www.spacehosting.co.uk:2082/3rdparty/phpMyAdmin/themes/original/img/b_help.png (http://dev.mysql.com/doc/mysql/en/error-messages-server.html)
#1146 - Table 'spacehos_mafia.users' doesn't exist

dollar
01-02-2008, 12:59 AM
Gotta read the errors:

#1146 - Table 'spacehos_mafia.users' doesn't exist

spacehosting
01-02-2008, 01:12 AM
but, When i log into phpmyadmin


Its there spacehos_mafia users

FieldTypefuctionNullValue

But the table is empty, Im not sure what goes in.

dollar
01-02-2008, 01:19 AM
Have you tried creating a user on the site?

spacehosting
01-02-2008, 01:20 AM
yes and the error i get is



Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 65

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/spacehos/public_html/ggscrip/index/register.php on line 66
mysql_error

Nich
01-02-2008, 02:26 AM
He said the table is empty. It sounds like it wasn't properly created (didn't run a packaged SQL file).

OP: Are there any columns in the table?

spacehosting
01-02-2008, 03:09 AM
im confused now...

Bilco105
01-02-2008, 06:49 AM
Spacehosting,

Did you follow the installation procedure when you purchased the script? - chances are, the script was bundled with a number of .sql files you need to execute on your mafia database, that should make the errors go away.

dollar
01-02-2008, 01:47 PM
im confused now...

When you just post that you are confused there's nothing we can do to help you. You need to explain what you are confused about or hire a programmer/talk to whoever sold you the script to fix your issues ;)

BlueHayes
01-02-2008, 01:55 PM
In PHPMyAdmin, click the table name. Then make sure "structure" is selected at the top. Is there any structure to the table, by this I mean fields.. such as "password" "id" "username".

If you're still confused now, then you may want to do some more learning before administrating a website of this type... since it isn't commercially supported I assume (since you're here about SQL problems) then you will probably need some of your own skills to back you when these sort of errors crop up. The guys above were trying to help you to understand and fix the problem yourself - they aren't going to login and solve it for you. Do some more searches on Google etc and try to understand the problem.

Its there spacehos_mafia users

FieldTypefuctionNullValue

But the table is empty, Im not sure what goes in.

I guess you need to consult the original owner of the "script" to find out what fields need to be in the users table, or work it backwards from the script - but this can be very difficult and buggy. Try removing the database if you haven't used it yet, and re-importing the original SQL file?

This is all I can say really.. all of the people above have given lots of information on how to go about solving the problem!!

Best of luck :agree:

Czaries
01-02-2008, 02:03 PM
You are getting this error because the query is failing. The query was not checked to ensure it was successful before it was shoved into the mysql_num_rows() function, so that function is complaining that the argument passed to it is not what it was expecting.

So instead of just doing this:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM users');
$num_rows = mysql_num_rows($result);
echo "There's " . $num_rows . " rows!";


You need to create an additional check to ensure the query was successfully executed, like so:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM users');
// CHECK to ensure the query executed successfully !!!
if($result) {
$num_rows = mysql_num_rows($result);
echo "There's " . $num_rows . " rows!";
} else {
exit("An error has occured: " . mysql_error());
}

Czaries
01-02-2008, 02:05 PM
Also in your case, the error you are getting means the table 'users' it is trying to select from does not exist. If this is a script that you installed, you may need to run the install script again, or setup the table manually through phpMyAdmin, command line, or some other similar tool.

spacehosting
01-02-2008, 02:23 PM
Fixed.


After all that i didn't upload the mysql file. LOL NOOB

BlueHayes
01-02-2008, 02:26 PM
hehe, always a silly mistake. Congratulations, good luck with your new venture.

Nich
01-02-2008, 07:15 PM
Fixed.


After all that i didn't upload the mysql file. LOL NOOB

After all your unhelpful "I'm confused, I'm confused" pleas, no appreciation for your fellow members who took the time to help you (not me personally, but everyone else in this thread)!


;)

spacehosting
01-03-2008, 11:50 AM
Thanks, Everyone.

<<snipped>>