server query problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
billabon0202
Forum Newbie
Posts: 8
Joined: Wed Feb 20, 2008 11:31 am

server query problem

Post by billabon0202 »

I am new to using php and mysql. For some reason or another query keeps failing. Could somone help me out with my logic. The query is as follows:

Separate connect file

<?php # mysql_connect.php

DEFINE('DB_USER', '*******');

DEFINE('DB_PASSWORD', '********');

DEFINE('DB_HOST', '**********');

DEFINE('DB_NAME', '********');

$dbc=@mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)

OR die('Could not connect to MYSQL: ' . mysqli_connect_error());

?>

Code within the webpage

<?php
require_once('mysql_connect.php');

$qry= "SELECT DISTINCT (restaurant_info.food_type)
FROM restaurant_info
INNER JOIN restaurants
ON restaurants.rest_id= restaurant_info.rest_id
WHERE restaurants.delivery = 'yes'
ORDER BY food_type ASC";

$result = mysql_query($qry);

if (!$result) {die("Query Failed.");
}else{
while($row = mysql_fetch_array($result)) {
echo '<p>' . $row['food_type'] . '</p>';
}
}
?>

I don't get an error however it says "Query Failed" every time so i am assuming $results isnt pulling information. However when i run the (
SELECT DISTINCT (restaurant_info.food_type)
FROM restaurant_info
INNER JOIN restaurants
ON restaurants.rest_id= restaurant_info.rest_id
WHERE restaurants.delivery = 'yes'
ORDER BY food_type ASC;
)
query in mysql it pulls the info I need.
Thanks for your help
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: server query problem

Post by califdon »

I don't get an error however it says "Query Failed" every time so i am assuming $results isnt pulling information.
That is coming, of course, from your line:

Code: Select all

if (!$result) {die("Query Failed.");
}else{
while($row = mysql_fetch_array($result)) {
echo '<p>' . $row['food_type'] . '</p>';
So to learn more about why it fails, change that to:

Code: Select all

if (!$result) {
    die("Query Failed.  " . mysql_error());
}else{
    while($row = mysql_fetch_array($result)) {
      echo '<p>' . $row['food_type'] . '</p>';
    }
}
In future postings of code, it will help us read your code if you will use the BBCode tags, as above.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: server query problem

Post by yacahuma »

Dont use mysql_* use adodbhttp://adodb.sourceforge.net/
Post Reply