-1

I'm in the process of converting all of my code to mysqli instead of mysql. The pages seem to be doing their queries and inserts correctly. However, I cannot get functions to work. Based on the several sites, as well as this one, that I've looked at, my syntax should be correct. In addition, all of these worked prior to converting to mysqli.

I've tried having this directly in the php file (such as index.php) as well as in a separate file (which is where I want it to be). I get the same result. This example is a function for a dropdown list.

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>

About halfway down you see this line:

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

What worked in mysql was this would provide me a dropdown list that is provided by the function listed in:

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>';
    }
}

There are several other functions on this page, but all of them are giving me the exact same results. I've even pretty much copied the connection function from this site but it just acts like there's nothing there. As stated above, I've tried this both locally within the pages and through the external file - both with identical results.

Is there something different you have to do with mysqli vs mysql when incorporated into a function? Based on what I've found searching, and the fact that it worked before, this should work.

Thanks in advance

  • [`mysqli_query`](http://php.net/manual/mysqli.query.php) requires at least **two** arguments. You will need to pass in `$con` object to `service()` as an argument, eg `function service($con) { ... }` – Phil Jul 25 '17 at 02:45
  • Voting to close as a typo. You should increase your error reporting during development. You would then see *"Warning: mysqli_query() expects at least 2 parameters, 1 given"* – Phil Jul 25 '17 at 02:47
  • @Phil So this explains why the query functions don't work, but why wouldn't the connection function work? The code I have in the top php section I had setup as a function and it didn't work - basically giving me the same response as how the query wasn't working. It just acts like it's not there. – JeremyThePHPNoob Jul 25 '17 at 03:00
  • I don't really know what you're talking about but you're probably running into a [scoping issue](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and). – Phil Jul 25 '17 at 03:29

1 Answers1

1

Here inside function service(){, first argument should be connection and second one should be your query string, READ MORE

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

You need connection

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

OR pass connection object

<?php service($con) ?>

and modify function

function service($connection)
{
    $qservice = mysqli_query($connection,  "SELECT VServiceID, Title FROM vservice ORDER BY Title");
    // your remaining code goes here 
}
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36