Jump to content

[SOLVED] unexpected T_CONSTANT_ENCAPSED_STRING


cleary1981

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.