PHP函数不起作用

时间:2017-07-25 02:42:27

标签: php mysql function mysqli

我正在将我的所有代码转换为mysqli而不是mysql。页面似乎正在进行查询并正确插入。但是,我无法让功能发挥作用。根据我看过的几个网站以及这个网站,我的语法应该是正确的。此外,所有这些在转换为mysqli之前都有效。

我已尝试直接在php文件(例如index.php)中以及在单独的文件中(这是我希望它的位置)。我得到了相同的结果。此示例是下拉列表的函数。

newserviceint.php:

<?php

    include 'php/phpfunctions.php';

    $con = mysqli_connect("mysql01", "jeremy","supersecret", "mycars");

    if (mysqli_connect_errno()) {

        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

?>

<?php

    $id = $_GET["id"];

    $sql = "    SELECT  vehicles.VehicleID,
                        vehicles.VYear, 
                        vmake.VMake, 
                        vmodel.VModel

                FROM    vehicles 
                        INNER JOIN mycars.vmake ON vehicles.VMakeID = vmake.VMakeID
                        INNER JOIN mycars.vmodel ON vehicles.VModelID = vmodel.VModelID

                WHERE   vehicles.VehicleID = '$id' ";

    $result = mysqli_query($con, $sql);

    while($row = mysqli_fetch_assoc($result)){

        $year       =   $row['VYear'];
        $make       =   $row['VMake'];
        $model      =   $row['VModel'];
        $vid        =   $row['VehicleID'];

    }

?>

<body>

    <div class="container">

        <?php include ("layout/header.php"); ?>

        <?php include ("layout/banner.php"); ?>

        <div class="maininfo">

            <h1>
                New Service Interval
            </h1>

            <div class="profiletable">
                <div class="previous">

                    <form action="php/newserviceintcode.php" method="post">

                        <table style="width:575px">
                            <tr>
                                <th style="width:175px">Vehicle</th>
                                <td style="width:375px"><input type="text" name="vehicle" style="width:40px" value="<?php echo $vid; ?>"> <?php echo $year; ?> <?php echo $make; ?> <?php echo $model; ?></td>
                                <td style="width:25px"></td>
                            </tr>
                            <tr>
                                <th>Service</th>
                                <td><select name="service" style="width:344px"><?php service() ?></select></td>
                                <td><a href="newservice.php"><img src="images/addnew.png" width="33px" height="25px"></a></td>
                            </tr>
                            <tr>
                                <th style="width:175px">Mileage Interval</th>
                                <td style="width:375px"><input type="text" name="miles" style="width:340px"></td>
                                <td></td>
                            </tr>
                            <tr>
                                <th style="width:175px">Time Interval</th>
                                <td style="width:375px"><input type="text" name="time" style="width:340px"></td>
                                <td></td>
                            </tr>
                            <tr>
                                <th style="height: 75px">Notes</th>
                                <td><textarea type="text" name="notes" style="width:340px" rows="4"></textarea></td>
                                <td></td>
                            </tr>

                        </table>

                        <br>

                        <input type="submit" value="Submit">

                    </form>

                </div>
            </div>
            <br>

        </div>

        <?php include ("layout/footer.php"); ?>

    </div>

    <!-- end .container -->
</div>

</body>
</html>

大约一半,你会看到这一行:

<td><select name="service" style="width:344px"><?php service() ?></select></td>

在mysql中有用的是这将为我提供一个下拉列表,该列表由以下列出的函数提供:

的PHP / phpfunctions.php

function service(){
    $qservice = mysqli_query( "SELECT VServiceID, Title FROM vservice ORDER BY Title");
    while($record = mysqli_fetch_array($qservice)){
        echo '<option value="' . $record['VServiceID'] . '">' . $record['Title'] . '</option>';
    }
}

此页面上还有其他一些功能,但所有这些功能都给了我完全相同的结果。我甚至几乎从this site复制了连接函数,但它只是表现得没有任何东西。如上所述,我已经在页面内部和外部文件中尝试了这两种方法 - 两者结果相同。

当合并到一个函数中时,你有没有与mysqli vs mysql有什么不同?根据我发现的搜索结果以及它之前的工作情况,这应该可行。

提前致谢

1 个答案:

答案 0 :(得分:1)

function service(){内,第一个参数应该是连接,第二个参数应该是您的查询字符串,READ MORE

$qservice = mysqli_query( "SELECT VServiceID, Title FROM vservice ORDER BY Title");

您需要连接

$connection = mysqli_connect("mysql01", "jeremy","supersecret", "mycars");
$qservice = mysqli_query( $connection, 'your query string' ); 

OR 传递连接对象

<?php service($con) ?>

并修改功能

function service($connection)
{
    $qservice = mysqli_query($connection,  "SELECT VServiceID, Title FROM vservice ORDER BY Title");
    // your remaining code goes here 
}