PHP语法与ColdFusion相对

时间:2011-11-26 22:11:29

标签: php sql

使用ColdFusion,您可以在相同的表上使用相同的搜索条件运行任意数量的查询,并为查询提供如下名称:

<cfquery name="getNext" datasource="mssqlcf_PDartist1">

但是使用PHP,我已经找到了如何命名查询。所以,当我需要多次查询同一个表时,我得到Parse错误:语法错误,意外的T_VARIABLE。

<?php
$dbname = 'pdartist2';
$table = 'artwork';
$result = mysql_query("SELECT * from artwork
a join categories b on (b.CID = a.CID)
join subcategories c on (c.CID = b.CID and c.SCID = a.SCID)
where AID = $AID
order by DisplayOrder") or die(mysql_error());  
while($row = mysql_fetch_array($result)){   
    $AID = $row['AID'];
    $ArtFilePath = $row['ArtFilePath'];
    $ThumbFilePath = $row['ThumbFilePath'];
    $Title = $row['Title'];
    $Dimensions = $row['Dimensions'];
    $Medium = $row['Medium'];       
    $Comments = $row['Comments'];
    $DisplayOrder = $row['DisplayOrder'];
    $Details = $row['Details']; 
    $nextDO = $rom['CID = getArt.CID and SCID = getArt.SCID and DisplayOrder'];
}
mysql_free_result($result);

?>

<?php $nextDO = getArt.DisplayOrder +1; ?>
<?php $prevDO = getArt.DisplayOrder -1; ?> 

<?php
$dbname = 'pdartist2';
$table = 'artwork';
$result = mysql_query("SELECT AID from artwork where CID = getArt.CID and SCID = getArt.SCID and DisplayOrder = $nextDO") or die(mysql_error());
mysql_query($query);
?> 

每个查询都可以单独使用,但不会在多个查询中有效。就像我说的,在ColdFusion中,我只是给每个查询一个不同的名字。

2 个答案:

答案 0 :(得分:0)

使用PHP,您需要连接到数据库,上面的代码没有任何内容,您只需要为变量分配内容。首先,就像您需要连接到数据库的任何其他语言一样:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

然后你需要指定你想要访问的数据库:(在运行这些查询之前,不要关闭sql连接)

mysql_select_db($db_name);

然后你可以发送查询到数据库

$result = mysql_query('SELECT * WHERE 1=1');

现在,如果您想多次使用该查询,您可以复制粘贴上面的代码或分离查询并将其放入变量中,然后执行变量,如:

$q = 'SELECT * WHERE 1=1';
$result = mysql_query($q);

我希望能回答你的问题

答案 1 :(得分:0)

正如所怀疑的那样,最后你有一行缺少分号:

<?php
$dbname = 'pdartist2';
$table = 'artwork';
$query = ("SELECT AID from artwork where CID = getArt.CID and SCID = getArt.SCID and DisplayOrder = $nextDO");
//--missing ; here-----------------------------------------------------------------------------------------^^^^
$result = mysql_query("SELECT AID from artwork where CID = getArt.CID and SCID = getArt.SCID and DisplayOrder = $nextDO")
or die(mysql_error());
$result = mysql_query($query);
 ?>