Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Why will this query not print?

Here's the query:

$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");

I want to see what the page is seeing, so I do: echo $data and nothing shows up. What am I missing?
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

This is someone else's code and I'm trying to debug a problem that surfaced a few days ago.

Your question prompted my going back and looking at things a bit closer. Here's the function:

function SqlQuery($database,$sql){

            if (strpos($_SERVER['SERVER_NAME'],"snowdogweb")){
            $user="";
            $pass="";
            $datasource="";
            $server="";
            }else{
            $user = "";
            $pass="";
            $datasource = "";
            $server="";
            }

            $dbase_user = mysql_connect($server,$user,$pass);
            mysql_select_db($datasource, $dbase_user);
            
            $query_results = mysql_query($sql, $dbase_user);
                                    
            
            return $query_results;

}

The problem is, I'm getting an error in that while there's data to retrieve, the array is empty. So I'm trying to see how the query is being read, but I don't know how to get it to print given the way it's been coded.

Thoughts?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can simply put a beautyful

echo $sql;

as first line of your function in order to see if the query is correctly passed. I presume that all values left blank are so for privacy reasons: I mean that in this function

$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");

if you don't pass a database name it won't work, but I think you left it blank only here at EE

Cheers
Here's what I did:

$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");
echo $sql;

on the page I get an error that says Undefined variable: sql in /mnt/vhosts/heavydutylighting.com/httpdocs/index.php on line 103.

What do you think?
The script appears to assign a value to a variable named $data, but not to a variable named $sql.  I think you might want to get some assistance from a professional programmer.  It will save you a lot of time.  You can learn about how to use var_dump() to print out the values of variables after they are assigned (variable name appears to the left of the single equal sign).  For example, in the most recent example we saw this:
$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");
echo $sql;

Open in new window

and a more useful code segment might be this:
$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");
var_dump($data);

Open in new window

To see if the variable $sql is passed correctly you have to do this:

your main script
$data=SqlQuery("","
SELECT *
FROM cart_products
WHERE featured='1'
");

Open in new window


within the function
function SqlQuery($database,$sql){

            echo $sql;            

            if (strpos($_SERVER['SERVER_NAME'],"snowdogweb")){
            $user="";
            $pass="";
            $datasource="";
            $server="";
            }else{
            $user = "";
            $pass="";
            $datasource = "";
            $server="";
            }

            $dbase_user = mysql_connect($server,$user,$pass);
            mysql_select_db($datasource, $dbase_user);
            
            $query_results = mysql_query($sql, $dbase_user);
                                    
            
            return $query_results;

}

Open in new window


This way you'll see if the function SqlQuery reads correctly the $sql variable. I repeat: are all varibles in the function lewft empty for privacy resons ($user, $pass, $datasource, $server? And why the function expects a $database variable if it doesn't use it to fill $datasource variable? Or $database variiable is superflous or it is superflous $datasource variable.
First of have you create database
 and
 created table in it?
 Have you connected database correctly?
If so has there is featured column in your cart_product table?
What you are exactly echoing? $sql or $data?
Pls check n Let us know!
Thank you!