Mysql_fetch_array(): supplied argument is not a valid MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Basic
Forum Newbie
Posts: 5
Joined: Fri Nov 13, 2009 3:32 am

Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by Basic »

Hello.

I'm pretty new into PHP scripting, and I am currently working on a script, where I can select a specific user from a list, and afterward edit them.
But I am getting the following error at the moment:
mysql_fetch_array(): supplied argument is not a valid MySQL result resource.. on line 9

This is the current code:

Code: Select all

<form name="formedituser" method="post" action="index.php?page=admin&sub=editcharacter">
<?php
$query="SELECT * FROM $tbl_name";
$result = mysql_query($query);
 
if($_POST["user"] != "") {
$selecteduser = $_POST["user"];
$userquery=mysql_query("SELECT * FROM $tbl_name WHERE username='$selecteduser");
$cuser = mysql_fetch_array($userquery);
}
 
echo 'User:</br>';
echo '<select name="user">';
while($nt=mysql_fetch_array($result)){
echo '<option value="' . $nt['username'] . '">' . $nt['username'] . '</option>';
}
echo '</select>';
?>
<input type="submit" name="Submit" value="Select">
</br></br>
Username:
<input name="username" type="text" id="username" value="<?php echo $cuser['username']; ?>">
 
</br>
 
</form>
The error occurs whenever I press the submit at the form.
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by angelicodin »

Hi there, I'm vary new as well but I've ran into this problem before. Now I'm still trying to break down your code but you can try making the while array an associative one.

Code: Select all

<?
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
   //results output
}
?>
Let me know how that works.

EDIT: I forgot to mention that you always could do mysql_fetch_row and just a numeric array ~shurg~
Basic
Forum Newbie
Posts: 5
Joined: Fri Nov 13, 2009 3:32 am

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by Basic »

Sorry, I didn't quite get what you mean :P
I changed the code to this now:

Code: Select all

 
<form name="formedituser" method="post" action="index.php?page=admin&sub=editcharacter">
<?php
$query="SELECT * FROM $tbl_name";
$result = mysql_query($query);
 
if($_POST["user"] != "") {
$selecteduser = $_POST["user"];
$userquery=mysql_query("SELECT * FROM $tbl_name WHERE username='$selecteduser");
$cuser = mysql_fetch_array($userquery, MYSQL_ASSOC);
}
 
echo 'User:</br>';
echo '<select name="user">';
while($nt=mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<option value="' . $nt['username'] . '">' . $nt['username'] . '</option>';
}
echo '</select>';
?>
<input type="submit" name="Submit" value="Select">
</br></br>
Username:
<input name="username" type="text" id="username" value="<?php echo $cuser['username']; ?>">
 
</br>
 
</form>
 
Could you please give an example, of how you would change the code, with the stuff you made?
I'm stilling getting the same error.
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by angelicodin »

yeah give me a few mins here and I'll try to work something up. Still new so please take what I do with a grain of salt ;p
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by angelicodin »

Try this and let me know. I changed some things around for me to look at easier, sorry.

Code: Select all

<form name="formedituser" method="post" action="index.php?page=admin&sub=editcharacter">
<?php
$query="SELECT * FROM `".$tbl_name."`";
$result = mysql_query($query);
 
if($_POST["user"] != "") {
$selecteduser = $_POST["user"];
$userquery = mysql_query("SELECT * FROM `".$tbl_name."` WHERE username = '".$selecteduser."'");
$cuser = mysql_fetch_array($userquery, MYSQL_ASSOC);
}
 
echo "User:</br>";
echo "<select name=\"user\">";
while($nt=mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<option value=\"" . $nt['username'] . "\">" . $nt['username'] . "</option>";
}
echo "</select>";
?>
<input type="submit" name="Submit" value="Select">
</br></br>
Username:
<input name="username" type="text" id="username" value="<?php echo $cuser['username']; ?>">
</br>
</form>
Basic
Forum Newbie
Posts: 5
Joined: Fri Nov 13, 2009 3:32 am

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by Basic »

Woohoo! It works :D

Thank you alot angelicodin! :) :mrgreen:
User avatar
angelicodin
Forum Commoner
Posts: 81
Joined: Fri Nov 13, 2009 3:17 am
Location: Oregon, USA

Re: Mysql_fetch_array(): supplied argument is not a valid MySQL

Post by angelicodin »

yeah not a problem, do you see where I changed the query's so they are reading the variables? What was going on before I think was they where trying to be read as a string. Want to make sure you learn something ;p
Post Reply