There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop malware monitor motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound spyware trojan usb virus vista vista 32-bit windows windows xp windowsxp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
php/mysql error


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
25-Jun-2004, 04:38 PM #1
php/mysql error
Hello,
I created a database where I can put news for my site. It has 4 fields, the nid which is auto increment, the headline, the body, and teams (baseball teams) to which it pertains. This way on the team pages I can have the latest news (or highest news id, nid). I have everything good so far except when I try to pick the latest news for only that team. Here is the code I am using:
<?
$hostname = "localhost";
$username = "******";
$password = "******";
$dbName = "*******";

mysql_connect($hostname, $username, $password) or die("DB connection unavailable");
mysql_select_db( "$dbName") or die( "Unable to select database");

$sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ";
$sql .= "ORDER BY nid ASC ";

$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$variable1=$row["nid"];
$variable2=$row["stitle"];
$variable4=$row["body"];
}
?>


When I try this I get this error message: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY nid ASC LIMIT 0, 30' at line 1. If I run the sql statement just how it is in phpmyadmin it works good. If I take that line out I get query is empy. Help is greatly appreciated. Oh and just so you know...the variables are all used later embedded in html.
brendandonhu's Avatar
Distinguished Member with 15,988 posts.
 
Join Date: Jul 2002
Location: Ann Arbor, MI
Experience: Advanced
26-Jun-2004, 10:14 AM #2
I didn't test this, but I think you need to change those two lines to
Code:
$sql = "SELECT * FROM 'news' WHERE 'teams' LIKE '%Atl%' ";
$sql .= "ORDER BY 'nid' ASC ";
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
26-Jun-2004, 11:52 AM #3
When I switch to that I get: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ''news' WHERE 'teams' LIKE '%Atl%' ORDER BY 'nid' ASC' at line 1
deuce868's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
26-Jun-2004, 04:54 PM #4
take out the quotes around the table name and field name
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
26-Jun-2004, 05:06 PM #5
Then it will go back to what I originally had: $sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ";
deuce868's Avatar
Senior Member with 638 posts.
 
Join Date: Nov 2000
Location: MI
26-Jun-2004, 05:42 PM #6
in the line after you set the $sql variable output the final SQL string with echo( $sql ); and post it here.
punjabian263's Avatar
Senior Member with 318 posts.
 
Join Date: Mar 2003
Location: PAKISTAN
Experience: Advanced
27-Jun-2004, 12:08 AM #7
run you code step by step and printing error message along.
Code:
$hostname = "localhost"; 
$username = "******"; 
$password = "******"; 
$dbName = "*******"; 
$id=mysql_connect($hostname, $username, $password)
if($id)echo"successfully connectd";else die("DB connection unavailable"); 
$select=mysql_select_db( "$dbName")
if($select)echo "database successfully selected";else die( "Unable to select database"); 

$sql = "SELECT * FROM news WHERE teams LIKE '%Atl%' ";
$sql .= "ORDER BY nid ASC ";
echo $sql;  //check the right syntax of the query, and check this query also on mysql prompt;
$result = mysql_query($sql) or die(mysql_error());
if($result) echo "records successfully retrieved";
Sportznow's Avatar
Junior Member with 9 posts.
 
Join Date: May 2004
Experience: Intermediate
27-Jun-2004, 02:27 PM #8
Hmm, very, very weird. All of a sudden it is working on one of the pages but if I copy paste the same code, it isn't working on another...
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:57 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.