Jump to content

[SOLVED] Warning: mysql_fetch_array():


brandon99919

Recommended Posts

Here's the error that shows up:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 28

 

Here's the code itself:

<?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE owner_id = '$userid' and p_slot = '1'");
			   $fetch = mysql_fetch_array($query);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 1' />";
						  }
						  ?>

 

 

and line 28 is the mysql_fetch_array function

Link to comment
Share on other sites

You have to execture the query first using mysql_query($query);

 

<?php
               $query = mysql_query("SELECT * FROM pokemon WHERE owner_id = '$userid' and p_slot = '1'");
$result = mysql_query($query);
               $fetch = mysql_fetch_array($result);
               $p_icon1 = $fetch['p_icon'];
               $p_name1 = $fetch['p_name'];
               
               if (empty($p_name1)) {
                       echo "<img src='{$p_icon}' alt='{$p_name1}' />";
                       } else {
                          echo "<img src='pokemonball.gif' alt='slot 1' />";
                       }
                       ?>

Link to comment
Share on other sites

sorry, little typo;

 

<?php
               $query = mysql_query("SELECT * FROM pokemon WHERE owner_id = '$userid' and p_slot = '1'");
$result = mysql_result($query);
               $fetch = mysql_fetch_array($result);
               $p_icon1 = $fetch['p_icon'];
               $p_name1 = $fetch['p_name'];
               
               if (empty($p_name1)) {
                       echo "<img src='{$p_icon}' alt='{$p_name1}' />";
                       } else {
                          echo "<img src='pokemonball.gif' alt='slot 1' />";
                       }
                       ?>


Link to comment
Share on other sites

yea the database connection is good and when I tested the query it got back an error saying that the owner_id column doesn't exist. Turns out the column is actually called p_owner, so I changed it to that  and ran the query and it was successful. And yea, the userid variable is set. I still get the same error even tho I fixed the problem with the where clause.  ???

Link to comment
Share on other sites

If the query was successfuly you wouldn't be getting that error. Try this,

 

<?php
$query = mysql_query("SELECT * FROM `pokemon` WHERE `p_owner`=$userid AND `p_slot`= 1");
$result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);
$fetch = mysql_fetch_array($result);
$p_icon1 = $fetch['p_icon'];
$p_name1 = $fetch['p_name'];

if (empty($p_name1)) {
echo "<img src='{$p_icon}' alt='{$p_name1}' />";
} else {
echo "<img src='pokemonball.gif' alt='slot 1' />";
}
?>

Link to comment
Share on other sites

You have to forgive me, I've been working on stuff today that has led me to do the oppsite than help.

 

Fixed Script bellow;

 

<?php
$query = mysql_query("SELECT * FROM `pokemon` WHERE `p_owner`=$userid AND `p_slot`= 1") or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);
$fetch = mysql_fetch_array($query);
$p_icon1 = $fetch['p_icon'];
$p_name1 = $fetch['p_name'];

if (empty($p_name1)) {
   echo "<img src='{$p_icon}' alt='{$p_name1}' />";
} else {
   echo "<img src='pokemonball.gif' alt='slot 1' />";
}
?>

Link to comment
Share on other sites

It's likely that the code you are posting is part of a loop and that the actual line with the error is not the posted code. Post your code from the start of your file through the end of any loop that is involved in what you are doing.

 

The $db variable in the mysql_error() is also not helping and should be removed because it is not relevant to the OP's code.

Link to comment
Share on other sites

here's the full code:

 

		       <a onClick="subMenu('general_submenu', this);">General</a>
			   <div id='general_submenu' class='sub_menu'>
			        <a href='/index.php?page=news'>News</a>
                        <a href='/index.php?page=promo'>Promo</a>
                        <a href='/index.php?page=staff'>Staff</a>
                        <a href='/index.php?page=logout'>Logout</a>
			   </div>
			   
			   <a onClick="subMenu('myAccount_submenu', this);">My Account</a>
			   <div id='myAccount_submenu' class='sub_menu'>
			        <a href='/index.php?page=profile'>My Profile</a>
			   </div>
                   <a onClick="subMenu('myPokemon_submenu', this);">My Pokémon</a>
			   <div id='myPokemon_submenu' class='sub_menu'>
			        <a href='/index.php?page=stats'>Starter Stats</a>
			   </div>
                   <?php if($user_level == "Admin") { ?>
                   <a onClick="subMenu('adminCP_submenu', this);">Admin CP</a>
                   <div id='adminCP_submenu' class='sub_menu'>
			        <a href='/index.php?page=gen'>Promo</a>
			   </div><?php }?>
			   
			   <a href='#'>Master Quest</a>
                   <div align='center'>
                   <?php
			   include('includes/basefile.php');
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '1'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 1' />";
						  }
						  ?>
						  <?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '2'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 2' />";
						  }
						  ?>
						  <?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '3'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 3' />";
						  }
						  ?><br />
						  <?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '4'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 4' />";
						  }
						  ?>
						  <?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '5'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 5' />";
						  }
						  ?>
						  <?php
			   $query = mysql_query("SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '6'");
			   $result = mysql_query($query);
			   $fetch = mysql_fetch_array($result);
			   $p_icon1 = $fetch['p_icon'];
			   $p_name1 = $fetch['p_name'];
			   
			   if (empty($p_name1)) {
						  echo "<img src='{$p_icon}' alt='{$p_name1}' />";
						  } else {
							  echo "<img src='pokemonball.gif' alt='slot 6' />";
						  }
						  ?>

Link to comment
Share on other sites

And the php error message from that code is?

 

There is no way that code can work, because there are double mysql_query() statements in it.

 

The easiest fix would be to change each of the leading mysql_query() statements so that the line just sets the $query variable with the query statement -

 

$query = "SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '1'";

 

And you should be executing a single query that returns all the rows you want as a set, then you loop through them. Duplicating code usually wastes time due to the typo's made while fixing up the different values in it.

 

 

Link to comment
Share on other sites

here's the errors:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 29

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 55

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 68

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 81

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in F:\hshome\brandon9\pcprpg.com\menu_user.php on line 94

 

Link to comment
Share on other sites

Back to ground zero. All that means is your query is failing and no one knows why. For troubleshooting purposes only, replace the lines making up your first query with the following -

 

$query = "SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot = '1'";
$result = mysql_query($query) or die("Query failed: $query, " . mysql_error());

Link to comment
Share on other sites

I'm guessing the original problem was the column name.

 

You can replace all that php code with the following single query and a loop -

 

Untested, but should work -

<?php
include('includes/basefile.php');
$query = "SELECT * FROM pokemon WHERE p_owner = '$userid' and p_slot IN('1','2','3','4','5','6') ORDER BY p_slot";
$result = mysql_query($query);
$i = 1;
while($row = mysql_fetch_array($result)){
$p_icon = $row['p_icon'];
$p_name = $row['p_name'];
if(!empty($p_name)){
	echo "<img src='{$p_icon}' alt='{$p_name}' />";
} else {
	echo "<img src='pokemonball.gif' alt='slot {$i}' />";
}
$i++;
}
?>

Link to comment
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.