dopuzf0898 2018-11-08 12:34
浏览 30


My code is Parsing HTML tags using regex and store all the links as an array inside the data base

I have a problem in my code i don't know how to fix it to save the links inside MySQL

i see this error message Error: SQLSTATE[HY093]: Invalid parameter number: Columns/Parameters are 1-based

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $domain = "";
    $input = @file_get_contents($domain) or die("Could not access file: $domain");
    $regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
    if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
        foreach($matches as $match) {
            // $match[2] = link address
            // $match[3] = link text
    $rows = array($domain, $url);
    $stmt = $conn->prepare("INSERT INTO linkss(id, domain, url) 
                            VALUES (NULL, :domain, :url)");
    foreach($rows as $key => $value){
        $stmt->bindParam($key, $value);
    $stmt -> execute();  
    echo "New records created successfully"; 
catch(PDOException $e){
    echo "Error: " . $e->getMessage();
$conn = null;
  • 写回答

2条回答 默认 最新

  • douzhouhan4618 2018-11-08 12:39

    The way you are passing the data to the prepared statement is incorrect, you using the index to the array - which is a 0 based numerically indexed array. This is why you get the error. Although not sure why you need this array...

    $rows = array($domain, $url);

    Instead I would suggest using...

    $stmt = $conn->prepare("INSERT INTO linkss(id, domain, url) 
                              VALUES (NULL, :domain, :url)");
    foreach($url as $value){
         $stmt->bindParam(':domain', $domain);
         $stmt->bindParam(':url', $value);
         $stmt -> execute();  

    This should also insert a record for each URL rather than the last one as the execute() is inside the loop.


    You also need to amend the code which builds the list of URL's, this was previously overwriting the last URL all the time, this will create a list of all of the URLs...

    $url = array();
    foreach($matches as $match) {
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示