TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Problem with code (http://www.talkphp.com/general/1390-problem-code.html)

/\/\ongoose 11-06-2007 01:52 AM

Problem with code
 
Hey guys,

I am working on a script and this portion of code throws back and error, and I can't seem to figure out why, any help would be appreciated!

Code:

$recordset = mysql_query('SELECT id, date, title, description FROM news WHERE id = '.$_GET['id'].' LIMIT 1;') or die(mysql_error());
$row = mysql_fetch_array($recordset);

Error:

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 'LIMIT 1' at line 1

/\/\ongoose 11-06-2007 01:52 AM

Oh and the code worked fine on another page, so I don't know what the problem is :-\

Wildhoney 11-06-2007 02:00 AM

Oh, I see the problem now. You appear to have placed a semi-colon at the end of your SQL statement. Remove it and try again. I don't understand why it works on another page though if it's an identical query to that. I'm fairly sure PHP would not like that semi-colon there.

/\/\ongoose 11-06-2007 02:04 AM

I changed the code to:


$recordset = mysql_query('SELECT id, date, title, description FROM news WHERE id = '.$_GET['id'].'LIMIT 1') or die(mysql_error());

It still throws back the same error?

Wildhoney 11-06-2007 02:26 AM

Code:

$recordset = mysql_query('SELECT id, date, title, description FROM news WHERE id = '.$_GET['id'].' LIMIT 1') or die(mysql_error());

Nor 11-06-2007 02:29 AM

Try adding back ticks.

Quote:

Oh, I see the problem now. You appear to have placed a semi-colon at the end of your SQL statement. Remove it and try again.
Doesn't really matter since most sql delimiters use a semi colon at the end.

PHP Code:

$recordset mysql_query("SELECT `id`, `date`, `title`, `description` FROM `news` WHERE `id` = " intval($_GET['id']) . " LIMIT 1;") or die(mysql_error()); 

If that don't work try adding single quotes around the id:

PHP Code:

$recordset mysql_query("SELECT `id`, `date`, `title`, `description` FROM `news` WHERE `id` = '" intval($_GET['id']) . "' LIMIT 1;") or die(mysql_error()); 


/\/\ongoose 11-06-2007 02:30 AM

That is exactly like the original code that works in the other page, but it still kicks back an error on this page. Here is the whole script :

<?php
$currentPage = basename($_SERVER['SCRIPT_NAME']);
require_once('dbconfig.php');
$recordset = mysql_query('SELECT id, date, title, description FROM news WHERE id = '.$_GET['id'].' LIMIT 1') or die(mysql_error());
$row = mysql_fetch_array($recordset);
?>

Maybe that will help

Nor 11-06-2007 02:31 AM

Check my post.

/\/\ongoose 11-06-2007 02:35 AM

Well, I tryed yours and it got rid of the error, but it did not echo the information, it is just blank. This is the whole page of code, with your code in it Nor.

<?php
$currentPage = basename($_SERVER['SCRIPT_NAME']);
require_once('dbconfig.php');
$recordset = mysql_query("SELECT `id`, `date`, `title`, `description` FROM `news` WHERE `id` = " . intval($_GET['id']) . " LIMIT 1;") or die(mysql_error());
$row = mysql_fetch_array($recordset);
?>
<span class="newscontainer">

<div class="newsinside">
<!--News section -->
<div class="newsep">
<span class="newsdate"><?php echo($row['date']);?></span><br />
<span class="newstitle"><?php echo($row['title']);?></span><br />
<span class="newsdes">

<?php echo($row['description']);?></span>

</div>
<!--End section-->

<!--News section -->
<div class="newsep">
<span class="newsdate"><?php echo($row['date']);?></span><br />
<span class="newstitle"><?php echo($row['title']);?></span><br />
<span class="newsdes">

<?php echo($row['description']);?></span>

</div>
<!--End section-->

</div>
</span>

Nor 11-06-2007 02:37 AM

try doing

PHP Code:

<?php
print_r
($row);
?>

and post the array that you get for us.

/\/\ongoose 11-06-2007 02:41 AM

where should i put that code? sry im really new to php

Nor 11-06-2007 02:43 AM

after:

$row = mysql_fetch_array($recordset);

PHP Code:

$row mysql_fetch_array($recordset);
print_r($row); 


/\/\ongoose 11-06-2007 02:45 AM

it didnt print anything

Nor 11-06-2007 02:48 AM

Then there is no value for that ID, check your database.

/\/\ongoose 11-06-2007 03:03 AM

ok i fixed all of that now one last problem or so i hope :(...i am getting a syntax error on this line

while ($row = mysql_fetch_array($recordset)){ echo('.$row['date']);

It looks right to me?

Andrew 11-06-2007 03:16 AM

What's the syntax error? I'm fairly sure it's because of "echo('.$row['date']);". Try changing that to:
PHP Code:

echo $row['date']; 


Nor 11-06-2007 03:20 AM

Quote:

"echo('.$row['date']);".
What are you talking about? Thats not even in his post that he posted. where you get that extra quotes..

/\/\ongoose 11-06-2007 03:38 AM

ok i fixed it all up how i wanted, although you people that are good at php probably would think its pretty butured up lol, thanks for the help guys

Nor 11-06-2007 04:12 AM

What was the problem just wondering?

/\/\ongoose 11-06-2007 04:16 AM

i just changed the whole script around it looks like this now :

<?php
require_once "dbconfig.php";
$recordset = mysql_query('SELECT id, date, title, description FROM news ORDER BY id DESC;');
while ($row = mysql_fetch_array($recordset)){ echo('<a href="newsedit.php?id='.$row['id'].'">'.$row['date'].'-'.$row['title'].'</a><br><a href="delete_news.php?id='.$row['id'].'">Delete</a><br><br>');}
?>


All times are GMT. The time now is 09:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0