/    Sign up×
Community /Pin to ProfileBookmark

SQL syntax error, wrong version?

Hi, I have written this script that partially displays some information, that is fine, except when you click on the [b]More…[/b] link it should displa all information for that record.

Except it displays this error

[code]SQL ERROR: SELECT FROM `branches` WHERE id = ‘2’ – 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 ‘FROM `branches` WHERE id = ‘2” at line 1[/code]

I am running;
Apache version 1.3.33 (Unix)
PHP version 4.3.10
MySQL version 4.0.25-standard-log

with this code

[code=php]<? session_start(); ?>
<?php include(‘header.php’); ?>

<p class=”header”>
Branches
</p>
<div align=”left”>
<table border=”0″ width=”97%” cellpadding=”0″ cellspacing=”0″ class=”body”>
<?php

if($_GET[“view”]) { //Find if showing all details
/#this part should display all the information when the link is clicked, but displays the error above #/
?>
<tr>
<td class=”smallborder”><b>Area</b></td>
<td class=”smallborder”><b>Contact</b></td>
<td class=”smallborder”><b>Phone</b></td>
<td class=”smallborder”><b>Address</b></td>
<td class=”smallborder”>&nbsp;</td>
</tr>
<?php

$view_sql = “SELECT FROM `branches` WHERE id = ‘{$_GET[‘view’]}'”;
$result = @mysql_query($view_sql);

if(!$result)
{
echo(“SQL ERROR: $view_sql – ” . mysql_error()); //shows this error
}
else
{
echo(“<tr><td class=’smallborder’>{$result2[‘area’]}</td>
<td class=’smallborder’>{$result2[‘phone’]}</td>
<td class=’smallborder’>{$result2[‘phone’]}</td>
<td class=’smallborder’>{$result2[‘phone’]}</td>
<td class=’smallborder’><a href=’branches.php’ />View all</a></td>
</tr>”);
}

}else {
//this is what loads first, partial data, then should expand with the top section of code to display all information from that record.
?>
<tr>
<td class=”smallborder”><b>Area</b></td>
<td class=”smallborder”><b>Phone</b></td>
<td class=”smallborder”>&nbsp;</td>
</tr>
<?php
//SQL QUERY TO SELECT ALL OF THE USERS
$sql2 = “SELECT * FROM branches ORDER by area ASC”;
$sql2 = mysql_query($sql2);

//WHILE USERS ARE IN THE DATABASE DO THE FOLLOWING
while($result2 = mysql_fetch_assoc($sql2)) {

//PRINT EACH USERS NAME & EMAIL
echo(“<tr><td class=’smallborder’>{$result2[‘area’]}</td>
<td class=’smallborder’>{$result2[‘phone’]}</td>
<td class=’smallborder’><a href=’branches.php?view={$result2[‘id’]}’ />More…</a></td>
</tr>”);
}

}
?>
</table>
</div />
<p>Please feel free to contact any of the above or</p>

<p>National Headquarters – Address – PO Box 88-028 Clendon. Manurewa. Manukau City. or</p>
<p>National Secretary (direct) 195 Weymouth Road. Manurewa. Phone 09-268 4536.</p>

<?php include(‘footer.php’); ?>[/code]

Thanks

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 15.2005 — You didn't specify any column(s) to select. Did you mean "SELECT * FROM...."?
Copy linkTweet thisAlerts:
@SheldonauthorSep 15.2005 — Yes, that helped thanks Charles, but now with the same code including the * it is loading with no errors but not echo'ing the result from the mySQL query?

Any more ideas?
Copy linkTweet thisAlerts:
@SheldonauthorSep 15.2005 — any one?population of the feilds?
Copy linkTweet thisAlerts:
@NogDogSep 15.2005 — You didn't do any mysql_fetch_* after the query to get the contents of the row (assuming a row was returned by the query).
Copy linkTweet thisAlerts:
@SheldonauthorSep 15.2005 — does [code=php]mysql_query()[/code] not od the same as [code=php]mysql_fetch_*...[/code]????

Thanks
Copy linkTweet thisAlerts:
@NogDogSep 15.2005 — Nope. When mysql_query() is successful, it returns a resource ID that points to the query's result set. You then need to use mysql_fetch_row(), mysql_fetch_assoc(), or mysql_fetch_array() to read a row from that result set (even if it only returned 1 row).
Copy linkTweet thisAlerts:
@SheldonauthorSep 15.2005 — i changed this[code=php]$view_sql = "SELECT FROM branches WHERE id = '{$_GET['view']}'";
$result = @mysql_query($view_sql); [/code]

to

[code=php]$view_sql = "SELECT * FROM branches WHERE id = '{$_GET['view']}'";

while($result = mysql_fetch_assoc($view_sql)) { //this is line 25 with the error???

if(!$result)
{
echo("SQL ERROR: $view_sql - " . mysql_error()); //shows this error
}
else
{
echo("{$result2['phone']}"); //echo all results edit for content
}
}[/code]


and get this error;

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sheldon/public_html/mva/branches.php on line 25




Thanks
Copy linkTweet thisAlerts:
@SheldonauthorSep 15.2005 — ah sorted thank all for your help!!!!!!
Copy linkTweet thisAlerts:
@SpectreReturnsSep 16.2005 — We really need to make people search before posting....
×

Success!

Help @Sheldon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...