...

View Full Version : "Warning: mysql_fetch_array() expects parameter 1 to be resource



naveendk.55
09-18-2011, 11:19 AM
Hi I'm new to php and trying to get all the list of names in a drop down menu from database. I tried the below code and received error "Warning: mysql_fetch_array() expects parameter 1 to be resource". Also there is a unique number assigned to each name in the database. I want to display this unique number in ID filed once the appropriate name is selected. Help !!!!!.






<table id="table">

<tr>

<td background-color="green"> Name </td>

<td> <input type="text" name="evaluator_name" id="evaluator_name"/> </td>

</tr>

<tr>

<td> Agent Name </td>

<td> <select name="aname" id="aname" style="width: 147px">
<?php
$result = mysql_query("SELECT Name FROM empdata ORDER BY Name ASC");
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row['Name'] . "</option>";
}
?>

</select></td>
</tr>
<tr>
<td> ID </td>
<td colspan='3'> <input type="text" name="sapid" id="sapid" value="" />
</td>

</tr>

</table>

<br/> <br/>

<center> <input name="submit" type="submit" value="Submit Details" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input name="Reset" type="reset" value="Reset"> </center>

</form>

thesam101
09-18-2011, 12:13 PM
Hi naveendk.55

Addressing the error first, its probably the query thats failing, double check the syntax. :thumbsup:

And as for the second question, am i right in saying you wish the ID field to be automically updated to show the appropriate ID when a name is chosen from the drop down menu?

naveendk.55
09-18-2011, 12:54 PM
About query, let me know if I made any syntax mistake.

Yes, I want the ID to be popped up automatically after selecting the name.

BluePanther
09-18-2011, 01:17 PM
You can find out easily yourself - add or die(mysql_error()) inbetween the closing ) of your mysql_query function and the ;. That will kill your code and show you the error:

mysql_query(...) or die(mysql_error());

naveendk.55
09-18-2011, 03:43 PM
I tried that. It didn't show any error code. However, it is not displaying any datafrom database. Also the ID text box is not displayed on the form.




<table id="table">

<tr>

<td background-color="green"> Name </td>

<td> <input type="text" name="evaluator_name" id="evaluator_name"/> </td>

</tr>

<tr>

<td> Agent Name </td>

<td> <select name="aname" id="aname" style="width: 147px">
<?php
$result = mysql_query("SELECT Name FROM empdata ORDER BY Name ASC") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row['Name'] . "</option>";
}
?>

</select></td>
</tr>
<tr>
<td> ID </td>
<td colspan='3'> <input type="text" name="sapid" id="sapid" value="" />
</td>

</tr>

</table>

<br/> <br/>

<center> <input name="submit" type="submit" value="Submit Details" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input name="Reset" type="reset" value="Reset"> </center>

</form>

BluePanther
09-18-2011, 04:05 PM
Is it still displaying the 'expects resource' error? Is the table definitely called empdata? Is the field definitely called Name (not name)?

naveendk.55
09-18-2011, 06:23 PM
hi That resolved the error 'expects resource. However, how about getting the unique ID after selecting the user name.

Inigoesdr
09-18-2011, 06:52 PM
You need to add it to your query:

SELECT ID, Name FROM empdata ORDER BY Name ASC
Where ID is your unique id field. Then you can access it inside of your loop with $row['ID'] just like you do with 'Name'.

thesam101
09-18-2011, 07:36 PM
hi That resolved the error 'expects resource. However, how about getting the unique ID after selecting the user name.

It may have stopped the error message, but if no data is being retrived it still isn't right. Have you check the table names and column names as BluePanther said?

As for the ID, assuming every name is unique, there are two ways of doing this really, javascript and PHP.

With PHP, you would submit the form onchange of the dropdown (which yes, i suppose would use javascript also for the automatic submission, but majority PHP), and pass the name selected as a GET or POST, and then check for it and display the appropriate ID in the text box, using something like this:


<td> ID </td>
<td colspan='3'> <input type="text" name="sapid" id="sapid" value="<?php
$r = mysql_query("select * from empdata where name = '$_GET[chosenName]'");
$t = mysql_fetch_row($r);
echo $t['ID'];
?>" />
</td>

Where chosenName would be the variable passed in the GET (from the submitted drop down).

You would also then be free to fill in other form details automatically if you wish, using the data retrived.

Alternatively you could use javascript.

Out of the ways to do it, I would suggest creating an array of IDs, and then updating that way.

something like this script section put in the head should do it:



<script type="text/javascript">
var ids = new Array();

window.onload = function() {
document.getElementById('aname').onchange = updateID;

<?php
$names = mysql_query("SELECT Name FROM empdata ORDER BY Name ASC") or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
echo "names[names.length] = $row['Name']";
}
?>


}

function updateID() {
document.getElementById('sapid').value = ids[document.getElementById('aname').selectedIndex];
}

</script>


All freetyped, and untested, so excuse any errors. :thumbsup:

naveendk.55
09-19-2011, 03:06 PM
I tried the below steps and ID filed is displaying as "undefined". All are working except the ID field.




<table id="table">
<tr>
<td background-color="green"> Evaluator Name </td>
<td> <input type="text" name="evaluator_name" id="evaluator_name"/> </td>
</tr>
<tr>
<td> Agent Name </td>
<td> <select name="aname" id="aname" style="width: 147px">
<?php
$result = mysql_query("SELECT name, userid FROM empdata where role='user' ORDER BY name ASC");
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row['name'] . "</option>";
}
?>
</select></td>
</tr>
<tr>
<td> SAP ID </td>
<td colspan='3'> <input type="text" name="sapid" id="sapid" value="" onchange="updateID()">
<script type="text/javascript">

var ids = new Array();
window.onload = function() {
document.getElementById('aname').onchange = updateID;

<?php
$names = mysql_query("SELECT userid FROM empdata") or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
echo "names[names.length] = $row[userid]";
}
?>

}
function updateID() {
document.getElementById('sapid').value = ids[document.getElementById('aname').selectedIndex];
}
</script>
</td>
</tr>
</table>

thesam101
09-19-2011, 03:37 PM
Hi naveendk.55

That <script> section I provided should go in the head section like I stated :thumbsup:

i.e. somewhere between the <head> and </head> tags.

naveendk.55
09-19-2011, 07:53 PM
Hi, I tried that and it works perfectly except displaying the unique number after selecting the name from drop down list.





<html>
<head>
<title>Form IE</title>

<script type="text/javascript">

var ids = new Array();
window.onload = function() {
document.getElementById('aname').onchange = updateID;

<?php
$names = mysql_query("SELECT userid FROM empdata") or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
echo "names[names.length] = $row[userid]";
}
?>
}
function updateID() {
document.getElementById('sapid').value = ids[document.getElementById('aname').selectedIndex];
}
</script>

</head>

<body id="body"> <br/><br/>

<form name="audit" method="POST" action="">

<table id="table">

<tr>

<td background-color="green"> Name </td>

<td> <input type="text" name="evaluator_name" id="evaluator_name"/> </td>

</tr>

<tr>

<td> Agent Name </td>

<td> <select name="aname" id="aname" style="width: 147px">
<?php
$result = mysql_query("SELECT name FROM empdata") or trigger_error ( mysql_error ( ) );
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row['name'] . "</option>";
}
?>

</select></td>
</tr>
<tr>
<td> ID </td>
<td colspan='3'> <input type="text" name="sapid" id="sapid" value="" onchange="updateID()" />
</td>

</tr>

</table>

<br/> <br/>

<center> <input name="submit" type="submit" value="Submit Details" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input name="Reset" type="reset" value="Reset"> </center>

</form>

</div>

</div>

</body>

</html>

thesam101
09-19-2011, 08:27 PM
Hi naveendk.55

I'm very sorry, i made a slight error (i did say it was freetyped lol)


This:


echo "names[names.length] = $row[userid]";

should be this:


echo "ids[ids.length] = $row[userid]";

:)

naveendk.55
09-19-2011, 08:37 PM
Nope, No output even after changing the code to ids.

thesam101
09-19-2011, 10:08 PM
Nope, No output even after changing the code to ids.

Oh dear, i'm sorry, my freetyping seem to have struck twice, this:


while ($row = mysql_fetch_array($result)) {
echo "ids[ids.length] = $row[userid]";
}

should be this:


while ($row1 = mysql_fetch_array($names)) {
echo "ids[ids.length] = $row1[userid]";
}

If it doesn't work after that change, could you provide the rendered source please? (the code shown when you goto view>source)

Inigoesdr
09-19-2011, 10:16 PM
You're missing a semicolon at the end of the JS line.

naveendk.55
09-20-2011, 12:29 PM
Hi I tried that also but without any results. :confused:

naveendk.55
09-20-2011, 01:24 PM
I would like to thank one and all who responded to this post. I resolved it by assigning a userid as value in options tag and then calling this using JAVASCRIPT.

This is the method that I used.


function showid ( val )
{
document.forms[0].elements['sapid'].value = val;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum