PHP MySql Not working

jerryology

Acquaintance
Joined
Jun 11, 2010
Messages
25
Reaction score
0
FP$
6
Hi, I have a game I am making, and in one of the files, php will decide if it wants to exacute the sql. If someone could look at it, that would be nice.
Code:
<?php
if (!isset($_SESSION))
{
	session_start();
}
$bullshit = true; //Cause im mad and it's not working
if ($bullshit) {
include "header.php";
$con2 = mysql_connect($dhost, $dusername, $dpassword) or die("Cannot Connect"); 
mysql_select_db($ddatabase, $con2);
$id = 100;//halen("select id from us_members where username='".$_COOKIE['Username']."' ");
$iId = 5;//halen("select itemId from us_items where ownerId='".$id."' && id='".$_GET['itemid']."'");
$top = 't';//halen("SELECT type from us_itemTypes WHERE itemid='".$iId."'");
$inuse = halen("SELECT inuse FROM us_items WHERE id='50'");

if ($iId == '' || $iId == null) {
echo "You don't own this item!";
include "footer.php";
exit;
}
$item = mysql_query("select * from us_itemTypes where itemId=$iId limit 1");
$item = mysql_fetch_array($item);

if ($inuse == 0){

if ($top == "t"){
mysql_query("Update us_members set c_top='5' where username='jerry'",$con2);
}

mysql_query("Update us_items set inuse='1' where id='50'",$con2);
mysql_query("Update us_members set def=def+10 where username='jerry' limit 1",$con2);
echo "Item has been equipped!";

} else {

if ($top == "t"){
mysql_query("Update us_members set c_top='0' where username='jerry'",$con2);
}
mysql_query("Update us_items set inuse='0' where id='50'",$con2);
mysql_query("Update us_members set def=def-10 where username='jerry' limit 1",$con2);
echo "Item has been un-equipped!";

}
}
include "footer.php";
That is the whole page, but
Code:
if ($inuse == 0){

if ($top == "t"){
mysql_query("Update us_members set c_top='5' where username='jerry'",$con2);
}

mysql_query("Update us_items set inuse='1' where id='50'",$con2);
mysql_query("Update us_members set def=def+10 where username='jerry' limit 1",$con2);
echo "Item has been equipped!";

} else {

if ($top == "t"){
mysql_query("Update us_members set c_top='0' where username='jerry'",$con2);
}
mysql_query("Update us_items set inuse='0' where id='50'",$con2);
mysql_query("Update us_members set def=def-10 where username='jerry' limit 1",$con2);
echo "Item has been un-equipped!";
}
is the part not working. :cry:
 
jerryology said:
PHP:
<span class="syntaxdefault"><br /></span><span class="syntaxkeyword">if&nbsp;(</span><span class="syntaxdefault">$inuse&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">){<br /><br />if&nbsp;(</span><span class="syntaxdefault">$top&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxstring">"t"</span><span class="syntaxkeyword">){<br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_members&nbsp;set&nbsp;c_top='5'&nbsp;where&nbsp;username='jerry'"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br />}<br /><br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_items&nbsp;set&nbsp;inuse='1'&nbsp;where&nbsp;id='50'"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_members&nbsp;set&nbsp;def=def+10&nbsp;where&nbsp;username='jerry'&nbsp;limit&nbsp;1"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br />echo&nbsp;</span><span class="syntaxstring">"Item&nbsp;has&nbsp;been&nbsp;equipped!"</span><span class="syntaxkeyword">;<br /><br />}&nbsp;else&nbsp;{<br /><br />if&nbsp;(</span><span class="syntaxdefault">$top&nbsp;</span><span class="syntaxkeyword">==&nbsp;</span><span class="syntaxstring">"t"</span><span class="syntaxkeyword">){<br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_members&nbsp;set&nbsp;c_top='0'&nbsp;where&nbsp;username='jerry'"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br />}<br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_items&nbsp;set&nbsp;inuse='0'&nbsp;where&nbsp;id='50'"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">mysql_query</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Update&nbsp;us_members&nbsp;set&nbsp;def=def-10&nbsp;where&nbsp;username='jerry'&nbsp;limit&nbsp;1"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$con2</span><span class="syntaxkeyword">);<br />echo&nbsp;</span><span class="syntaxstring">"Item&nbsp;has&nbsp;been&nbsp;un-equipped!"</span><span class="syntaxkeyword">;<br />}<br />&nbsp;</span><span class="syntaxdefault"></span>

I'm not great with PHP yet but it may have something to do with the username='jerry' string on a few of the lines which may be causing an error. Like I said though I'm not great with PHP. Also it may be an error in after the } else {, in that section you may not need to update anything. Not to sure though.
 
ok. That is mysql, and that is hardcoded because we wanted to see if it was the GET vars.
The else unequips it
 
What's not working?

Three things you should do with your queries:
1. Capitalize keywords. Example: update to UPDATE
2. surround column names and table names with the ` character. not a single quote but the ` to the left of the 1 on the keyboard.
3. Change: "mysql_query('something');" to "mysql_query('something') or die(mysql_error());"
 
See like I said


"mysql_query('turn cosmic to 1337') or die(mysql_worlderror());"
 
Cosmic, we tried all three. It turned out it just didn't like the conditional, so we made two files, and it works.
 
Um... I can try to help if you still need it, for one, like Cosmic said, just use this as your mysql_query()

Or here, I have no idea what you are doing, but I'll try to help out
Code:
if ($inuse == 0) {
    if ($top == "t") {
        mysql_query("UPDATE us_members SET c_top = '5' WHERE username = 'jerry'");
    }
    mysql_query("UPDATE us_items SET inuse = '1' WHERE id = '50'");
    mysql_query("UPDATE us_members SET def += 10 WHERE username = 'jerry'");
    echo "Item has been equipped!";
} else {
    if ($top == "t") {
        mysql_query("UPDATE us_members SET c_top = '0' WHERE username = 'jerry'");
    }
    mysql_query("UPDATE us_items SET inuse = '0' WHERE id='50'");
    mysql_query("UPDATE us_members SET def -= 10 WHERE username='jerry'");
    echo "Item has been un-equipped!";
}

1) no idea why you were trying to use a connection inside the mysql_query, make sure you just connect like normal - i always do the following when working with databases

top of page
Code:
// Connect to database
include ('include/db.php');

db.php
Code:
// Connect to database
mysql_connect("IP OR localhost", "USERNAME", "PASSWORD");

// Select database
mysql_select_db("DATABSE NAME");

Than, your queries will be set to connect to that database.

I decided to help you and clean up that first part of code as well
Code:
<?php
// Always use session_start();
session_start();

$bull**** = true; //Cause im mad and it's not working
if ($bull****) {
    include ('header.php');
    include ('db.php'); // Look at above, you might want to just have this part put into the header.php for ease of access, idk idc

    $id = 100; //halen("SELECT id FROM us_members WHERE username = '".$_COOKIE['Username']."'");
    $iId = 5;//halen("SELECT itemId FROM us_items WHERE ownerId = '".$id."' AND id = '".$_GET['itemid']."'");
    $top = 't';//halen("SELECT type FROM us_itemTypes WHERE itemid = '".$iId."'");
    $inuse = halen("SELECT inuse FROM us_items WHERE id='50'"); // << WTF IS HALEN?

    if(mysql_num_rows($iId) <= 0) {
    echo "You don't own this item!";
    include ('footer.php');
    exit;
    }

    $item = mysql_query("SELECT * FROM us_itemTypes WHERE itemId = '". $iId ."' LIMIT 1");
    $item = mysql_fetch_array($item);

    if ($inuse == 0) {
        if ($top == "t") {
            mysql_query("UPDATE us_members SET c_top = '5' WHERE username = 'jerry'");
        }
        mysql_query("UPDATE us_items SET inuse = '1' WHERE id='50'");
        mysql_query("UPDATE us_members SET def += 10 WHERE username = 'jerry'");
        
        echo "Item has been equipped!";
    } else {
        if ($top == "t") {
            mysql_query("UPDATE us_members SET c_top = '0' WHERE username = 'jerry'");
        }
        mysql_query("UPDATE us_items SET inuse = '0' WHERE id='50'");
        mysql_query("UPDATE us_members SET def -= 10 WHERE username = 'jerry'");
        echo "Item has been un-equipped!";
    }
}
include ('footer.php');

Also, you might want to read up more about PHP and MySQL before you start to make a game with it, you will be hacked assuming you don't use techniques to prevent it.

Also, from above, wth is halen()? Unless that is a function, which calls a mysql_query (which would be stupid) i would change that, I left it how it is though. Um, don't use LIMIT's when in an UPDATE, there is no need if the username's are unique - let me know if that works ouut for ya
 
Back
Top Bottom