Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4084886
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:29:49+00:00 2026-05-20T18:29:49+00:00

I am trying to make a long list of xpath query tests into a

  • 0

I am trying to make a long list of xpath query tests into a much more condensed function and having issues getting the xpath queries to work once pulled from mysql.

This is driving me batty, can anyone explain where I am going wrong please?

Example MySQL table:

CREATE TABLE `spidey_regex` (
  `id` int(12) NOT NULL auto_increment,
  `regexp` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM;
INSERT INTO `spidey_regex` (`id`, `regexp`) VALUES (1, '''//div[starts-with(@id, "stuff")]''');

Example PHP code using xpath:

<?php
   $html = '<div id="stuff123"><a href="link1.html">Link 1</a><a href="#link">Link 2</a><a href="link2.html">Link 3</a></div>';
   $cnt=0;
       $dom = new DOMDocument();
       @$dom->loadHTML($html);
       $dom->preserveWhiteSpace = false;
       $xpath = new DOMXPath($dom); 
     $myresult = mysql_query("select * from spidey_regex",$db);
   while($myrow = mysql_fetch_array($myresult)){
     $regexp = stripslashes(trim($myrow[regexp]));
     $result = $xpath->query($regexp);  // use xpath with query from MySQL
     foreach($result as $e){ $cnt++; }
       if($cnt == '0'){ 
         continue; 
       } 
       else { 
         echo "1. Got here!!"; 
       }
   }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:29:49+00:00Added an answer on May 20, 2026 at 6:29 pm
    INSERT INTO `spidey_regex` (`id`, `regexp`) VALUES (1, '''//div[starts-with(@id, "stuff")]''');
    

    The double-quote-to-escape thing is a bit weird. If we were to use the normal backslashes, instead we’d see:

    '\'//div[starts-with(@id, "stuff")]\''
    

    In other words, your XPath query has one too many layers of quotes, and is being rejected by the XPath parser. Once you remove the quotes from the value in the database, your query should work.


    Proof at the PHP Interactive prompt:

    php > $html = '<div id="stuff123"><a href="link1.html">Link 1</a><a href="#link">Link 2</a><a href="link2.html">Link 3</a></div>';
    php > $dom = new DOMDocument();
    php > $dom->loadHTML($html);
    php > $xpath = new DOMXPath($dom);
    php > $regexp = '//div[starts-with(@id, "stuff")]';
    php > $result = $xpath->query($regexp);
    php > echo $result->length;
    1
    php > $regexp = "'{$regexp}'";
    php > $result = $xpath->query($regexp);
    php > echo $result->length;
    0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a MySQL-based application support MS SQL, I ran into the following
I am trying to make a div, that when you click it turns into
I'm trying to figure out the best way to display a long list of
i'm trying to make an application, getting all the birthdays and other information from
I'm in the process of trying to make a game-in-progress more modular. I'd like
I am trying to make a call with a long number that looks something
Trying to make a make generic select control that I can dynamically add elements
Trying to make a generic PL/SQL procedure to export data in specific XML format,
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make the case for click-once and smart client development but my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.