cleary1981 Posted July 1, 2008 Share Posted July 1, 2008 if i try and run this php script directly in the browser i get the following error Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\wamp\www\lookupdescription.php on line 9 <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $select = 'SELECT mod_desc'; $from = ' FROM module'; $where = ' WHERE type = \'' . $model' . '\''; $queryResult = mysql_query($select . $from . $where) or die("Error!".mysql_error()); echo $queryResult; mysql_close($conn); ?> line 9 is the line: $where = ' WHERE type = \'' . $model' . '\''; Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/ Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 Ok the way you did it wayyyy crazy. Your problem is not having a '.' after $model. But the problem is bigger than that. You need cleaner code. Not only so you can do things easier, but you can find errors better and others can see your code and help you out. Here's a better way to do what you're doing: $where = " WHERE type = '$model' "; Though I will say that the way you do it by breaking it up into 3 variables is very unnecessary and if anything takes up more resources by using 3 variables when you really don't even need one. Just put the entire string into the query by itself. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578948 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 Your problem is this line: $where = ' WHERE type = \'' . $model' . '''; You'd make your life much easier if you were to enclose your query string in double quotes; you then wont have to worry about escaping the single quotes around values in the query: $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); I agree with danny with regard to the fact that using 3 variables is pointless. Apart from anything else, it makes it harder to follow. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578953 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 I didn't quite understand why I was doing that. I just tried adapting code from a book to suit mine. I made the change you suggested and got the following error. Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\lookupdescription.php on line 9 heres my new code <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $select = 'SELECT mod_desc'; $from = ' FROM module'; $where = ' WHERE type = '$model'; $queryResult = mysql_query($select . $from . $where) or die("Error!".mysql_error()); echo $queryResult; mysql_close($conn); ?> Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578955 Share on other sites More sharing options...
Wolphie Posted July 1, 2008 Share Posted July 1, 2008 Your problem is this line: $where = ' WHERE type = \'' . $model' . '''; You'd make your life much easier if you were to enclose your query string in double quotes; you then wont have to worry about escaping the single quotes around values in the query: $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); I agree with danny with regard to the fact that using 3 variables is pointless. Apart from anything else, it makes it harder to follow. I also like to enclose database references in back quotes. e.g. $sql = "SELECT `mod_desc` FROM `module` WHERE `type` = '$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); I find it makes distinguishing between SQL code and PHP easier. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578960 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 I didn't quite understand why I was doing that. I just tried adapting code from a book to suit mine. I made the change you suggested and got the following error. Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\lookupdescription.php on line 9 heres my new code <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $select = 'SELECT mod_desc'; $from = ' FROM module'; $where = ' WHERE type = '$model'; $queryResult = mysql_query($select . $from . $where) or die("Error!".mysql_error()); echo $queryResult; mysql_close($conn); ?> *slightly upset* you didn't do the code exactly as I typed it... check it again Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578961 Share on other sites More sharing options...
mmarif4u Posted July 1, 2008 Share Posted July 1, 2008 you can try GingerRobot code OR edit this to this. $where ="WHERE type = '$model'"; Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578962 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Apologies. I just noticed that after I posted. Hers my code now. <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); echo $result; mysql_close($conn); ?> the error now is as follows Resource id #4 Warning: mysql_close(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\lookupdescription.php on line 14 Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578964 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 you can try GingerRobot code OR edit this to this. $where ="WHERE type = '$model'"; nothing wrong with my code. clearly1981 clearly didn't type the code, clearly. ah, i crack myself up Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578965 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 you don't need mysql_close. Where are you getting the $conn variable from?? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578967 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 cleary1981, i would assume you didn't name your connection $conn. Either way, mysql_close is pretty unnecessary -- php will close the link at the end of the script for you. danny, i do agree that it makes things clear. It's something i used to do, but am beginning to drop. I usually find that correct capitalisation is sufficient for me to follow the query. That said, if things start getting more complex, i do tend to add the backticks back in. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578969 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 cleary1981, i would assume you didn't name your connection $conn. Either way, mysql_close is pretty unnecessary -- php will close the link at the end of the script for you. danny, i do agree that it makes things clear. It's something i used to do, but am beginning to drop. I usually find that correct capitalisation is sufficient for me to follow the query. That said, if things start getting more complex, i do tend to add the backticks back in. what are backticks? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578973 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Right i sorted that. still getting Resource id #4 What does this mean? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578977 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 you're trying to print the resource that was gathered by doing the query. A query makes a resource(or we like to call it a $result) A $result needs to be put into $row using mysql_fetch_array(). then the variables from the database can be used. But before mysql_fetch_array(), your query is useless. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578982 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Write thanks guys. heres my code now. <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); $row = mysql_fetch_assoc($result); echo $row; ?> if I run it directly in browser i get no errors. But should the result not be displayed. It doesn't show anything Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578992 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 Backticks are what you called back quotes. Never heard them called back quotes -- not sure if its a language thing? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-578994 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 I call them single quotes... Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579001 Share on other sites More sharing options...
GingerRobot Posted July 1, 2008 Share Posted July 1, 2008 Err, well then what the hell do you call a regular single quote? : ' Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579003 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Hi remeber me? How do I get my result to display? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579009 Share on other sites More sharing options...
br0ken Posted July 1, 2008 Share Posted July 1, 2008 Write thanks guys. heres my code now. <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo $row['mod_desc']."<br />"; } ?> if I run it directly in browser i get no errors. But should the result not be displayed. It doesn't show anything Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579011 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 That doesn't work. In my result im only expecting one result from my database does this matter? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579014 Share on other sites More sharing options...
br0ken Posted July 1, 2008 Share Posted July 1, 2008 Be more specific please. Do you get any errors? Is anything displayed? Have you tried debugging this yourself? Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579018 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 there are no errors. I just get a blank page when i run it directly in the browser. I tried different things on it. All it needs to do is show the description. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579019 Share on other sites More sharing options...
cleary1981 Posted July 1, 2008 Author Share Posted July 1, 2008 Surely $row should hold my description so this should work but it doesn't <?php require "config.php"; $model = preg_replace("/[\. \(\)\-]/", "", $_REQUEST['model']); $sql = "SELECT mod_desc FROM module WHERE type='$model'"; $result = mysql_query($sql) or trigger_error(mysql_error()); $row = mysql_fetch_assoc($result); echo "description:".$row; ?> Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579022 Share on other sites More sharing options...
dannyb785 Posted July 1, 2008 Share Posted July 1, 2008 Err, well then what the hell do you call a regular single quote? : ' lol I thought you meant single quotes when you said backtick. Link to comment https://forums.phpfreaks.com/topic/112738-solved-unexpected-t_constant_encapsed_string/#findComment-579347 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.