PHP报废... mysqli_error()期望参数1为mysqli,给定整数 [英] PHP Scrapping...mysqli_error() expects parameter 1 to be mysqli, integer given

查看:82
本文介绍了PHP报废... mysqli_error()期望参数1为mysqli,给定整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我解决了,这里是工作代码: http://pastebin.com/51TfZm2R 让我知道您是否有任何疑问,或者我在代码方面做错了什么.欢呼!

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!

嘿,我的php代码遇到了麻烦,在Zend Studio 10中调试它时给了我这个错误:

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

mysqli_error()期望参数1为mysqli,给定整数(第83行)

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

我希望将数据添加到事件"数据库中,但是当我在phpmyadmin中检查数据库和表时,未添加任何行.完全没有数据!

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);

?>

推荐答案

eventlist表中看不到任何记录的原因是由于以下问题:

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

  1. 您已将PID声明为not nullauto_increment,但是仍然明确地传递了NULL.
  2. 您的价值观没有正确传递.
  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.

请将您的插入查询更新为以下内容(这是您将插入查询分配给$sql变量的行:

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

这将解决您的问题.

尽管这可以解决您的问题,但是您可以通过构建完整的插入查询并仅对数据库进行一次调用来执行插入操作,而不用在循环中执行插入操作,从而优化此脚本.

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.

这篇关于PHP报废... mysqli_error()期望参数1为mysqli,给定整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆