dongqianzhan8325 2012-08-26 19:18
浏览 673
已采纳

while($ row = mysqli_fetch_array($ result)){当选择多个表时

I'm creating this search function with PHP, javascript and mysql.

This code is working like it should. I type in some data in a a html search box, which sends the %keyword% to this PHP function, which returns the data as I want.

$query = "SELECT id,fornavn,efternavn,adresse,postnr,city,tlf,kon,email,fodselsdag FROM kunde WHERE email LIKE '%$keyword%' OR fornavn LIKE '%$keyword%'";


$result = mysqli_query($dbc,$query);

if($result){
    if(mysqli_affected_rows($dbc)!=0){
      while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
        listings($row['fornavn'],$row['efternavn'], $row['email'],$row['adresse'],$row['tlf'],$row['postnr'],$row['city'],$row['fodselsdag'],$row['id'],$bartype);
    }

I have another TABLE where the id from the SQL above is stored in relation to another ID. The table looks like this:

Table name: vip
Colmns: id, brugerid, barid

Now I want to be customize the SQL in the code above, so it only picks data that also has a relation in the vip table.

I do it this way:

SELECT *, kunde.id as kundeid FROM kunde,vip WHERE  vip.brugerid = kunde.id AND vip.barid =1 AND email LIKE '%$keyword%' OR fornavn LIKE '%$keyword%';

Actually whenever I add more than one table, the php code stops working.. any idea why this is happening? And how I can achieve this?

Any help will be very appreciated

  • 写回答

3条回答 默认 最新

  • dqroktbn005028 2012-08-26 19:32
    关注

    http://dev.mysql.com/doc/refman/5.0/en/join.html

    SELECT *, kunde.id as kundeid FROM kunde INNER JOIN vip ON (vip.brugerid = kunde.id) WHERE  kunde.id=1 AND email LIKE '%$keyword%' OR fornavn LIKE '%$keyword%'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分