Page 1 of 1

mysql_fetch_assoc

Posted: Wed Jan 28, 2009 11:42 am
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?

Re: mysql_fetch_assoc

Posted: Wed Jan 28, 2009 11:46 am
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

Re: mysql_fetch_assoc

Posted: Wed Jan 28, 2009 11:54 am
by danjoe15
Thank you for your help. It is working now.