0

Hello,

I have a problem with my php script.
Everything works fine except when i will write the data to the database.
I get a error Warning: Wrong parameter count for mysql_query()

Here is my code:

<?php
 include('../sec/inc_mysql_connect.php');
 include 'googledistance.class.php';
 $sql = "SELECT VVBnummer, Adres, Postcode FROM tblscheidsrechters";// echo($sql);
 $result = mysql_query($sql);

 $sql_sh = "SELECT ID, SporthalAdres, Postcode FROM tblsporthal"; //echo('<br>' . $sql_sh);
 $result_sh = mysql_query($sql_sh);
 while($record = mysql_fetch_array($result))
 {
  while($record_sh = mysql_fetch_array($result_sh))
  {
   $fromAddress = $record['Adres'] . ',' . $record['Postcode']; //echo($fromAddress . '<br>');
   $toaddress = $record_sh['SporthalAdres'] . ',' . $record_sh['Postcode']; //echo($toaddress . '<br>');

   $gd = new GoogleDistance($fromAddress, $toaddress);

   $vvb = $record['VVBnummer'];
   $shid =  $record_sh['ID'];
   $afstand = $gd->getDistance();
   $tijd = $gd->getDuration();


   ?>
   <body> 
        <p>Scheidsrechter: <?php echo($record['VVBnummer']); ?></p>
        <p>Sporthal: <?php echo($record_sh['ID']); ?></p>        
        <p>Afstand in km (h/t): <?php echo $gd->getDistance()/1000 ; ?></p>
        <p>Tijd in minuten: <?php echo $gd->getDuration()/60; ?></p>
        <p>Gevonden oorsprong: <?php echo $gd->getOrigin(); ?></p>
        <p>Gevonden bestemming: <?php echo $gd->getDestination(); ?></p>
        <hr />
    </body>
   <?php
   $sql = "INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('$vvb', '$shid', '$afstand', '$tijd')"; echo($sql);
   $record = mysql_query();
  }
 }
?>

the google class is:

<?php

class GoogleDistance
{
    private $obj;

    function __construct($origin, $destination)
    {
        $this->obj = $this->run($origin, $destination);
    }

    public function getObject()
    {
        return $this->obj;
    }

    public function getOrigin()
    {
        return $this->obj->origin_addresses[0];
    }

    public function getDestination()
    {
        return $this->obj->destination_addresses[0];
    }

    public function getDistance()
    {
        return $this->obj->rows[0]->elements[0]->distance->value;
    }

    public function getDuration()
    {
        return $this->obj->rows[0]->elements[0]->duration->value;
    }

    private function run($origin, $destination)
    {     
        $path = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' . 
            $this->formatstring($origin) . '&destinations=' . 
            $this->formatstring($destination) .'&language=nl_NL&sensor=false';

        // our curl handle (initialize ifrequired)
        static $ch = null;
        if (is_null($ch)) {
            $ch = curl_init();
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $path);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        // run the query
        $res = curl_exec($ch);
        if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
        $dec = json_decode($res);
        if(!$dec)
        {
            echo '<pre>';
            print_r($res);
            echo '</pre>';
            throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
        }
        return $dec;
    }

    private function formatstring($text)
    {
        return str_replace(' ', '+', $text);
    }

};

Thx for the help

4
Contributors
4
Replies
18
Views
4 Years
Discussion Span
Last Post by Atli
0

I'm assuming that this is the issue:

INSERT INTO klvv_sr_afstand_sh ( vvb_nr_sr, shid, afstand, tijd) VALUES('$vvb', '$shid', '$afstand', '$tijd')

There are 4 fields and four values, so the count seems OK. However - are these inputs sanitized? Beware of SQL injection.

I'm assuming that you do not have a primary key in the field list. If so, then you'd do well to set that value to NULL.

0

The old (and deprecated) MySQL API doesn't support parameterized queries, so it wouldn't complain about invalid parameter counts. MySQL might return such errors if they exist in the SQL query, but since there is no handling of errors in that code, that wouldn't be an issue here either. (MySQL execution errors aren't automatically reported by PHP.)

My guess is that the problem occurs on line 36:

$record = mysql_query();

You define the $sql variable, but don't pass it into the mysql_query call. It's expecting at least one parameter, but you give it none.

I'm assuming that you do not have a primary key in the field list. If so, then you'd do well to set that value to NULL.

That's actually not necessary. By excluding a field from the INSERT query field list, it's automatically set to NULL. MySQL indentity fields (auto_increment) really shouldn't be included, unless you need to manually specify the next ID number.

0

P.S.
The names of the databse tables and fields are horrible. Make it very hard to figure out what's even meant to be happening there. Explicit naming is always better in the long run. Makes code so much easier to maintain, especially if the project is passed on to other people. Acronyms and vague phrases just make things more difficult, even if they make the code look shorter. (Trust me, I've been on the receiving end of such a project.)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.