Josh Miller
Josh Miller

Reputation: 21

Print large select SQL statement via php

I hope this is something you can help me with. I have through much trial and error come up with this statement which lists everything neatly in my phpmyadmin view.

SELECT DISTINCT inzone_product_lang.name, inzone_product.price, (inzone_product.price * 1.25) as pris
FROM inzone_product_lang 
INNER JOIN inzone_product
ON inzone_product.id_product=inzone_product_lang.id_product
ORDER BY inzone_product_lang.name, pris

I'd like to make this into a php query and print it - but I am stuck here.

$result = mysql_query("SELECT DISTINCT inzone_product_lang.name, inzone_product.price, (inzone_product.price * 1.25) as pris
FROM inzone_product_lang 
INNER JOIN inzone_product
ON inzone_product.id_product=inzone_product_lang.id_product
ORDER BY inzone_product_lang.name, pris
LIMIT 0, 900
");

$data = mysql_fetch_object($result);
    echo $data->foo();

Upvotes: 0

Views: 328

Answers (3)

Amit Yaron
Amit Yaron

Reputation: 654

You can use a concatenation string with the new-line characters as follows:

$query = "SELECT DISTINCT inzone_product_lang.name, inzone_product.price, (inzone_product.price * 1.25) as pris\n" .
"FROM inzone_product_lang \n" .
"INNER JOIN inzone_product\n" .
"ON inzone_product.id_product=inzone_product_lang.id_product\n" .
"ORDER BY inzone_product_lang.name, pris\n";

mysql_query($query);

"\n" is a whitespace. Allways use it in double-quoted strings. If you want to print the query to a browser, there's a nice PHP function named 'nl2br'

Upvotes: 0

Suhel Meman
Suhel Meman

Reputation: 3852

echo $query="SELECT DISTINCT inzone_product_lang.name, inzone_product.price, inzone_product.price * 1.25) as pris FROM inzone_product_lang INNER JOIN inzone_product ON inzone_product.id_product=inzone_product_lang.id_product ORDER BY inzone_product_lang.name, pris LIMIT 0, 900";

$result = mysql_query($query);

while($data = mysql_fetch_object($result))
{
    print_r($data);
}

Upvotes: 0

Seer
Seer

Reputation: 5237

Loop through the data, like this, if you want it in a table, you can just echo that data in a table row. Something like this:

while ($data = mysql_fetch_object($result)) {
    echo $data->name;
    echo $data->price;
    ...
}

Also, you can see the structure of objects like this by using tools like print_r (which is especially helpful in logs) and if you have XDebug installed, var_dump is very nice to look at.

var_dump($data); // Prints to document when loaded
error_log(print_r($data, true)); // Prints to error log

Upvotes: 1

Related Questions