0

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>
2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by phorce
0

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

0

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?

1

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

Votes + Comments
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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.