Jump to content

Recommended Posts

can any1 tell me why this comes up for my code

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in XXX/buildings.php on line 23.

[code]
<?php
session_start();
//buildings.php

//open database connections
require_once('db.php');

if ($_POST['farm'] || $_POST['house'] || $_POST['mine'])
{
$farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0;
$house = $_POST['house'] > 0 ? $_POST['house'] : 0;
$mine = $_POST['mine'] > 0 ? $_POST['mine'] : 0;
$qry = "update buildings set nfarms = nfarms + " . $farm . " where userid='$userid', nhouse = nhouse + " . $house . " where userid='$userid', nfarm  = nfarm +  " . $farm . " where userid='$userid'";
$qry = mysql_query($qry);
}

//define planet size
define("TLAND", 200);
//get current number of buildings
$qry="select nfarms,nhomes,nmines from buildings where userid='$userid'";

$qry = mysql_query($qry);
$row = mysql_fetch_assoc($qry);  //line23
$nf = $row[nfarms];
$nh = $row[nhomes];
$nm = $row[nmines];

//check for free land sapce
$fland = ((TLAND) - ($nf+$nh+$nm));

//output current data
echo "Total land: " . TLAND;
echo "Free land: " . $fland;
echo "Number of Farms: " . $nf;
echo "Number of Homes: " . $nh;
echo "Number of Mines: " . $nm;

//allow building form
if ($fland>0)
{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input size="4" type="text" name="farm">
<input size="4" type="text" name="house">
<input size="4" type="text" name='mine'>
<input type="submit" name="submit" value="Build">
<?php
}
else
{
echo "No free land to build upon";
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/
Share on other sites

ahh good thx i fixed that error that came up a spelling mistake in my database, many thx.

new problem now is that using code seen above.

the number of buildings constructed should refresh
so when user says build 30 farms (nfarms) it will say Number of Farms 30, but nothing is showing up.

i know this is php help part but since i dont know if the problem is this or my databse ill post both.

script above

table used to create below

[code]
CREATE TABLE buildings (
  userid int(25) NOT NULL auto_increment,
  nfarms int(10) NOT NULL default '',
  nmines int(10) NOT NULL default '',
  nhomes int(10) NOT NULL default '',
  PRIMARY KEY  (userid)
) TYPE=MyISAM COMMENT='buildings';
[/code]

userid i was intending this to be same userid as in my users table.  otherwise how does my database store data for each individual user.  this is made primary key.  others are just number of farms, mines, e.t.c. for that user. 

any help with data not showing appreciated.
Link to comment
https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56925
Share on other sites

Your update query should be
[code]
$qry = "update buildings set
            nfarms = nfarms + $farm ,
            nhomes = nhomes + $house ,
            nmines  = nmines +  $mine
          where userid='$userid'";[/code]
Link to comment
https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56934
Share on other sites

ok thx for that i made the chagne like u said but nothing happened. still nothing appears.

Total land: 200Free land: 200Number of Farms: Number of Homes: Number of Mines:

is what it looks like as u can see no values appear for number of farms e.t.c.
also total land is defined in script so maybe it is the table.

im using the phpfreaks user script, just like at registration script
it does this:
[code]
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date)
VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now())") or die (mysql_error());

[/code]

do i also need to add insert records for the other tables i.e. so when the user gets created it also creates 1st records for buildings table, research table.  i think then this will make use of the user id i have added to other tables like buildings e.t.c. as the user id then in all should be same??  am i right here.  only been doing php 2-3 days so its all new terrain.
Link to comment
https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56943
Share on other sites

You can only update a record if that record exists. As your script  needs a buildings record for each user, create it when you add the new user. "userid" in user recird should be auto_increment but not the userid in buidings table as that will always be set to the same id as the new user.

[code]<?php
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address,
                username, password, info, signup_date)
VALUES ('$first_name', '$last_name', '$email_address',
                '$username', '$db_password', '$info2', now())") or die (mysql_error());
$newuserid = mysql_insert_id();

$sql = mysql_query ("INSERT INTO buildings VALUES ('$newuserid', 0, 0, 0)" );
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56962
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.