MrLoefjo Posted June 21, 2011 Share Posted June 21, 2011 Heya. I have a very basic problem. I am following some tutorials to create a Blog, but im changing it around a bit, to fit it better to my needs. Atm i have a small database, one of the tables in it is blog, which has id, title, entry and date section. Problem lies in following code: <?php require("config.php"); if(isset($_GET['id']) == TRUE) { if(is_numeric($_GET['id']) == FALSE) { $error = 1; } if($error == 1) { header("Location: " . $config_basedir); } else { $validentry = $_GET['id']; } } else { $validentry = 0; } require("header.php"); if($validentry == 0) { $sql = "SELECT blog.* FROM blog ". "ORDER BY Date DESC ". "LIMIT 1;"; } else { echo "$validentry"; $sql = "SELECT blog.* FROM blog ". "WHERE blog.id = " . $validentry . "ORDER BY Date ASC ". "LIMIT 1;"; } $result = mysql_query($sql); $row = mysql_fetch_assoc($result); specifically, this line : "WHERE blog.id = " . $validentry . isn't working. When i add it, i get fetching error in last line, when i remove it works fine. I made sure, that validentry is ok, using the echo. Also inserting SQL command in phpmyadmin: " SELECT blog.* from blog WHERE blog.id = 1, or 2 works fine. Any help would be appreciated. MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/ Share on other sites More sharing options...
TeNDoLLA Posted June 21, 2011 Share Posted June 21, 2011 Try to echo the query in your code and see if it is what you expect it to be? Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232977 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232978 Share on other sites More sharing options...
MrLoefjo Posted June 21, 2011 Author Share Posted June 21, 2011 If i echo the $result it gives me : Resource id #7 Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232980 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 There's no need to do all of the string concatenation you're doing; it just leads to problems like the one you're having. If you echo the query, you'll see there is no space between the $validentry variable and ORDER BY. You should also incorporate some basic logic to check for and handle errors such as these. $sql = "SELECT blog.* FROM blog ". "WHERE blog.id = " . $validentry . "ORDER BY Date ASC ". "LIMIT 1;"; // Would be better written as: $sql = "SELECT blog.* FROM blog WHERE blog.id = $validentry ORDER BY Date ASC LIMIT 1"; Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232981 Share on other sites More sharing options...
MrLoefjo Posted June 21, 2011 Author Share Posted June 21, 2011 It's fixed, thanks alot. I thought you need concatenation if you use variables inside sql query:) Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232989 Share on other sites More sharing options...
Pikachu2000 Posted June 21, 2011 Share Posted June 21, 2011 Variables are interpolated within a double-quoted string, so concatenation isn't needed. Both of these are perfectly valid, but the one without the concatenation is much easier to read, and a lot less likely to contain typos. $query = "SELECT `$field` FROM `$table` WHERE `$field` = '$value' ORDER BY `{$array['index']}` DESC"; // versus // $query = "SELECT `" . $field . "` FROM `" . $table . "` WHERE `" . $field . "` = '" . $value . "' ORDER BY `" . $array['index'] . "` DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/240030-basic-problem-with-fetching-sql-data/#findComment-1232998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.