Jump to content

PHP Session_Start(); ERROR AND MORE! PLEASE HELP!


Freid001

Recommended Posts

Hi guys, (I'm not sure if this is in the right topic. If not please could you move to the correct topic thanks.)

 

Ok I recently moved my website from the host ULMB to a new host called Awardspace. My web site is a PHP code MMORPG which uses a MYSQL database. Whilst moving the website I did not change any of the PHP code. On the new host the PHP code still does work as it was  programmed to do but these errors keep occurring on all the pages which contain PHP code to update the MYSQL database:

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /~/www/europeanwarfare.atwebpages.com/checklogin.php:1) in /srv/disk4/657046/www/europeanwarfare.atwebpages.com/connection2.php on line 2

 

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /~/www/europeanwarfare.atwebpages.com/Shop/R3.php:1) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 32

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 124

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 124

 

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 126

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 127

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 127

 

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 128

 

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 129

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 130

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 130

 

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 131

 

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 132

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 133

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 133

 

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 134

 

I just don't understand why I keep getting these errors? :confused: The PHP code still works fine and does update and retrieve information from the MYSQL data base correctly but these error keep occurring I just don't understand it?  :wtf: I spock to Awardspace and found that they run PHP v5.2.5 and MYSQL v5.1 and my old host ULMB ran on PHP v5.3.3 and MYSQL v5.0.90. I don't know if this is what is causing the problem?

 

Feel free to see the errors for your self go to the web site at http://europeanwarfare.atwebpages.com/ and use the user name: tester and password: tester to login and see the errors. Then go to any page which contains PHP update code and these will be a error but the code will still work correctly? :confused:

 

 

Thanks For Any Help!

 

Fraser  :-\ :confused::D

Link to comment
Share on other sites

Regarding the header errors, there's a sticky topic on that at the top of the forum, titled HEADER ERRORS - READ HERE BEFORE POSTING THEM. The rest of the errors appear to all be due to a failure to connect to the database in this particular script. Can you post the code (with db username/password edited out, of course)?

Link to comment
Share on other sites

Regarding the header errors, there's a sticky topic on that at the top of the forum, titled HEADER ERRORS - READ HERE BEFORE POSTING THEM. The rest of the errors appear to all be due to a failure to connect to the database in this particular script. Can you post the code (with db username/password edited out, of course)?

 

Yep sure! Ok I use a the PHP code include("connection.php"); which refers back to the code on the page connection.php which contains all connection variables.

 

Here is the code for connection.php

 

<?php
session_start();
$host = "fdb3.awardspace.com";
$mysql_user = "XXXXXXX";
$mysql_password = "XXXXXX";
$mysql_database = "657046_ewgame";
$user = $_SESSION['username'];
$username = $user;
$update = $user;

if($user==""){
die ("<style type='text/css'>
<!--
body,td,th {
   font-family: Arial, Helvetica, sans-serif;
   font-size: 14px;
   color: #48371E;
}
body {
   background-color: #000000;
}
a:link {
   color: #48371E;
}
a:visited {
   color: #48371E;
}
a:hover {
   color: #48371E;
}
a:active {
   color: #48371E;
}
.style7 {font-size: 18px; }
.style11 {font-size: 9px}
-->
</style>
<title>Login</title><table width='300' height='100' border='0' cellpadding='6' background='/Photos/Message box/Msgbox.png'>
  <tr>
    <td width='274' height='30'>
You are NOT logged in! <br><a href='/memberlogin.php'>Click here to login!</a>
 </td>
  </tr>
</table>");
}else {
$con = mysql_connect($host,$mysql_user,$mysql_password) or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");

$connection  = mysql_connect($host,$mysql_user,$mysql_password) or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");

}
   ?>

 

Thanks for your help!

 

Fraser

 

MOD EDIT:

 . . . 

tags added.

Link to comment
Share on other sites

Regarding the header errors, there's a sticky topic on that at the top of the forum, titled HEADER ERRORS - READ HERE BEFORE POSTING THEM. The rest of the errors appear to all be due to a failure to connect to the database in this particular script. Can you post the code (with db username/password edited out, of course)?

 

I have read the HEADER ERRORS - READ HERE BEFORE POSTING THEM post and still complete don't understand it?  Do I need to remove the session_start(); and $username from connection.php and put them in to each page normally? 

Link to comment
Share on other sites

So the script you've posted above is include()d in another script, right? Can you also post that script between

 . . . 

BBCode tags, please?

 

Yep sure but it is included on every page! (Sorry about code tags)

 

connection2.php

 

 <?php
Session_start();
$host = "fdb3.awardspace.com";
$mysql_user = "XXXXXX";
$mysql_password = "XXXXX
$mysql_database = "657046_ewgame";


$con = mysql_connect($host,$mysql_user,$mysql_password) or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");

$connection = mysql_connect($host,$mysql_user,$mysql_password) or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");
?> 

 

Here is the code for checklogin.php:

 

 <style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #000000;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style11 {font-size: 9px}
.style12 {
font-size: 12px;
font-style: italic;
}
-->
</style>
<title>Login</title><table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30">
    <?php
include("connection2.php");
$username2 = strtolower(strip_tags ($_POST['myusername']));
$password3 = strtolower(strip_tags ($_POST['mypassword']));
$password2 = md5($password3);

function anti_injection($sql){ 
   $sql = preg_replace(sql_regcase("/(from|select|insert|delete|where|drop table|like|show tables|\'|'\| |=|-|;|,|\|'|<|>|#|\*|--|\\\\)/"), "" ,$sql); 
   
   $sql = trim($sql); 
   $sql = strip_tags($sql); 
   $sql = (get_magic_quotes_gpc()) ? $sql : addslashes($sql); 
   return $sql; 
}  

$username = anti_injection($username2); 
$password = anti_injection($password2); 

if ($username&&$password)
{
$connection = mysql_connect("$host","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");

$query = mysql_query("SELECT * FROM Game WHERE user='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{

while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['User'];
$dbpassword = $row['Password'];
$Blocked = $row['Blocked'];
$Online = $row['Online'];
$Away = $row['Away'];
$XP = $row['XP'];
}
$_SESSION['Online'] = $Online;
if ($Blocked == Yes) {
die ("$dbusername your account has been suspended!");
}
if ($username==$dbusername&&$password==$dbpassword) {
$_SESSION['username'] = $username;
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Online = 'Yes' WHERE user = '$username'");
mysql_close($con);
if($XP==1){
echo "<meta http-equiv='refresh' content='1;url=/Player/welcome.php'>";
} else {
echo "<img src='Photos/Loading%20bar.gif' width='280' height='19'><meta http-equiv='refresh' content='6;url=memberspage.php'>";
}
}
else 
    die ("incorrect password!");

}
else
    die("That user doesn't exist!");

}
else 
    die("Please enter a username and password!");

?> </td>
  </tr>
</table>

<table width="302" height="188" border="0" cellpadding="6" background="../Photos/Message box/Msgbox2.png">
  <tr>
    <td width="286" height="136" valign="top"><table width="280" border="0" align="center">
      <tr>
        <td><?php
	if($Online=="Yes"){
echo"<center><b>You forgot to logout last time!</b></center>";
} ?> </td>
      </tr>
    </table>
      <table width="280" border="0" align="center" cellpadding="6">
      <tr>
        <td width="18%" valign="middle"><p>
          <?php
	  if ($Away==""){
                    echo "<img src='/images/icons/NC.png'>";
	  } else if ($Away==0){
	   echo "<img src='/images/icons/NC.png'>";
	  } else  if ($Away<0){
	   echo "<img src='/images/icons/DC.png'>";
	  } else  if ($Away >0){
	   echo "<img src='/images/icons/UC.png'>";
	  }	  		  
	  ?>
        </p>
          </td>
        <td width="82%" height="56" valign="top"><strong>Player Information </strong><br>
          <?php
$NZ = "0";
	  if ($Away==""){
	  echo "Nothing happend whilst you were away!";
                  } else if ($Away == 0){
	  echo "Nothing happend whilst you were away!";
	  } else  if ($Away<0){
	  echo "Whilst you were away you were attacked in battle and lost ($Away XP)";
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET away = '$NZ' WHERE user = '$username'");
mysql_close($con);
	  } else  if ($Away >0){
	  echo "Whilst you were away you were attacked in battle and won (+ $Away XP)";
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET away = '$NZ' WHERE user = '$username'");
mysql_close($con);
	  }	  		  
	  ?>           </td>
      </tr>
    </table>
      <table width="280" border="0" align="center" cellpadding="6">
        <tr>
          <td width="75%" height="59" valign="top"><p align="left"><span class="style11"><img src="/images/icons/IE.png" /><span class="style12"> Best viewed in Internet Explorer</span><br>
            © European  warfare was created by Freid creations. </span><br>
            <br>
          </p></td>
        </tr>
    </table></td>
  </tr>
</table>

 

Here is the code for a 1 of the pages which contains PHP update code but as I said this error keeps happening on all these pages which seem to update the MYSQL database I just dont know whats wrong cos it was working fine before:

 

 <style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #000000;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style1 {
font-size: 9px;
font-style: italic;
}
-->
</style>
<title>Buy Item</title><table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30"><?php
session_start();
$user = $_SESSION['username'];
$username = $user;
include("../connection.php");

$connection = mysql_connect("$host","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server.");
mysql_select_db($mysql_database) or die ("Unable to select requested database.");

$query = mysql_query("SELECT * FROM Game WHERE user='$username'");

$numrows = mysql_num_rows($query);

while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['User'];
$dbpassword = $row['Password'];
$email = $row['Email'];
$name= $row['Fac Name'];
$attck = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$extra1 = $row['Extra 1'];
$extra2 = $row['Extra 2'];
$extra1N = $row['Extra number 1'];
$extra2N = $row['Extra number 2'];
$Rank = $row['Rank'];
$location = $row['Location'];
$Won = $row['Won'];
$Loss = $row['Loss'];
$money = $row['Money'];
$credits = $row['Credits'];
$xp = $row['XP'];
$attack = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$S = $row['S'];
$V = $row['V'];
$A = $row['A'];
}
$query = mysql_query("SELECT * FROM HQ WHERE User='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$HQ = $row['HQ'];
$Raidar = $row['Raidar'];
$Defence = $row['Defence'];
$Missile = $row['Missile'];
$Runway = $row['Runway'];
$Gate = $row['Gate'];
$Defence2 = $row['Defence2'];
$Vehicle = $row['Vehicle'];
$Troops = $row['Troops'];
}
if ($Troops >1){

$level = -1;
$item = "Riflemen 3rd Class";
if ($xp>$level) {
if ($money>249) {
$AD2 = $S + "10";

if($Troops ==2){
if($AD2 > "100"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==3){
if($AD2 > "1000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==4){
if($AD2 > "3000"){
die ("Troop limit reached!<br><a href='../Shop.php'<br>Back</a>");
} 
} if($Troops ==5){
if($AD2 > "5000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
}
}

$price = 250;
$total = $money-$price;
$update= $username;
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Money = '$total'WHERE user = '$update'"); 
mysql_close($con);

$attack2 = $attack + 25;
$defence2 = $defence + 25;
$Yes = $S + 10;


mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Attack = '$attack2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Defence = '$defence2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET S = '$Yes'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Weapons SET R3 = '2' WHERE user = '$update'"); 
mysql_close($con);
echo $update;
echo " you have just bought $item! You now have $total Coins<br><a href='../Shop.php'>Back</a>";

}
else
    echo "You do not have enough money to buy this item! <br><a href='../Shop.php'>Back</a>";
} 
else 
     echo "You need 250 XP to buy this item! <br><a href='../Shop.php'>Back</a>";
} else 
  echo "You require a  troop camp to use this item!<br><a href='../../Shop.php'>Back</a>";

?>
  </tr>
</table>

<p> </p>

 

 

Thanks for all your help!  :D

 

 

 

 

 

 

 

Link to comment
Share on other sites

Your header problems *should* be solved by moving the include() to be the first line in the script, before all the output that it sends, such as CSS and HTML.

 

Why are you using 2 different calls to mysql_connect() and mysql_select_db() using the same credentials and database info?

Link to comment
Share on other sites

Your header problems *should* be solved by moving the include() to be the first line in the script, before all the output that it sends, such as CSS and HTML.

 

Why are you using 2 different calls to mysql_connect() and mysql_select_db() using the same credentials and database info?

 

Ok great thanks for your help I will move the include() straight away thank you very much! I was just using 2 diffrent calls to mysql_connect() to try and get rid of this error cos I was not sure what was wrong but I can change it back to just $con now so thank you so much for your help! Will moving the include() to the top of the page sort out all the other errors?

 

Thanks again

 

Fraser

Link to comment
Share on other sites

No, it probably won't sort out any mysql errors at all. Post the result after moving the include() and getting rid of one of the connect routines, and we'll go from there.

 

 

Ok i'll try get that done got to go now but I will try get the include() moved and will post the result on here tommoro! Thanks for your help!

 

Fraser

Link to comment
Share on other sites

No, it probably won't sort out any mysql errors at all. Post the result after moving the include() and getting rid of one of the connect routines, and we'll go from there.

 

Cool thats fixed the problem on checklogin.php however I am still getting all these errors on all the pages which contain PHP code to update the MYSQL data base:

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 121

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 121

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 123

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 124

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 124

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 125

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 126

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 127

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 127

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 128

Warning: mysql_select_db(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 129

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 130

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 130

Warning: mysql_close(): 2 is not a valid MySQL-Link resource in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 131

 

Heres is the PHP code:

 

<?php 
include("../connection.php");
?>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #000000;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style1 {
font-size: 9px;
font-style: italic;
}
-->
</style>
<title>Buy Item</title><table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30"><?php
session_start();
$user = $_SESSION['username'];
$username = $user;

$query = mysql_query("SELECT * FROM Game WHERE user='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['User'];
$dbpassword = $row['Password'];
$email = $row['Email'];
$name= $row['Fac Name'];
$attck = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$extra1 = $row['Extra 1'];
$extra2 = $row['Extra 2'];
$extra1N = $row['Extra number 1'];
$extra2N = $row['Extra number 2'];
$Rank = $row['Rank'];
$location = $row['Location'];
$Won = $row['Won'];
$Loss = $row['Loss'];
$money = $row['Money'];
$credits = $row['Credits'];
$xp = $row['XP'];
$attack = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$S = $row['S'];
$V = $row['V'];
$A = $row['A'];
}
$query = mysql_query("SELECT * FROM HQ WHERE User='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$HQ = $row['HQ'];
$Raidar = $row['Raidar'];
$Defence = $row['Defence'];
$Missile = $row['Missile'];
$Runway = $row['Runway'];
$Gate = $row['Gate'];
$Defence2 = $row['Defence2'];
$Vehicle = $row['Vehicle'];
$Troops = $row['Troops'];
}
if ($Troops >1){

$level = -1;
$item = "Riflemen 3rd Class";
if ($xp>$level) {
if ($money>249) {
$AD2 = $S + "10";

if($Troops ==2){
if($AD2 > "100"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==3){
if($AD2 > "1000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==4){
if($AD2 > "3000"){
die ("Troop limit reached!<br><a href='../Shop.php'<br>Back</a>");
} 
} if($Troops ==5){
if($AD2 > "5000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
}
}

$price = 250;
$total = $money-$price;
$update= $username;
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Money = '$total'WHERE user = '$update'"); 
mysql_close($con);

$attack2 = $attack + 25;
$defence2 = $defence + 25;
$Yes = $S + 10;


mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Attack = '$attack2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Defence = '$defence2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET S = '$Yes'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Weapons SET R3 = '2' WHERE user = '$update'"); 
mysql_close($con);
echo $update;
echo " you have just bought $item! You now have $total Coins<br><a href='../Shop.php'>Back</a>";

}
else
    echo "You do not have enough money to buy this item! <br><a href='../Shop.php'>Back</a>";
} 
else 
     echo "You need 250 XP to buy this item! <br><a href='../Shop.php'>Back</a>";
} else 
  echo "You require a  troop camp to use this item!<br><a href='../../Shop.php'>Back</a>";

?>
  </tr>
</table>

<p> </p>

Link to comment
Share on other sites

No, it probably won't sort out any mysql errors at all. Post the result after moving the include() and getting rid of one of the connect routines, and we'll go from there.

 

Could the problem be that Awardspace runs PHP v5.2.5 and MYSQL v5.1 and my old host ULMB ran on PHP v5.3.3 and MYSQL v5.0.90. I don't know if this is what is causing the problem?

Link to comment
Share on other sites

This chunk of code is wrong:

<?php
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Attack = '$attack2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Defence = '$defence2'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET S = '$Yes'WHERE user = '$update'"); 
mysql_close($con);
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Weapons SET R3 = '2' WHERE user = '$update'"); 
mysql_close($con);
?>

You shouldn't be selecting the db & closing the connection after every query and all those queries can be performed in one query:

<?php
$q = "update Game set Attack = '$attack2', Defence = '$defence2', S = '$Yes', R3 = '2' where user = '$update'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
?>

 

Also, the "session_start()" needs to be moved to the start of the code right after the opening "<?php".

 

Ken

Link to comment
Share on other sites

I am still getting these errors?  :confused:

 

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 119

 

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /~/www/europeanwarfare.atwebpages.com/Shop/R3.php on line 119

Problem with the query: update Game set Attack = '22724', Defence = '9775', S = '998', R3 = '2' where user = 'freid001'

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Link to comment
Share on other sites

What's on line 119 now?

 

Ken

 

Line 119:

 $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());

 

Here is all the code:

 

<?php 
include("../connection.php");
session_start();
$user = $_SESSION['username'];
$username = $user;
?>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #000000;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style1 {
font-size: 9px;
font-style: italic;
}
-->
</style>
<title>Buy Item</title><table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30"><?php
$query = mysql_query("SELECT * FROM Game WHERE user='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['User'];
$dbpassword = $row['Password'];
$email = $row['Email'];
$name= $row['Fac Name'];
$attck = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$extra1 = $row['Extra 1'];
$extra2 = $row['Extra 2'];
$extra1N = $row['Extra number 1'];
$extra2N = $row['Extra number 2'];
$Rank = $row['Rank'];
$location = $row['Location'];
$Won = $row['Won'];
$Loss = $row['Loss'];
$money = $row['Money'];
$credits = $row['Credits'];
$xp = $row['XP'];
$attack = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$S = $row['S'];
$V = $row['V'];
$A = $row['A'];
}
$query = mysql_query("SELECT * FROM HQ WHERE User='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$HQ = $row['HQ'];
$Raidar = $row['Raidar'];
$Defence = $row['Defence'];
$Missile = $row['Missile'];
$Runway = $row['Runway'];
$Gate = $row['Gate'];
$Defence2 = $row['Defence2'];
$Vehicle = $row['Vehicle'];
$Troops = $row['Troops'];
}
if ($Troops >1){

$level = -1;
$item = "Riflemen 3rd Class";
if ($xp>$level) {
if ($money>249) {
$AD2 = $S + "10";

if($Troops ==2){
if($AD2 > "100"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==3){
if($AD2 > "1000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==4){
if($AD2 > "3000"){
die ("Troop limit reached!<br><a href='../Shop.php'<br>Back</a>");
} 
} if($Troops ==5){
if($AD2 > "5000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
}
}

$price = 250;
$total = $money-$price;
$update= $username;
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Money = '$total'WHERE user = '$update'"); 
mysql_close($con);

$attack2 = $attack + 25;
$defence2 = $defence + 25;
$Yes = $S + 10;

$q = "update Game set Attack = '$attack2', Defence = '$defence2', S = '$Yes', R3 = '2' where user = '$update'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());

echo $update;
echo " you have just bought $item! You now have $total Coins<br><a href='../Shop.php'>Back</a>";

}
else
    echo "You do not have enough money to buy this item! <br><a href='../Shop.php'>Back</a>";
} 
else 
     echo "You need 250 XP to buy this item! <br><a href='../Shop.php'>Back</a>";
} else 
  echo "You require a  troop camp to use this item!<br><a href='../../Shop.php'>Back</a>";

?>
  </tr>
</table>

<p> </p>

Link to comment
Share on other sites

Excellent it works now thank you so much!  :D :D :D :D

Just have to make these change on about 100 other pages now! Huh going to be fun LOL!

 

Here the finished code and it seems to work fine! Thanks again

 

Fraser

 

<?php 
include("../connection.php");
session_start();
$user = $_SESSION['username'];
$username = $user;
?>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #000000;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style1 {
font-size: 9px;
font-style: italic;
}
-->
</style>
<title>Buy Item</title><table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30"><?php
$query = mysql_query("SELECT * FROM Game WHERE user='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['User'];
$dbpassword = $row['Password'];
$email = $row['Email'];
$name= $row['Fac Name'];
$attck = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$extra1 = $row['Extra 1'];
$extra2 = $row['Extra 2'];
$extra1N = $row['Extra number 1'];
$extra2N = $row['Extra number 2'];
$Rank = $row['Rank'];
$location = $row['Location'];
$Won = $row['Won'];
$Loss = $row['Loss'];
$money = $row['Money'];
$credits = $row['Credits'];
$xp = $row['XP'];
$attack = $row['Attack'];
$defence = $row['Defence'];
$health = $row['Health'];
$S = $row['S'];
$V = $row['V'];
$A = $row['A'];
}
$query = mysql_query("SELECT * FROM HQ WHERE User='$username'");
$numrows = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query))
{
$HQ = $row['HQ'];
$Raidar = $row['Raidar'];
$Defence = $row['Defence'];
$Missile = $row['Missile'];
$Runway = $row['Runway'];
$Gate = $row['Gate'];
$Defence2 = $row['Defence2'];
$Vehicle = $row['Vehicle'];
$Troops = $row['Troops'];
}
if ($Troops >1){

$level = -1;
$item = "Riflemen 3rd Class";
if ($xp>$level) {
if ($money>249) {
$AD2 = $S + "10";

if($Troops ==2){
if($AD2 > "100"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==3){
if($AD2 > "1000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
} 
} else if($Troops ==4){
if($AD2 > "3000"){
die ("Troop limit reached!<br><a href='../Shop.php'<br>Back</a>");
} 
} if($Troops ==5){
if($AD2 > "5000"){
die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
}
}

$price = 250;
$total = $money-$price;
$update= $username;

mysql_query("UPDATE Game SET Money = '$total'WHERE user = '$update'"); 

$attack2 = $attack + 25;
$defence2 = $defence + 25;
$Yes = $S + 10;

mysql_query("UPDATE Game SET Attack = '$attack2'WHERE user = '$update'"); 
mysql_query("UPDATE Game SET Defence = '$defence2'WHERE user = '$update'"); 
mysql_query("UPDATE Game SET S = '$Yes'WHERE user = '$update'"); 
mysql_query("UPDATE Weapons SET R3 = '2' WHERE user = '$update'"); 

echo $update;
echo " you have just bought $item! You now have $total Coins<br><a href='../Shop.php'>Back</a>";

}
else
    echo "You do not have enough money to buy this item! <br><a href='../Shop.php'>Back</a>";
} 
else 
     echo "You need 250 XP to buy this item! <br><a href='../Shop.php'>Back</a>";
} else 
  echo "You require a  troop camp to use this item!<br><a href='../../Shop.php'>Back</a>";

?>
  </tr>
</table>

<p> </p>

Link to comment
Share on other sites

Look at your code. On line 112 you close the mysql connection and then try to use it again. Remove the mysql_close(). You don't really need it since the mysql connection is closed when the script exits.

 

Also, this whole section of code

<?php
$price = 250;
$total = $money-$price;
$update= $username;
mysql_select_db("my_db", $con); 
mysql_query("UPDATE Game SET Money = '$total'WHERE user = '$update'"); 
mysql_close($con);

$attack2 = $attack + 25;
$defence2 = $defence + 25;
$Yes = $S + 10;

$q = "update Game set Attack = '$attack2', Defence = '$defence2', S = '$Yes', R3 = '2' where user = '$update'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());

echo $update;
?>

can be replaced with

<?php
$q = "update Game set Money = Money - 250, Attack = Attack + 25, Defence = Defence + 25, S = S + 10, R3 = 2 where user = '$username'";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
echo $username;
?>

 

Ken

Link to comment
Share on other sites

about 100 other pages now!

 

^^^ If you have created a different page for each possible item, that only differs in the value(s) being used in the code, you have missed the point of using a programming language and missed the point of using variables to hold values.

 

You should have ONE page that is passed some item identifier and it simply sets up a variable(s) and reuses one set of code.

 

In programming, anytime you find yourself repeating code, either within one page or making duplicate pages that only differ in the value they use, you can consolidate the code into one instance and save yourself a HUGE amount of time in the process. You will only have one set of code to change. This will also kind of force you to organize all the values your code is using in to an array (or perhaps a database table) that will make it easier to change any values in the future simply by changed the data, rather than going through the code making changes to the values hard-code in the statements.

 

If you are going to go through a large number of pages 'fixing' the same code over and over anyway, wouldn't it be better to spend the same or less amount of time simplifying the whole scheme to use one file?

 

Link to comment
Share on other sites

about 100 other pages now!

 

^^^ If you have created a different page for each possible item, that only differs in the value(s) being used in the code, you have missed the point of using a programming language and missed the point of using variables to hold values.

 

You should have ONE page that is passed some item identifier and it simply sets up a variable(s) and reuses one set of code.

 

In programming, anytime you find yourself repeating code, either within one page or making duplicate pages that only differ in the value they use, you can consolidate the code into one instance and save yourself a HUGE amount of time in the process. You will only have one set of code to change. This will also kind of force you to organize all the values your code is using in to an array (or perhaps a database table) that will make it easier to change any values in the future simply by changed the data, rather than going through the code making changes to the values hard-code in the statements.

 

If you are going to go through a large number of pages 'fixing' the same code over and over anyway, wouldn't it be better to spend the same or less amount of time simplifying the whole scheme to use one file?

 

Yep I know about using ONE page that is passed some item identifier now but when I first created the website I was new to PHP so didn't do it and as a result the whole game and data base is kind of built around it so it will require a lot of work to change but I am definatly going to think about it for the future!

 

Thanks

 

Fraser

Link to comment
Share on other sites

In the amount of time you have spent fooling around with the errors on the one page of code in this thread (which I am guessing was just to add one item to the list of available items), you could have consolidated all your items into one page.

 

Are you going to keep taking the better part of a day every time you add a page or simplify your life and make this easier? You don't even need to do this all at once. Just set up the scheme and get one item working, then you can use the new method to add new items and migrate your existing items over as you see fit.

Link to comment
Share on other sites

In the amount of time you have spent fooling around with the errors on the one page of code in this thread (which I am guessing was just to add one item to the list of available items), you could have consolidated all your items into one page.

 

Are you going to keep taking the better part of a day every time you add a page or simplify your life and make this easier? You don't even need to do this all at once. Just set up the scheme and get one item working, then you can use the new method to add new items and migrate your existing items over as you see fit.

 

Ok well thanks I think I may investigate it further and see how well it will intergrate with the rest of my system. Thanks anyway  :D

Link to comment
Share on other sites

Here is what the code on the page you have posted in this thread would look like -

 

<?php 
include("../connection.php");
session_start();
$username = $_SESSION['username'];
?>
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #48371E;
}
body {
background-color: #ffffff;
}
a:link {
color: #48371E;
}
a:visited {
color: #48371E;
}
a:hover {
color: #48371E;
}
a:active {
color: #48371E;
}
.style1 {
font-size: 9px;
font-style: italic;
}
-->
</style>
<title>Buy Item</title>
</head>
<body>
<table width="300" height="100" border="0" cellpadding="6" background="../Photos/Message box/Msgbox.png">
  <tr>
    <td width="274" height="30">
<?php
$query = mysql_query("SELECT * FROM Game WHERE User='$username'");

$row = mysql_fetch_assoc($query);
//$dbusername = $row['User']; // not used
//$dbpassword = $row['Password']; // not used
//$email = $row['Email']; // not used
//$name= $row['Fac Name']; // not used
//$attck = $row['Attack']; // not used (spelling error in variable name)
//$defense = $row['Defense']; // not used (also duplicated below)
//$health = $row['Health']; // not used (also duplicated below)
//$extra1 = $row['Extra 1']; // not used
//$extra2 = $row['Extra 2']; // not used
//$extra1N = $row['Extra number 1']; // not used
//$extra2N = $row['Extra number 2']; // not used
//$Rank = $row['Rank']; // not used
//$location = $row['Location']; // not used
//$Won = $row['Won']; // not used
//$Loss = $row['Loss']; // not used
$money = $row['Money']; // used
//$credits = $row['Credits']; // not used
$xp = $row['XP']; // used
// $attack = $row['Attack']; // not used (no spelling error in this one)
// $defense = $row['Defense']; // not used (duplicated above)
//$health = $row['Health']; // not used (duplicated above)
$S = $row['S']; // used
//$V = $row['V']; // not used
//$A = $row['A']; // not used

$query = mysql_query("SELECT * FROM HQ WHERE User='$username'");
$row = mysql_fetch_assoc($query);
//$HQ = $row['HQ']; // not used
//$Raidar = $row['Raidar']; // not used
//$Defense = $row['Defense']; // not used, letter case difference
//$Missile = $row['Missile']; // not used
//$Runway = $row['Runway']; // not used
//$Gate = $row['Gate']; // not used
//$Defence2 = $row['Defence2']; // not used
//$Vehicle = $row['Vehicle']; // not used
$Troops = $row['Troops']; // used

// item specific values (see the $items array that is immediately after these comments in the code)
//$price = 250;
//$level = -1; // XP level needed (-1 to disable)
//$item = "Riflemen 3rd Class";
//$skill = 10; // added to skill level
//$attack = 25; // added to attack level
//$defense = 25; // added to defense level

// put the following $items array code into a separate file and include it into any page that needs to access the item array
// you can also loop through the $items array to list the item name and features in your menu where you select an item to purchase
$items = array();
$items[1] = array('price'=>250, 'level'=> -1, 'item' => "Riflemen 3rd Class", 'skill' => 10, 'attack' => 25, 'defense' => 25);
// Define other items here...

$item_code = isset($_GET['item']) ? (int)$_GET['item'] : 0 ; // get and condition any item_code on the end of the URL
if(isset($items[$item_code])){
// the item does exist, populate the program variables with the appropriate values
foreach($items[$item_code] as $key => $value){
	${$key} = $value;
}
if($Troops >1){
	if($xp>$level){
		if($money>=$price){
			$AD2 = $S + $skill; // calc new skill level
			if(($Troops == 2 && $AD2 > 100) || ($Troops == 3 && $AD2 > 1000) || ($Troops == 4 && $AD2 > 3000) ||($Troops == 5 && $AD2 > 5000) ){
				die ("Troop limit reached!<br><a href='../Shop.php'>Back</a>");
			}
			$total = $money - $price; // for display purposes
			mysql_query("UPDATE Game SET Money = Money - $price, Attack = Attack + $attack, Defense = Defense + $defense, S = S + $skill WHERE user = '$username'");
			mysql_query("UPDATE Weapons SET R3 = 2 WHERE user = '$username'"); 
			echo "$username you have just bought $item! You now have $total Coins<br><a href='../Shop.php'>Back</a>";
		} else { echo "You do not have enough money to buy this item! <br><a href='../Shop.php'>Back</a>"; }
	} else { echo "You need 250 XP to buy this item! <br><a href='../Shop.php'>Back</a>"; }
} else { echo "You require a troop camp to use this item!<br><a href='../Shop.php'>Back</a>"; }
} else { echo "The item you selected does not exist!<br><a href='../Shop.php'>Back</a>"; }
?>
  </tr>
</table>
<p> </p>

 

I left in, but commented out, the variables being assigned from your queries that are not used, contain typo's, are duplicated, and have different letter case than what the program is actually using. There was also some logic errors and at least one wrong link and one HTML error in a link.

 

Edit: The background color is also different in the above code from what you have in your code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.