Newbiephper Posted July 12, 2006 Share Posted July 12, 2006 can any1 tell me why this comes up for my codeWarning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in XXX/buildings.php on line 23.[code]<?phpsession_start();//buildings.php//open database connectionsrequire_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 sizedefine("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 dataecho "Total land: " . TLAND;echo "Free land: " . $fland;echo "Number of Farms: " . $nf;echo "Number of Homes: " . $nh;echo "Number of Mines: " . $nm;//allow building formif ($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] Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/ Share on other sites More sharing options...
Barand Posted July 12, 2006 Share Posted July 12, 2006 You will get that messagge if your original query had an error.Try[code]$qry = mysql_query($qry) or die (mysql_error() );[/code]to see if there indeed was an error and what the error was. Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56917 Share on other sites More sharing options...
Newbiephper Posted July 12, 2006 Author Share Posted July 12, 2006 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 abovetable 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. Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56925 Share on other sites More sharing options...
Barand Posted July 12, 2006 Share Posted July 12, 2006 Your update query should be[code]$qry = "update buildings set nfarms = nfarms + $farm , nhomes = nhomes + $house , nmines = nmines + $mine where userid='$userid'";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56934 Share on other sites More sharing options...
Newbiephper Posted July 12, 2006 Author Share Posted July 12, 2006 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 scriptit 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. Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56943 Share on other sites More sharing options...
Barand Posted July 12, 2006 Share Posted July 12, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14407-mysql_fetch_assoc/#findComment-56962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.