Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse error...

Status
Not open for further replies.

Mrtechno

Technical User
Nov 6, 2002
75
NZ
Hi Guys...

I need some help with this PHP script in Dreamweaver (I wasn't sure where to post it so I also posted it in the Dreamweaver section also :) ).

I have no experience what so ever with PHP so have no idea what the problem is. The script is obviously auto generated by Dreamweaver :) and nothing has been changed manually.

On using the Live Data option, I get an error saying "Parse error: parse error, expecting `'," or `')" in /Users/hnm/Sites/myresults_23ul2krtr1.php on line 2"

The script is as follows and is called myresults.php (also note that the text appearing in the hashes i.e. #gname# and #dname# etc. appears in yellow in the script in Dreamweaver and I have no idea what that signifies):

<?php require_once('Connections/newconn.php'); ?>
<?php
$gname_rsresults = "%";
if (isset(#gname#)){
$gname_rsresults = (get_magic_quotes_gpc()) ? #uname# : addslashes(#uname#);
}
$gname_rsresults = "%";
if (isset(#gname#)) {
$gname_rsresults = (get_magic_quotes_gpc()) ? #uname# : addslashes(#uname#);
}
$dname_rsresults = "%";
if (isset(#dname#)) {
$dname_rsresults = (get_magic_quotes_gpc()) ? #deptname# : addslashes(#deptname#);
}
mysql_select_db($database_newconn, $newconn);
$query_rsresults = sprintf("SELECT name, dptname FROM testtable WHERE name='%s' AND dptname='%s'", $gname_rsresults,$dname_rsresults);
$rsresults = mysql_query($query_rsresults, $newconn) or die(mysql_error());
$row_rsresults = mysql_fetch_assoc($rsresults);
$totalRows_rsresults = mysql_num_rows($rsresults);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="510" border="0">
<tr>
<th width="224" scope="col">Employee Name </th>
<th width="276" scope="col">Department name </th>
</tr>
<tr>
<td><div align="center"><?php echo $row_rsresults['name']; ?></div></td>
<td><div align="center"><?php echo $row_rsresults['dptname']; ?></div></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($rsresults);
?>

Any help would be appreciated.

Thanks.
 
I'd guess the #gname# bits are supposed to be for DreamWeaver's template system. They're certainly not valid PHP code.

In php, # is the comment character, so everything from the first # in #gname# to the end of the line will be ignored by the interpreter. You need to replace #gname#, #dname#, etc. with actual PHP variables (e.g. $gname). However, it's unclear to me exactly what the correct variables would be in this context.
 
Thanks a lot for your reply Ad.

Well, I did what you said and that did seem to have fixed the problem but now what happens is that the page does load up but only the column names are displayed and no data is being picked up from the database.

Any ideas why?
 
Try adding this line:
Code:
echo mysql_errno($newconn) . ": " . mysql_error($newconn) . "\n";
after your mysql_query, and see if it gives you a database error number.
 
Well, I managed to get rd of the SQL problem last night... The variable name, as AdaHackr had suggested, had been changed to $gname, $uname etc. I changed the variable names to $_GET['gname'] and it worked...

Now the problem is that if I don't enter anything in the search fields and click search, the page again comes out blank displayng only the Column names. I had read somewhere that the % character is taken as a default character by SQL and if a User doesn't enter anything in the search fields then on clicking search all the rows of the table will be displayed but I can't seem to get it to work. SQL doesn't seme to be accepting the % character.

Thanks.
 
you should not, imo, use the dreamweaver code snips function. some of them are of rather dubious quality.

try this for size. i have not checked it so it may be full of parse errors

Code:
<?php 
require_once('Connections/newconn.php');
function cleanMe($var){
	if (get_magic_quotes_gpc()){
		$var = stripslashes($var);
	}
	$result = @mysql_real_escape_string(trim($var));
	if (!$result){
		$result = mysql_escape_string(trim($var));
	}
	return $return;
}
//turn on error management for debugging
if (isset($_GET['debug'])){
	error_reporting(E_ALL);
	ini_set('display_errors', "on");
} else {
	ini_set('display_errors', "off");
}
//turn off magic quotes
set_magic_quotes_runtime(0);
mysql_select_db($database_newconn, $newconn) or die(mysql_error());

//pre set your variables
$vars = array("gname", "dname");

//test each incoming variable
$where = array();
foreach ($vars as $var){
	$tmp = (isset($_GET[$var])) ? "$var = '".cleanMe($_GET[$var])."'" : null;
	if (!empty($tmp)){
		$where[] = $tmp;
	}
}
//assemble the where clause
if (count($where) > 0){
	$where = " WHERE " . implode(' AND ', $where);
} else {
	$where = '';
}
//assemble and run the query
$sql  = "SELECT name, dptname FROM testtable $where";
$result = mysql_query($sql, $newconn) 
	or die("There was a sql error on line " .__LINE__.".  Error was <br/>".mysql_error()."<br/>Query was: $sql");
//iterate through the recordset
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<table width="510" border="0">
  <tr>
    <th width="224" scope="col">Employee Name </th>
    <th width="276" scope="col">Department name </th>
  </tr>
<?php
	while ($row = mysql_fetch_assoc($result)){?>
	<tr>
    	<td><div align="center"><?php echo $row['name']; ?></div></td>
    	<td><div align="center"><?php echo $row['dptname']; ?></div></td>
	</tr>
<?php } //close the while loop?>
</table>
</body>
</html>
 
Thx JP.... it's solved the problem but now no matter what you enter or don't enter at all, it displays all the values from the table...

Could you please double check your script again to see where the priblem could be as I have no idea about the php language...

Thanks again.
 
my mistake. I made a (reasonable) assumption which I should have realised was incorrect.

use this code in place of its current equivalent.
Code:
$vars = array("gname"=>"name", "dname"=>"dptname");

//test each incoming variable
$where = array();
foreach ($vars as $iVar=>$wVar){
    $tmp = (isset($_GET[$iVar])) ? "$wVar = '".cleanMe($_GET[$iVar])."'" : null;
    if (!empty($tmp)){
        $where[] = $tmp;
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top