PHP Scrapping…mysqli_error() expects parameter 1 t

2019-09-20 02:05发布

问题:

UPDATE: i solved it and here is the working code: http://pastebin.com/51TfZm2R let me know if u have any questions or if maybe i did something wrong on my part in the code. cheers!

hey there I'm having trouble with my php code, its giving me this error when debugging it in Zend Studio 10:

mysqli_error() expects parameter 1 to be mysqli, integer given (line 83)

I wish to add data into the 'events' database but when I check on the database and table in phpmyadmin no rows were added. no data at all!

any help is greatly appreciated! here is my code:

<?php
date_default_timezone_set('Europe/Belgrade');
header('Content-Type: text/plain; charset=utf-8');

// MySQL connection parameters
$hostname = "localhost"; 
$username = "bla1234";
$password = "blabla";
$database = "mysql";

// Create a database handle
$dbh = mysqli_connect($hostname, $username, $password, $database)
    or die("Unable to connect to MySQL");
echo "Connected to MySQL\n";

// Create database
$dbh->query("CREATE DATABASE events"); echo "Created events database\n";

// Select database
$dbh->query("USE events"); echo "Selected events database\n";

// Create table
$dbh->query("CREATE TABLE eventlist(PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID), Name VARCHAR(30), Day INT, Month VARCHAR(10), Until VARCHAR(200), Category VARCHAR(20), Info VARCHAR(200))"); echo "Created events table\n"; echo "\n\n";

// PHP Scrapping
$contents = file_get_contents('http://www.tob.rs/en/events_list.php');

$regexp1 = '/<div class="list\\_articles/';

$records = preg_split($regexp1, $contents);

for ($ix=1; $ix < count($records); $ix++) 
{
    $tmp = $records[$ix];

    preg_match('/events\\.php\\?id=[0-9][0-9][0-9]">(.*?)</', $tmp, $match_name);
    preg_match("/<p class='day'>(.*?)</", $tmp, $match_day);
    preg_match("/<p class='mon'>(.*?)</", $tmp, $match_month);
    preg_match("/>[\s]*(.*?)<a/", $tmp, $match_until);
    preg_match('/events_list\\.php\\?t=[0-9]">(.*?)</', $tmp, $match_cat);
    preg_match('/<p>(.*?)</', $tmp, $match_info);

    $Name = $match_name[1];
    $Day = $match_day[1];
    $Month = $match_month[1];
    $Until = $match_until[1];
    $Category = $match_cat[1];
    $Info = $match_info[1];

    echo "Name: " . $Name . "\n";
    echo "Day: " . $Day . "\n";
    echo "Month: " . $Month . "\n";
    echo "Until: " . $Until . "\n";
    echo "Category: " . $Category . "\n";
    echo "Info: " . $Info . "\n";

    echo "\n\n";
    echo "---------------------------------------------------------------------------------";
    echo "\n\n";

    $sql =  "INSERT INTO `events`.`eventlist` (PID, Name, Day, Month, Until, Category, Info) 
                                    VALUES (NULL, $Name, $Day, $Month, $Until, $Category, $Info)";

    mysqli_query($dbh, $sql);
}

mysqli_close($dbh);

?>

回答1:

The reason you do not see any records in your eventlist table is because of the following problems:

  1. You've declared PID to be not null and auto_increment but still you are explicitly passing NULL.
  2. You values are not being passed correctly.

Please update your insert query to be the following (This is the line where you as assigning the insert query to $sql variable:

$sql = "INSERT INTO `events`.`eventlist` (Name, Day, Month, Until, Category, Info) VALUES ('{$Name}', '{$Day}', '{$Month}', '{$Until}', '{$Category}', '{$Info}')";

This will fix your problem.

Although this fixes your problem, you could optimize this script my building the complete insert query and making just one call to the database to do the insert instead of executing the insert in a loop.