mysql_fetch_assoc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
danjoe15
Forum Newbie
Posts: 5
Joined: Tue Jan 06, 2009 8:19 am

mysql_fetch_assoc

Post by danjoe15 »

I messed something up here though I am not entirely sure what. Here is the code in question:

Code: Select all

 
<?php
include 'common.php';
 
dbConnect();
 
$query  = "SELECT * FROM customers WHERE 1=1 ";
 
if ($_POST['Customer_id'] != "")
    {
        $query .= " AND customer_id = '%" . mysql_real_escape_string($_POST['Customer_id']) . "%' ";
    }
if ($_POST['FirstName'] != "")
    {
        $query .= " AND first_name LIKE '%" . mysql_real_escape_string($_POST['FirstName']) . "%' ";
    }
if ($_POST['LastName'] != "")
    {
        $query .= " AND last_name LIKE '%" . mysql_real_escape_string($_POST['LastName']) . "%' ";
    }
if($_POST['State'] != "")
    {
        $query .= " AND states = '" . mysql_real_escape_string($_POST['State']) . "' ";
    }
if ($_POST['City'] != "")
    {
        $query .= " AND city LIKE '%" . mysql_real_escape_string($_POST['City']) . "%' ";
    }   
    
$customer = mysql_fetch_assoc($result[$query]);
 
When I do print_r($customer) nothing shows up. What did I do incorrectly to cause this to happen?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: mysql_fetch_assoc

Post by Mark Baker »

You're not executing $query against the database.... just building a string containing the SQL and trying to display a result without actually generating a result.

In fact, $result[$query] is plain weird, you're trying to return the value of an array called $result, with an index of your SQL query. It's totally meaningless. Take a look at the PHP documentation for mySql to see how it should be done
danjoe15
Forum Newbie
Posts: 5
Joined: Tue Jan 06, 2009 8:19 am

Re: mysql_fetch_assoc

Post by danjoe15 »

Thank you for your help. It is working now.
Post Reply