Page 1 of 1

Query problems

Posted: Thu Jun 23, 2011 2:03 am
by Nexus
Hey, I am having problems with my queries as I always get my die message like this one for example
(Not my actual code)

Code: Select all

$result="SELECT * from players where name='$player' ";
         $result2=mysql_query($result) or die("Could not retrieve info from table");
         $result3=mysql_fetch_array($result2) or die("Could not retrieve array");
I am always getting the die string for result 3 so I don't think is my $result or my $result2

here is the code for the files I am having trouble with

reguser.php

Code: Select all

<?php
include_once 'connect.php';
?>
<?php
include 'logo.php';
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="reguser" align="center">
<?php
$player=$_GET['player'];
$password=$_GET['password'];
$pass2=$_GET['pass2'];
$email=$_GET['email'];

if($email == "")
{
	echo "You did not enter an email!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($pass2 != $password)
{
	echo "Your passwords did not match!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($player == "")
{
	echo "You did not enter a username!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if ($password == "")
{
	echo "You did not enter a password!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($pass2 == "")
{
	echo "You did not retype your password!?<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($password == $pass2)
{
	$isplayer="SELECT * from players where name='$player'";
	$isplayer2=mysql_query($isplayer) or die("Could not get players");
	$isplayer3=mysql_fetch_array($isplayer2) or die("Could not get array");
}
if($isplayer3)
{
	echo "There is already someone with that name!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
else
{
	$password=md5($password);
	$mysql="INSERT into players(name, password, email, level, exper, hpoints, attack, defence, damage) VALUES ($name, $password, 							$email, 1, 0, 30, 5, 5, 5)";
	mysql_query($mysql) or die("Could not update players");
	echo "Thanks for registering!<br/><a href='login.php'><input type='submit' value='Login' name='submit' />";
}
?>
I posted all of it just incase there was an error that was made earlier in the document that may have contributed to my troubles and here is the other file

authenticate.php

Code: Select all

<?php
include_once 'connect.php';
session_start()
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="authenticate" align="center">
<?php
include 'logo.php';
?>
<?php
if (isset($_POST['submit']));
 {
	$player=$_POST['name'];
	$password=$_POST['password'];
	$player=strip_tags($player);
	$password=strip_tags($password);
$query="SELECT name, password from players where name='$player' and password='$password'";
$result=mysql_query($query) or die("Could not get player information");
$result2=mysql_fetch_array($result) or die("Could not get player information query");
if($result2)
  {
	$_SESSION['player']=$player;
	echo "Logged in succesfully!<br/><a href='index.php'><input type='submit' value='Enter the game' name='submit'>";
  }
else
{
echo "Wrong username or password!<br/><a href='login.php'><input type='submit' value='Go back' name='submit'>";

}
 }
?>


Re: Query problems

Posted: Thu Jun 23, 2011 2:36 am
by Tim
Remove the spacing when declaring result 2 and 3.

Code: Select all

$result="SELECT * from players where name='$player' ";
$result2=mysql_query($result) or die("Could not retrieve info from table");
$result3=mysql_fetch_array($result2) or die("Could not retrieve array");

Re: Query problems

Posted: Thu Jun 23, 2011 3:54 pm
by Nexus
Well that was my example of the type of queries I am doing I was looking for my actual code to be examined but thanks for correcting my example :P

Re: Query problems

Posted: Thu Jun 23, 2011 4:18 pm
by ConceptDestiny
is this to check whether or not the player exists? if so, its doing its job.

Try doing the query, then count the result. if result > 0, then echo the username is already taken.

i.e.

Code: Select all


$queryplayer = mysql_query("SELECT * FROM players WHERE name='$player'");
$checkplayer = mysql_num_rows($queryplayer);
if ($checkplayer > 0)
{echo "This username is already taken. Please try another.";}


Re: Query problems

Posted: Thu Jun 23, 2011 4:23 pm
by Nexus
is mysql_num_rows doing the same thing as mysql_fetch_array?

Re: Query problems

Posted: Thu Jun 23, 2011 4:26 pm
by ConceptDestiny
fetch_array is actually fetching the data from the database whereas num_rows is simply counting the result. if you're just checking to see if the username has been used before, then I recommend you use num_rows.

Re: Query problems

Posted: Thu Jun 23, 2011 4:27 pm
by Nexus
Alright I will try that

Re: Query problems

Posted: Thu Jun 23, 2011 4:31 pm
by Nexus
I still get the die message :(

Re: Query problems

Posted: Thu Jun 23, 2011 5:40 pm
by ConceptDestiny
show me your code with the new query

Re: Query problems

Posted: Thu Jun 23, 2011 5:57 pm
by Nexus

Code: Select all

<?php
include_once 'connect.php';
?>
<?php
include 'logo.php';
?>
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="reguser" align="center">
<?php
$player=$_GET['player'];
$password=$_GET['password'];
$pass2=$_GET['pass2'];
$email=$_GET['email'];

if($email == "")
{
	echo "You did not enter an email!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($pass2 != $password)
{
	echo "Your passwords did not match!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($player == "")
{
	echo "You did not enter a username!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if ($password == "")
{
	echo "You did not enter a password!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($pass2 == "")
{
	echo "You did not retype your password!?<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
if($password == $pass2)
{
	$isplayer="SELECT * from players where name='$player'";
	$isplayer2=mysql_query($isplayer) or die("Could not get players");
	$checkplayername=mysql_num_rows($isplayer2) or die("Could not get array");
}
if($checkplayername > 0)
{
	echo "There is already someone with that name!<br/><a href='register.php'><input type='submit' value='Go Back' name='submit' />";
	exit;
}
else
{
	$password=md5($password);
	$mysql="INSERT into players(name, password, email, level, exper, hpoints, attack, defence, damage) VALUES ($name, $password, 							$email, 1, 0, 30, 5, 5, 5)";
	mysql_query($mysql) or die("Could not update players");
	echo "Thanks for registering!<br/><a href='login.php'><input type='submit' value='Login' name='submit' />";
}
?>
I have had this thought in the back of my mind that maybe its my connect.php or how my database is but I don't know