2014-05-13 1 views
0

일부 구문 분석 된 데이터를 mysql 데이터베이스에 저장하려고한다. 나는이 질문은 이미 여러 번 요청 알고간단한 HTML DOM Parser를 사용하여 원격 HTML 파일을 구문 분석 할 때받은 값이 mysql 데이터베이스에 저장되지 않음

Warning: mysqli_query() expects parameter 1 to be mysqli, string given 

: 나는 아무것도 발생하지 않았다, mysql_query를 사용하여 때

<?php 
include('mysql_connection.php'); 
include('simplehtmldom_1_5/simple_html_dom.php'); 

$site = "www.xyz.com/19326072316"; 
$html = file_get_html($site); 
foreach($html->find('body') as $body) 
{ 
    foreach($body->find('a.url') as $e) 
    { 
     $title = $e->plaintext; 
     echo '<b>Title: </b>' . $title . '<br>'; 
    } 
    foreach($body->find('a.category') as $cat) 
    { 
     $category = $cat->plaintext; 
     echo '<b>Category: </b>' . $category . '<br>'; 
    } 
    preg_match('/(\w+)\.xyz\.com\/.+/', $site, $matches); 
    $city = $matches[1]; 
    echo '<b>City: </b>' . $matches[1] . '<br>'; 

    foreach($body->find('div.month') as $month){ 
     $month = $month->plaintext; 
     echo '<b>Start and end month: </b>' . $month . '<br>'; 
    } 
    foreach($body->find('div.date') as $date){ 
     $date = $date->plaintext; 
     preg_match('/([0-9]{1,2})/', $date, $match_date); 
     $date = $match_date[0]; 
     echo '<b>Start and end date: </b>' . $date . '<br>'; 
    } 
    foreach($body->find('li.new_WatchIcon') as $time){ 
     $time = $time->plaintext; 
     echo '<b>Time: </b>' . $time . '<br>'; 
    } 
    foreach($body->find('li#new_locationIconIE7 div') as $address){ 
     $address = $address->plaintext; 
     echo '<b>Address: </b>' . $address . '<br>'; 
    } 
    foreach($body->find('span.description') as $description){ 
     $description = $description->innertext; 
     echo '<b>description: </b>' . $description . '<br>'; 
    } 

    $query = ("INSERT INTO articles (event_name, date_added, start_date, start_month, end_date, end_month, year, city, state, full_address, time, description, contact) VALUES('$title', now(), '$date', '$month', '$date', '$month', '2014', '$city', 'Karnatka', '$address', '$time', '$description', 'NULL')") or die(mysql_error()); 

    $run_query = mysqli_query($query, $connection); 
} 
?> 

하지만 다음 mysqli_query을 사용할 때 나는 다음과 같은 오류가 발생했습니다 : 내 코드는 아래와 같습니다 하지만이 문제를 해결하기위한 대부분의 방법을 시도했지만 나에게는 아무런 효과가 없었습니다!

+0

mysqli_query에 대한 호출 결과를 확인하지 않습니다. 또한 mysql_query와 mysqli_query는 서로 관련이 없다는 점에 유의하십시오. 교환 할 수 없습니다. –

답변

0

mysqli_query()의 매개 변수 순서가 잘못되었습니다.

// Prepare the statement before your outer foreach loop: 
$query = "INSERT INTO articles (event_name, date_added, start_date, start_month, end_date, end_month, year, city, state, full_address, time, description, contact) 
      VALUES (?, now(), ?, ?, ?, ?, '2014', ?, 'Karnatka', ?, ?, ?, NULL)"; 
// instead of mysqli_query use in the loop 
if ($stmt = mysqli_prepare($connection, $query) { 
    // your parameters are all of string type and you have 9 
    mysqli_stmt_bind_param(
     $stmt, 
     'sssssssss', 
     $title, $date, $month, $date, $month, $city, $address, $description); 

    foreach($html->find('body') as $body) { 
     // Please take care, that you only can assign values to those variables 

     // and execute the insert at the end of your loop 
     mysqli_stmt_execute($stmt); 
    } 
} 

비고 : 대신 당신의

$run_query = mysqli_query($query, $connection); 

의 당신이 당신의 INSERT 쿼리에 대한 자리 표시 자와 준비된 문을 사용하는 경우

$run_query = mysqli_query($connection, $query); 

그것은 훌륭한 향상 될 것해야한다 : 쿼리의 마지막 매개 변수가 NULL이고 문자열은 'NULL'이 아닌 것으로 생각했습니다.

+0

이제 치명적인 오류가 발생합니다. 변수 만 참조로 전달할 수 있습니다. 모든 변수를 두 곳에서 선언해야합니까? mysqli_stmt_bind_param에서 $ 쿼리와 다른 쿼리 중 하나? – user2902000

+0

@ user2902000 죄송합니다. 잘못되었습니다. 루프에서 문을 준비하고 심지어 루프 앞에있는 변수를 바인딩해야합니다. 루프에서 값 할당 만 사용하십시오. – VMai

+0

이제 내 코드입니다. $ query = ("INSERT INTO articles \t VALUES ('$ title', now(), '$ date1', ... NULL)"); if ($ stmt = mysqli_prepare ($ connection, $ query)) { \t mysqli_stmt_bind_param ($ connection, $ title, ...); } foreach는 ($, HTML> $ 본체로 ('몸') 찾기) { \t의 foreach ($와 같은 신체 - $> 찾기 ('a.url')를 전자) { {...} } mysqli_stmt_execute ($ stmt); 하지만 여전히 동일한 오류가 발생합니다. – user2902000