Page 1 of 1

Mysql_fetch_array(): supplied argument is not a valid MySQL

Posted: Fri Nov 13, 2009 3:36 am
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.

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

Posted: Fri Nov 13, 2009 3:43 am
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~

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

Posted: Fri Nov 13, 2009 3:52 am
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.

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

Posted: Fri Nov 13, 2009 3:53 am
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

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

Posted: Fri Nov 13, 2009 4:06 am
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>

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

Posted: Fri Nov 13, 2009 4:10 am
by Basic
Woohoo! It works :D

Thank you alot angelicodin! :) :mrgreen:

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

Posted: Fri Nov 13, 2009 4:13 am
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