dshfjsh_5455 2012-04-28 12:37
浏览 80
已采纳

php mysql fetch语句提取问题

i have a problem my script has three mysql_query which should be used after each other , i am trying to create a script that reserve tickets by changing their status from sold = "No" to "Yes", the script count the number of tickets user has entered on html form which give the server side a variable with number of tickets called = $tickets.

hint : this is such a model so no need for mysql injection security

here is my code :

//get ticket status
    $eventTicket = mysql_query("SELECT eventTickets FROM beventreservation WHERE eventId = '$eventId'") or die(mysql_error());
    $ticketrow = mysql_fetch_array($eventTicket) or die(mysql_error());


    //test... which is working !
    echo $ticketrow['eventTickets'];


    //get classId from classes
    $selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());    

    $classrow = mysql_fetch_array($selectClass) or die(mysql_error());

    //this var is to define which class the user used
    $choosedClass = $classrow['classId'];

//test ... which did not work !!!
echo $classrow['classId'];

        if ($ticketrow['eventTickets'] == "Yes")

    {
        for($counter=1;$counter<$numberOfTickets;$counter++)

        {
            $bookTicket = mysql_query("UPDATE unites SET ticketSold = 'Yes' WHERE businessreservationIdFk = '$eventId' AND classIDfk ='$choosedClass'") or die(mysql_error());  
            echo "ticket ". $counter . "  done !";


        }

    }

the script doesn't fetch this syntax, and there is no errors showed on my page !

$classrow = mysql_fetch_array($selectClass) or die(mysql_error());

also , i tried to echo the variable $tickets after this syntax , it did not showed up, is there a problem to fetch more than mysql_query on the same script page ? tell me where do i go wrong here please .

  • 写回答

1条回答 默认 最新

  • duancaozen6066 2012-04-28 12:41
    关注

    Don't call die() in conjunction with a mysql_fetch_*() call. If there are no rows returned, mysql_fetch_array() returns FALSE, which triggers your die() and kills your script even though there was no error. Since you have already don error checking on $selectClass in the mysql_query() call, you know it has succeeded.

    // This query returned no rows, but was successful syntactically and functionally.
    $selectClass = mysql_query("SELECT classId FROM quotaclasses WHERE className = '$classes' AND eventFK = '$eventId'") or die (mysql_error());    
    

    Instead, test if rows were returned:

    if (mysql_num_rows($selectClass) > 0) {
      // Fetch and do other stuff
       $classrow = mysql_fetch_array($selectClass);
       $choosedClass = $classrow['classId'];
       // etc...
       // etc...
    }
    else {
      // Do whatever you need to do if no rows return
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条