Jump to content

Warning: mysql_fetch_array() expects parameter 1 to be resource


naveendk.55

Recommended Posts

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" />      

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

</form>



Link to comment
Share on other sites

mysql_query returns a resource on success and false on failure. As the parameter passed is not a resource states that your query is indeed failing due to an error of some kind.

 

Please use the following format:

$result = mysql_query ( "SELECT Name FROM empdata ORDER BY Name ASC" ) or trigger_error ( mysql_error ( ) );

What this does, if an error were to occur it'll throw an error. The key function here is mysql_error.

 

 

Link to comment
Share on other sites

I tried your function and triggered two errors as you said.

 

a) "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\projectphp\newEmptyPHPWebPage1.php on line 30". 

 

Line 30 on the code is  :    while ($row = mysql_fetch_array($result)) {

 

b) "Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1 in C:\wamp\www\projectphp\newEmptyPHPWebPage1.php on line 29" .. Line 29 is mentioned below.

 

                        $result = mysql_query("SELECT Name FROM database") or trigger_error ( mysql_error ( ) );

 

 

I think syntax wise it is correct. Let me know if I'm making any mistakes still. Thanks.

 

Link to comment
Share on other sites

I tried and also included some additional steps to get the desired results. Now, I'm able to get the names in dropdown list. However, unique ID is not displaying after selecting name from the drop down list. I tried the below code.

 

 



<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, userid 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" />      

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

</form>

</div>

</div>

</body>

</html>


Link to comment
Share on other sites

Sure, I have a table in mysql with two columns  (userid and name). I want to display all names in drop down menu. Once the end user select name from drop down menu, the userid should be display automatically in the next textbox filed available.

 

I tried the above code to get the userid once the name is selected. It shows all the name from database but not theuser id once the name is selected.  I hope it is clear now.

Link to comment
Share on other sites

 

Try something like this.

 

 

The value in the dropdown is the ID of the user.


<script type="text/javascript">
    function showid ( val )
    {
          document.forms[0].elements['id'].value = val;
    }
</script>
<form>
    <select onchange="showid(this.value)">
        <option value="-1" selected disabled>Select One</option>
        <option value="1">Username 1</option>
        <option value="2">Username 2</option>
    </select>
    <input type="text" name="id" />
</form>


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.