Jump to content

Archived

This topic is now archived and is closed to further replies.

naveendk.55

Warning: mysql_fetch_array() expects parameter 1 to be resource

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>



Share this post


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

 

 

Share this post


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

 

Share this post


Link to post
Share on other sites

Your use of "database" is not allowed, because it's part of MySQL syntax. You can prevent this from happening by using back ticks around the table name eg: `database` or changing the table name to something different.

Share this post


Link to post
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>


Share this post


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

Share this post


Link to post
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>


Share this post


Link to post
Share on other sites

Hi, please read my post. I tried my best to explain you in details.  Both the values, name and the unique ID, is displayed from database. If I select the name, its related Unique ID should be displayed. Your (TrueColors) code didn't show both the values.

 

 

Share this post


Link to post
Share on other sites

From my understanding, you have a list of Names and when you select one - you want the relevant ID to be in the textbox. That is exactly what I did. The value attribute contains the ID of the username.

Share this post


Link to post
Share on other sites

Yes Truecolor,  but I want to display the results in two different (select and textbox) so it is little different that what you shows. However, I resolved this issue by using scripting and assigning ID as value for select option. Thanks for your helps and postings.

Share this post


Link to post
Share on other sites

×
×
  • 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.