Query problems

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Query problems

Post 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'>";

}
 }
?>

Last edited by Nexus on Thu Jun 23, 2011 3:54 pm, edited 1 time in total.
Tim
Posts: 37
Joined: Fri Jun 10, 2011 12:49 am

Re: Query problems

Post 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");
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Query problems

Post 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
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Query problems

Post 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.";}

User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Query problems

Post by Nexus »

is mysql_num_rows doing the same thing as mysql_fetch_array?
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Query problems

Post 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.
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Query problems

Post by Nexus »

Alright I will try that
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Query problems

Post by Nexus »

I still get the die message :(
ConceptDestiny
Posts: 261
Joined: Wed Apr 28, 2010 8:35 am

Re: Query problems

Post by ConceptDestiny »

show me your code with the new query
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Query problems

Post 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
Post Reply

Return to “Beginner Help and Support”