/    Sign up×
Community /Pin to ProfileBookmark

While loop and mysql queries

[code=php]<?php
session_start();
require_once(‘mysql_connect.php’);
if (isset($_SESSION[‘username’])){
$user = $_SESSION[‘username’];
//////// Select user information from database
$userquery = “SELECT * FROM `users` WHERE `username` = ‘$user'”;
$userresult = mysql_query($userquery);
while ($userrow = mysql_fetch_array($userresult)){
$username = $userrow[‘username’];
$userid = $userrow[‘id’];
$character1 = $userrow[‘character1’];
$character2 = $userrow[‘character2’];
$character3 = $userrow[‘character3’];
//////// Select characters from database
$charquery = “SELECT * FROM `characters` WHERE `id` = ‘$character1’ OR `id` = ‘$character2’ OR `id` = ‘$character3′”;
$charresult = mysql_query($charquery);
while ($charrow = mysql_fetch_array($charresult)) {
$race = $charrow[‘race’];
$gender = $charrow[‘gender’];
//////// Select inventory from database
$invquery = “SELECT * FROM `inventory` WHERE `userid` = ‘$userid'”;
$invresult = mysql_query($invquery);
while ($invrow = mysql_fetch_array($invresult)) {
$itemid = $invrow[‘itemid’];
$charid = $invrow[‘charid’];
$quantity = $invrow[‘quantity’];
//////// Select items from database
$itemquery = “SELECT FROM `items` WHERE `id` = ‘$itemid'”;
$itemresult = mysql_query($itemquery);
while ($itemrow = mysql_fetch_array($itemresult)){
$etype = $itemrow[‘etype’];
$iattack = $itemrow[‘iattack’];
$idefense = $itemrow[‘idefense’];
$imind = $itemrow[‘imind’];
$iaccuracy = $itemrow[‘iaccuracy’];
$ispeed = $itemrow[‘ispeed’];

do {
echo “Charid is set for”. $itemid;
}while (isset($charid));

}

}

}
}

}else{
echo “log in”;
}

?>[/code]

This code keeps on returning this error for me

[QUOTE]

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/LinuxPackage04/ch/ao/sj/chaosjump.com/public_html/charconfig.php on line 31

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/LinuxPackage04/ch/ao/sj/chaosjump.com/public_html/charconfig.php on line 31

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites/LinuxPackage04/ch/ao/sj/chaosjump.com/public_html/charconfig.php on line 31

[/QUOTE]

But I can’t see what it is I did wrong. The line in question is this.

[code=php] while ($itemrow = mysql_fetch_array($itemresult)){
[/code]

And then finally, the do while is supposed to echo the values of the items that have a charid value assigned to them, if that makes sense. Would this work in theory since I’ve assigned the the mysql arrays to variables? If that doesn’t make much sense just let me know, I’ll try to explain it in more detail.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@freebird1127Mar 27.2008 — Stick an echo mysql_error(); under the query and see what pops out.
Copy linkTweet thisAlerts:
@NogDogMar 27.2008 — Your $itemquery SQL has no column(s) after the "SELECT".

You should always check to see if the return value of your mysql_query() commands is false. If so you can log an error with information such as the actual query string and the output of mysql_error(), and display an appropriate error to the user.
×

Success!

Help @LyonB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...