Machform error: Cardinality violation: 1242 Subquery returns more than 1 row

We have a multiuser Machform PHP application running on a Redhat 7 Linux server with Apache webserver and MariaDB database.

In a specific form after page 3, accessing this URI: confirm.php?id=NNNNNNNN&mf_page_from=3 where NNNNNNNN is the id number of the form,

The following error message appears:

SQL: [671] select A.element_id, A.option_id, (select if(B.element_matrix_parent_id=0,A.option, (select C.`option` from ap_element_options C where C.element_id=B.element_matrix_parent_id and C.form_id=A.form_id and C.live=1 and C.option_id=A.option_id)) ) ‘option_label’ from ap_element_options A left join ap_form_elements B on (A.element_id=B.element_id and A.form_id=B.form_id) where A.form_id=? and A.live=1 and B.element_type=’matrix’ and B.element_status=1 order by A.element_id,A.option_id asc Sent SQL: [680] select A.element_id, A.option_id, (select if(B.element_matrix_parent_id=0,A.option, (select C.`option` from ap_element_options C where C.element_id=B.element_matrix_parent_id and C.form_id=A.form_id and C.live=1 and C.option_id=A.option_id)) ) ‘option_label’ from ap_element_options A left join ap_form_elements B on (A.element_id=B.element_id and A.form_id=B.form_id) where A.form_id=’NNNNNNNN’ and A.live=1 and B.element_type=’matrix’ and B.element_status=1 order by A.element_id,A.option_id asc Params: 1 Key: Position #0: paramno=0 name=[0] “” is_param=1 param_type=2 Query Failed: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

The solution is to change these files:

/includes/entry-functions.php and export_entries.php

where line: C.option_id=A.option_id))

is changed to:

C.option_id=A.option_id LIMIT 1))

Alert Blog Admin php script

I had to send emails to all the Blog admins in our WordPress Multisite installation. This is how I did it:

[code lang=”php”]
<?php
# Alert Blog Admin php script
# connect to blog db, select from, find all blog admin email adresses
# get subject, body from pre-created local files, send the email

# how to run the script
# php alertblogadmin.php -s subjecttext.txt -b bodytext.txt

$mysqlserver="localhost";
$mysqluser="dbuser";
$mysqlpassword="XXXXXXX";
$database="blog";

$ERROR=0;

# This script require arguments
$arguments = getopt("s:b:f:dr");

# Test if array $arguments as at least s,b and r
# maybe to make sure that $arguments array contains at least 3 items?
if ( count($arguments) < 3 ){
echo "Missing arguments…\n";
echo "Usage: php alertblogadmin.php -s subjecttext.txt -b bodytext.txt\n";
die;
}

# 2 text files need to exist
if ( !file_exists($arguments["s"])) {
echo "File $arguments[s] does not exist?\n";
$ERROR=1;
}
if ( !file_exists($arguments["b"])) {
echo "File $arguments[b] does not exist?\n";
$ERROR=1;
}

if ($ERROR == "1"){
die;
}

$link = mysql_connect($mysqlserver, $mysqluser, $mysqlpassword);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
mysql_select_db ($database,$link);

# collect blogs
$sql="SELECT blog_id from wp_blogs where deleted not like ‘1’";
$result=mysql_query($sql,$link);
if (!$result) {
die(‘Invalid query: ‘ . mysql_error());
}

$i = "0";
$emailarray = array();

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$blog_id = $row[0];
$table = "wp_" . $blog_id . "_options";

# Find email adresses to admins in each blog in the system
$sql2="SELECT option_value FROM $table WHERE option_name = ‘admin_email’";
$result2=mysql_query($sql2,$link);
while ($row2 = mysql_fetch_array($result2, MYSQL_NUM)) {
$emailarray[$i] = $row2[0];
}
$i++;
}

# sort the email array and find unique emails
asort($emailarray);
$emailarray = array_unique($emailarray);

$emails = "";
foreach ($emailarray as $email) {
$emails .= $email.",";
}

$emails = substr($emails,0,-1);

# Could be smart to see who you will be sending your email to
echo $emails;

# open subject and body text files
$subject = file_get_contents($arguments["s"]);
$body = file_get_contents($arguments["b"]);
$from = $arguments["r"];

# Use this one to test before you actuelly send to $emails, which is a variabel that might contain hundreds of emails.
# You really would like to test first
$bcc = "user1@something.com,user2@something.net";
//$bcc = $emails; # uncomment this one, when you are ready to go

$headers = "From: noreply@something.com" . "\r\n" .
"Reply-To: noreply@something.com" . "\r\n" .
"Bcc: $bcc" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
$header_ = ‘MIME-Version: 1.0’ . "\r\n" . ‘Content-type: text/plain; charset=UTF-8’ . "\r\n";

mail(”, ‘=?UTF-8?B?’.base64_encode($subject).’?=’, $body, $header_ . $headers);
mysql_close($link);
?>

[/code]

Extract img url links from HTML document

I needed to find and get a webpage’s img src url links. I wanted to do this with a script on a regular basis. The solution I found was to use PHP domdocument:

http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php

[php]
<?php
$url="http://example.com";

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName(‘img’);

foreach ($tags as $tag) {
echo $tag->getAttribute(‘src’);
}
?>

[/php]

After spending some time using wget, cat, grep and so on to solve my problem, this little php code made my life easier 🙂