Hi All,

I am a newcomer to PHP and still learning. Any help with this problem would be appreciated.
Please see the code below. Whenever I run this I get an error message:

"Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in
C:\wamp\www\selected.php on line 25"

Can someone tell me what Iam doing wrong?

<html>
<Head>
<Title>Drop Down Choice Example</title>

<?php

//connect to database, checking, etc
include_once "connect.php";

// process form when posted
if(isset($_POST['value'])) {
    if($_POST['value'] == 'Kulp') {
        // query to get all Kulp records  
        $query = "SELECT * FROM names WHERE LastName='Kulp'";  
    }  
    elseif($_POST['value'] == 'Smith') {  
        // query to get all Smith records  
        $query = "SELECT * FROM names WHERE LastName='Smith'";  
    } else {  
        // query to get all records  
        $query = "SELECT * FROM names";  
    }  
    $sql = mysql_query($query);  

    while ($row = mysql_fetch_array($query)){ 
        $Id = $row["Id"]; 
        $FirstName = $row["FirstName"]; 
        $LastName = $row["LastName"]; 

        // Echo your rows here... 
        echo 'The user ID is:' . $row['id'];
    }
 }
?>
</head>

<body>
<P>Please select from below<p><BR>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
        <select name="value">
        <option value="All">All</option>
        <option value="Kulp">Kulp</option>
        <option value="Smith">Smith</option>
    </select>
    <br />
    <input type='submit' value = 'Filter'>
</form>

Recommended Answers

All 5 Replies

Hey, please try the following:

<html>
<Head>
<Title>Drop Down Choice Example</title>

<?php

//connect to database, checking, etc
include_once "connect.php";

// process form when posted
if(isset($_POST['value'])) {
    if($_POST['value'] == 'Kulp') {
        // query to get all Kulp records  
        $query = "SELECT * FROM names WHERE LastName='Kulp'";  
    }  
    elseif($_POST['value'] == 'Smith') {  
        // query to get all Smith records  
        $query = "SELECT * FROM names WHERE LastName='Smith'";  
    } else {  
        // query to get all records  
        $query = "SELECT * FROM names";  
    }  
    $sql = mysql_query($query);  

    while ($row = mysql_fetch_array($sql)){ 
        $Id = $row["Id"]; 
        $FirstName = $row["FirstName"]; 
        $LastName = $row["LastName"]; 

        // Echo your rows here... 
        echo 'The user ID is:' . $row['id'];
    }
 }
?>
</head>

<body>
<P>Please select from below<p><BR>

<form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post' name='form_filter' >
        <select name="value">
        <option value="All">All</option>
        <option value="Kulp">Kulp</option>
        <option value="Smith">Smith</option>
    </select>
    <br />
    <input type='submit' value = 'Filter'>
</form>

Your problem is that $query just stores what the query should perform, and, $sql is what executes the query, therefore, you need to pass in this value rather than the string.

Hope this solves your problem

phorce,

Thanks for the reply mate - that makes sense to me. I knew I was overlooking something.
I changed the code to reflect

while ($row = mysql_fetch_array($sql)){ 

However, I am still reciving the error message.
Any additional thoughts?

I don't get an error message doing this:

<?php

//connect to database, checking, etc
include_once "connect.php";

// process form when posted
if(isset($_POST['value'])) {


    if($_POST['value'] == 'Kulp') {
        // query to get all Kulp records  
        $query = "SELECT * FROM names WHERE LastName='Kulp'"; 
        $sql = mysql_query($query);  
    }  
    elseif($_POST['value'] == 'Smith') {  
        // query to get all Smith records  
        $query = "SELECT * FROM names WHERE LastName='Smith'";  
        $sql = mysql_query($query); 
    } else {  
        // query to get all records  
        $query = "SELECT * FROM names"; 
        $sql = mysql_query($query);  
    }  
    while ($row = mysql_fetch_array($sql)){ 
        $Id = $row["Id"]; 
        $FirstName = $row["FirstName"]; 
        $LastName = $row["LastName"]; 

        // Echo your rows here... 
        echo 'The user ID is:' . $row['id'];
    }
 }
?>

I don't have your server, so I can't really test it.. But give it a try..

commented: Quick reply and very helpful +0

Phorce,

Thank you! I am an idiot sometimes - had the wrong sql table name in my script.
Your solution works perfectly. Thanks so much for the help. Time for my to take a break for the night obviously my brain is fried. :-)
Thanks again. Much appreciated.

No problem mate :) Good luck with it! Please mark this thread as solved, if you haven't done so already! Get some sleep haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.