使用连接查询从数据库打印数据

时间:2015-04-28 07:01:09

标签: php mysql join

新手加入查询。我已经为两个表写了一个连接查询。候选人和candidate_contact。 其中cand_number是候选表中的主键,而candidate_contact中是FK。 cand_cid也是我在candidate_contact中的主键  但在提交查询后,数据未显示。 它的唯一印刷直到-echo"结果&#34 ;;

if(isset($_POST['candi_s_by_code']))
{
  $sql="SELECT * FROM candidate,candidate_contact WHERE candidate.cand_number=candidate_contact.cand_number AND candidate.cand_number=$candname";   
  echo "selected candidate code";
} 
$result = mysql_query($sql) or die(mysql_error()); 
echo "in result";

while($row=mysql_fetch_array($result))
{ 
  $FirstName  =$row['cand_fname']; 
  $LastName=$row['cand_lname']; 
  $Mail=$row['cand_mail'];
  $Telephone=$row['cand_tel']; 
  $Mobile=$row['cand_mobile'];
}

3 个答案:

答案 0 :(得分:0)

你正在关闭按钮的sql查询点击括号,所以这样做我不知道你从哪里获得if(isset($_POST['candi_s_by_code'])) { $sql="SELECT * FROM `candidate,candidate_contact` WHERE `candidate.cand_number`=`candidate_contact.cand_number` AND `candidate.cand_number`='".$candname."'"; $result = mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_assoc($result)) { $FirstName =$row['cand_fname']; $LastName=$row['cand_lname']; $Mail=$row['cand_mail']; $Telephone=$row['cand_tel']; $Mobile=$row['cand_mobile']; }} 顺便说一下

$zip = new ZipArchive;
if ($zip->open('directory/to/zipfile') === TRUE) 
{
    $zip->extractTo('directory/where/to/extract', array('the/filename/that/is/most/similair/most/go/here'));
    $zip->close();
    echo 'ok';
} 
else 
{
    echo 'failed';
}

答案 1 :(得分:0)

确保查询中的变量$candname正确无误。你代码中我最好的客人是,你可以习惯使用$_POST['candi_s_by_code']代替$candname 试试这个

$sql="SELECT * FROM `candidate,candidate_contact` WHERE `candidate.cand_number`=`candidate_contact.cand_number` AND `candidate.cand_number`='".$_POST['candi_s_by_code']."'";   

答案 2 :(得分:0)

尝试检查您是否

$_POST['candi_s_by_code']使用echo $_POST['candi_s_by_code'];

和 使用$candname

echo $candname;

在选择查询之前。并按如下所示更改您的选择查询

select * from candidate join candidate_contact ON candidate.cand_number =candidate_contact.cand_number where candidate.cand_number='$candname';

如果您不想更改您的选择查询,请尝试在选择查询中的php变量周围添加单个引号'$ candname'