why i am i getting this synyax error when trying to display data from the database

the code for saving the data is

// save content to db
if(isset($_POST["inpContent"])) 
	{
	
	$sContent=stripslashes($_POST['inpContent']); // remove slashes (/)	
	$sContent=ereg_replace("'","''",$sContent); // fix SQL
		
	$sql="UPDATE cms_core SET editorial='$sContent' WHERE id=$id";
	$query = mysql_query($sql);
	}

and the code to get the data from the data base is

$SQL = "SELECT cms_core FROM '$sContent' WHERE id=1";
 $res = mysql_query( $SQL, $db )
 or die( "<br><br><b>MySQL
 Error:</b> " . mysql_errno() . " // " .
 mysql_error() . "<br><br>" );

 if( $res !== "" ) {
 echo "Nothing in recordset.";
 } else {
 echo "Something in recordset.";
 }

thanks if anyone can help

Hi Kevin

What else does the error message say?

Normally something along the lines of "near..."

here is the full error message

MySQL Error: 1064 // You have an error in your SQL syntax near '' at line 1

OK. A little hazy still ... We don't know which is line 1.

Print out your queries with echo $SQL and see if that helps.

Syntax error while updating the record or while fetching ?

$SQL = "SELECT cms_core FROM '$sContent' WHERE id=1";

$sql="UPDATE cms_core SET editorial='$sContent' WHERE id=$id";

cms_core ? is it a table or a column name ?

Maybe my reply was a little hazy too! Sorry! ;-)

By printing out the queries, you will be able to see what the mySQL server is being sent and hopefully that will steer you in the right direction!

i am no getting a different problem with error codes. cmsservdb is the database cms_core is the table name and editorial is the coloum name. my database sewtup looks like this

<?php
$db = mysql_connect("xxxxxxxx","xxxxx","xxxx");
mysql_select_db("cmsservdb", $db);

// Create table in my_db database
mysql_select_db("cmsserdb");
$sql = "CREATE TABLE cme_core 
(
editroial longblob
)";
mysql_query($sql);

?>

and the error message i am getting now on the page that is recalling the data is

MySQL Error: 1146 // Table 'cmsservdb.editroial' doesn't exist

the code used to access this data is

$SQL = "SELECT * FROM editorial WHERE id= 1 ";
 $res = mysql_query( $SQL, $db )
 or die( "<br><br><b>MySQL
 Error:</b> " . mysql_errno() . " // " .
 mysql_error() . "<br><br>" );

 if( $res !== "" ) {
 echo "Nothing in recordset.";
 } else {
 echo "Something in recordset.";
 }

i have just edited the code used to recall the data so it looks like this and the error has gone it now tells me there is nothing in the record set.

the code looks like this

$SQL = "SELECT editorial FROM cms_core WHERE id= 1 ";
 $res = mysql_query( $SQL, $db )
 or die( "<br><br><b>MySQL
 Error:</b> " . mysql_errno() . " // " .
 mysql_error() . "<br><br>" );

 if( $res !== "" ) {
 echo "Nothing in recordset.";
 } else {
 echo "Something in recordset.";
 }

$res will have the resource_id of the query you just executed. You can check if there are any records or not this way.

$res = mysql_query($SQL,$db);
if(mysql_num_rows($res) > 0){
echo "There is something!";
} else {
echo "Nothing !";
}

i echoed the $sql on both the pages and on the page where the data is being recall it gave the


Nothing in recordset.CREATE TABLE cms_core ( editroial longblob id int )

and when i echoed the $sql on the page where it is saving it gave me


UPDATE cms_core SET editorial='

then all the text that is being saved to the database with

WHERE id=$id

after the text.

it looks like the data is being saved to the database but why can i not recall the data on the other page.

Also -

if( $res !== "" ) { 
 echo "Nothing in recordset."; 
 } else {
 echo "Something in recordset.";
 }

You are saying If $res is not == "" then echo "Nothing in Recordset" else ...

So if $res has anything in it, you will get the "Nothing" message.

i tried the code you just put up and it gives me the same reply as before telling me there is nothing the database.

how can this be when i test the save to data base and it says it is saving the data that is needed. the as the data is being saved to the database it is stored in a variable called $sContent should i use the variable name when recalling the data in stead of the column name?

i will try this i see what the result is.

i have changed the part of the code where it is asked to display the answer if there is something in the record set and changed the editorial to

$sContent

and it now says there is something and the message it leaves looks like this


There is something!CREATE TABLE cms_core ( editroial longblob id int )

why is the create table part showing up is this meant to?

i just took the create table from the db set up page and i am now just left with the message


There is something!

Huh! Can you post your complete code ?

Then obviously, the table has records. What do you want exactly ?

i need the code to display the item being called for from the database if there is something in the database and i am using the select statement should it not display what is being call for. i will upload the code for the saving and accessing the database. will you need any more?

And you don't have that syntax error anymore. Right ? Now, if that solves your problem, well and good. If it doesn't, you need to be specific about your problem.

commented: he is very helpful all the time +1

the code for saving it to the database is

include ('../includes/db_inc.php');

// save content to db
if(isset($_POST["inpContent"])) 
	{
	
	$sContent=stripslashes($_POST['inpContent']); // remove slashes (/)	
	$sContent=ereg_replace("'","''",$sContent); // fix SQL
		
	$sql="UPDATE cms_core SET editorial='$sContent' WHERE id=$id";
	$query = mysql_query($sql);
	}

and the code i have used to recall the data from the database is

$SQL = "SELECT '$sContnet' FROM cms_core WHERE id= '1' ";
      $res = mysql_query($SQL,$db);
      if(mysql_num_rows($res) > 0){
      echo "There is something!";
      } else {
      echo "Nothing !";
      }
 echo $sql;

if you need anymore let me know.

Update query is correct. But select query is wrong.

SELECT * FROM cms_core WHERE id= '1'

You can use this.

what i want it to do is to display the save data on the page it is being recalled on as it is text for the homepage. it is being saved from an editor into the database and then should display on the pages the text is needed.

the problem is that it is saying that it is saving the data to the database but it will not display the data from the database. it will probably be something which is stupid stoping it from working as it usually is when it comes to code.

fresh eyes looking at it always helps.

You should actually try and avoid using SELECT * and rather list the columns you want to be returned SELECT [I]col1, col2, col3[/I] It just helps speeding things up a bit and you don't have a ton of data flying around the web that you don't need.

commented: was very helpful with me +1

Where are you trying to display the records ?

You should actually try and avoid using SELECT * and rather list the columns you want to be returned SELECT [I]col1, col2, col3[/I] It just helps speeding things up a bit and you don't have a ton of data flying around the web that you don't need.

Very true. But if you don't know what columns you wanna display and what not, you can use *.

When you use select query, you need to 'fetch' the rows and display it. You do it this way.

<?php
//connect
//select db
$query = "select * from table where id='$id'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo $row['firstname'];
echo $row['lastname'];
//etc..
}

:) Cheers..

the record is meant to be displayed within a table on the homepage. i have changed the code now so that it no longer displayed the message something is in the database and just left the new select line in and still nothing is being displayed.

the code for the full page looks looke this

<body onload="MM_preloadImages('global/home_over.gif','global/refurbs_over.gif','global/setups_over.gif','global/contact_over.gif')">
<?php
include ('includes/db_inc.php');
?>
<table width="740" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td>&nbsp;</td>
        <td><img src="global/navTop.gif" width="370" height="60" /></td>
    </tr>
    <tr>
        <td><img src="global/logo.gif" alt="CMS Services" width="370" height="65" /></td>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td><a href="index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('home','','global/home_over.gif',1)"><img src="global/home_off.gif" alt="Home" name="home" width="370" height="17" border="0" id="home" /></a></td>
                </tr>
                <tr>
                    <td><a href="refurbishment/index.htm" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('refurbs','','global/refurbs_over.gif',1)"><img src="global/refurbs_off.gif" alt="Refurbishment Projects" name="refurbs" width="370" height="15" border="0" id="refurbs" /></a></td>
                </tr>
                <tr>
                    <td><a href="setups/index.htm" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('setups','','global/setups_over.gif',1)"><img src="global/setups_off.gif" alt="Set Ups &amp; Dismantles" name="setups" width="370" height="16" border="0" id="setups" /></a></td>
                </tr>
                <tr>
                    <td><a href="contact/index.htm" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('contact','','global/contact_over.gif',1)"><img src="global/contact_off.gif" alt="Contact CMS Services" name="contact" width="370" height="17" border="0" id="contact" /></a></td>
                </tr>
            </table></td>
    </tr>
    <tr>
        <td><img src="global/navSpacer.gif" width="10" height="20" /></td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><img src="images/img_home.jpg" width="370" height="160" /></td>
        <td><img src="images/header_home.gif" width="370" height="160" /></td>
    </tr>
</table>
<table width="740" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td width="185" bgcolor="#F1DECB"><table width="185" border="0" cellspacing="0" cellpadding="10">
            <tr>
                <td bgcolor="#002C54" class="textWhite">&nbsp;</td>
            </tr>
        </table></td>
        <td width="20">&nbsp;</td>
        <td width="535" background="global/pageBg.gif"><table width="95%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
					<?php
						
						$sql= "SELECT * FROM cms_core WHERE id= '1'";
						$query = mysql_query($sql);
						$result = mysql_fetch_array($query);
						
					?>								
				</td>
            </tr>
        </table></td>
    </tr>
</table>
<table width="740" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td><img src="global/footer.gif" width="740" height="70" /></td>
    </tr>
    <tr>
        <td><div align="center">
            <p class="footer">&copy; CMS Services. All rights reserved. </p>
        </div></td>
    </tr>
</table>
</body>
</html>

In your above script, you are getting the records to $result. But you aren't printing anywhere. Try printing out $result somewhere.

just as i posted that last thread i realised that i had not echoed the result anywhere so it could never be displayed without this. i told you it would be something silly and i dont think it can get worse than that.

:) Yep! lol.. Sometimes it happens..

solved thanks for all your help i felt like punching myself when i seen i did not echo the result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.