Problem Retrieving Data From MySql Table

All data returned as "Resource Id"s

Page 1 of 1

3 Replies - 690 Views - Last Post: 14 December 2009 - 10:12 PM Rate Topic: -----

#1 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Problem Retrieving Data From MySql Table

Posted 13 December 2009 - 04:43 PM

Hello,

Recently I have been trying to make my own online browser game somewhat similar to Travian, if anyone has ever played that. I have only recently started, and I am currently working on how a player would level up a resource field.

My problem is that my solution depends on retrieving the current data from the SQL table, and then update it. Unfortunately, all date comes back as a "Resource ID". I have tried finding some solutions online, but I have yet to find one that works.

My code is here. Please check it out:

<?php
session_start();
include("database.php");
include("createcamp.php");

/*This is trying to fetch the current level of "crop1," 
a resource field stored ina table called "Villages". 
The variable $cname has a value in "createcamp.php", 
and is used as the name for the player'svillage.*/

$level = mysql_query("SELECT crop1 FROM Villages 
WHERE camp='$cname'");
$newlevel = $level++;

/*This function is supposed to update the level of 
"crop1" I have hard-coded some values now,
 but they will be replaced with variables. 
The level is needed for this to happen*/

function resUpdt($resnum, $level){
	$conn;
	$result = mysql_query("UPDATE Villages SET $crop = '$newlevel' WHERE username = '$username'");
	//$row = mysql_fetch_array($result);
	//echo "$row";
	}
	
switch($_GET['action']) {
	case 'crop1': 			//case == $resnum;
		resupdt(crop1, 0);
		break;
}
?>


My problem is that without the SQL table returning values, it is impossible for me to complete the goal of this script.

Any and all ideas are appreciated as always.
Thanks in advance.

Cbeppe.

This post has been edited by Cbeppe: 13 December 2009 - 04:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem Retrieving Data From MySql Table

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Problem Retrieving Data From MySql Table

Posted 13 December 2009 - 06:23 PM

You're misunderstanding how the API works. If you read the documentation, you'll see that for SELECT queries, mysql_query() returns a result set resource. Hence the resource IDs you're seeing. The result set is passed to mysql_fetch_array() to fetch the actual data. So your first query should look something like this:
# First get the result set
$level_result = mysql_query("SELECT crop1 FROM Villages 
WHERE camp='$cname'");
# Next fetch the row of data
$level = mysql_fetch_array($level_result);
# Last, extract the field you want
$newlevel = $level['crop1']++;


Was This Post Helpful? 0
  • +
  • -

#3 Cbeppe  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 31
  • View blog
  • Posts: 215
  • Joined: 16-September 09

Re: Problem Retrieving Data From MySql Table

Posted 14 December 2009 - 06:39 AM

Thanks a lot for that, AdaHacker. It helps me, but unfortunately, I always seem to end up in a problem I don't understand.

By running this code, I am able to get the number in the "crop1" coloumn to equal "1". However, if I run it again, it should fetch the new level (which is 1) and add 1 to it, before inserting it into the table again. That means that the second time I run it, I should be inserting the number "2" into the table, but instead, it is inserting "1" again.

Can anyone see where I'm going wrong?

Thanks a bunch.

Here is my new code:
<?php
include("database.php");
include("createcamp.php");

function resUpdt($resnum){
		$conn;
		$level_result = mysql_query("SELECT '$resnum' FROM Villages 
		WHERE camp = 'Player Camp'");
		$level = mysql_fetch_array($level_result);
		$newlevel = ($level['crop1'] + 1);
		$result = mysql_query("UPDATE Villages SET $resnum = '$newlevel' WHERE camp = 'Player Camp'");
		}
	
switch($_GET['action']) {
	case 'crop1': 
		resUpdt(crop1);
		break;
/*Here is a large switch statement. I only post
 the first one since that is what I'm
 experimenting with. */
}
?>


This post has been edited by Cbeppe: 14 December 2009 - 06:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Problem Retrieving Data From MySql Table

Posted 14 December 2009 - 10:12 PM

Try to use mysql_error at the end of your query, and also try to remove parentheses () from $level,
        $level_result = mysql_query("SELECT '$resnum' FROM Villages WHERE camp = 'Player Camp'")or die(mysql_error());
        $level = mysql_fetch_array($level_result);
        $newlevel = $level[0] + 1;
        $result = mysql_query("UPDATE Villages SET $resnum = '$newlevel' WHERE camp = 'Player Camp'")or die(mysql_error());


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1