I want to get all names of my products and push them into an array. If I use the SQL Query (SELECT name FROM wp_all_import_xml GROUP BY name) in phpmyadmin I get this:
But if I use the query in my script I get this array:
array (
0 => NULL,
1 => NULL,
2 => NULL,
3 => NULL,
4 => NULL,
...)
So, the query must return null. But why? - Everything is correct!
I do not get any connection error or query error... Where is my error?
I already tried to decode the strings and I also tried $myTitles[] = $tmp. But it also doesn't work... Any ideas?
And because everything in the $myTitles array is null, the script always enters the if(!(in_array($title, $myTitles) condition and I cannot execute what I actual want... I can only execute this, if the array and the condition is really true!
<?php
if ( ! defined('ABSPATH') ) {
// Set up WordPress environment
require_once( dirname( __FILE__ ) . '/wp-load.php' );
}
unlink('deleteOldProducts.txt');
$myfile = fopen('deleteOldProducts.txt', "w");
$database_gk = new mysqli("localhost", "censored", "censored", "censored");
$database_jt = new mysqli("localhost", "censored", "censored", "censored");
$myTitles = array();
$string = "";
if($database_jt->connect_errno){
$string .= "+++Couldn't connect to database_jt!+++\n\n");
}
if($database_gk->connect_errno){
$string .= "+++Couldn't connect to database_gk!+++\n\n");
}
$values_jt = $database_jt->query("SELECT name FROM `wp_all_import_xml` GROUP BY name");
$string .= $database_jt->error . "\n";
while($rowj = $values_jt->fetch_assoc()){
$tmp = utf8_encode($row["name"]);
array_push($myTitles, $tmp);
$tmp = null;
}
$values_gk = $database_gk->query("SELECT `post_title` FROM `fWR6qIN_posts` where post_type = 'product' AND post_status = 'publish' GROUP BY `post_title`");
$string .= $database_gk->error . "\n";
$string .= "+++ Start of Check +++\n\nMein Array:\n" . var_export($myTitles, true) . "\n\n";
$i = 1;
while($row = $values_gk->fetch_assoc()){
$title = utf8_decode($row["post_title"]);
if(!(in_array($title, $myTitles))){
$string .= $i . ":\t" . $title . "\n";
$i = $i + 1;
}
$title = null;
}
$string .= "\n +++ End of Check! +++";
fwrite($myfile, $string);
fclose($myfile);
?>
Greetings and Thank You!
Your while isn't correct. On while you are using $rowj but on array_push you are using $row. You can try the following code instead:
while($row = $values_jt->fetch_assoc()) {
$myTitles[] = utf8_encode($row["name"]);
}
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
http://php.net/manual/en/function.array-push.php
Related
I was recently tasked to update some older sites from MySQL to MySQLi.
Though slow and steady, the update has been ok until I ran into an issue when exporting some data to an excel document.
This code was written by a previous developer. There's a lot going on in the file, and I hope I'm grabbing the part that is supposed to be creating the excel document:
<?php
$export = mysqli_query ( $session->connDB(),$sql ) or die ( "Sql error : " . mysqli_error( ) );
$fields = mysqli_num_fields ( $export );
$num_rows = mysqli_num_rows( $export );
$pattern = '/[A-Z0-9][A-Z0-9._-]+#[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i'; //email
$phonpat = '/(\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4})(( |-|\.)?[ext\.]+ ?\d+)|\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4}))/i'; //telephone
$phPat = '/([0-9]{3})([0-9]{3})([0-9]{4})/';
$vippatt = '/VIP/i';
for($f=0; $f<$fields; $f++){
$header.='"'.mysqli_fetch_fields($export, $f).'"'."\t";
}
for($i=0; $i<$num_rows; $i++){
for($x=0; $x<$fields; $x++){
$email = mysqli_fetch_assoc($export,$i,"EMAIL");
$phone = mysqli_fetch_assoc($export,$i,"PHONE");
$viprm = mysqli_fetch_assoc($export,$i,"VIP");
preg_match ($pattern, $email, $matches);
preg_match ($phonpat, $phone, $phoneno);
preg_match ($vippatt, $viprm, $vpmatch);
if(isset($matches[0])) {$emal=strtolower($matches[0]);} else {$emal="";}
if(isset($vpmatch[0])) {$vips=strtoupper($vpmatch[0]);} else {$vips="";}
if(isset($phoneno[0])) {$phne=preg_replace($phPat,'($1) $2-$3 ',formatPhone($phoneno[0],false,false));} else {$phne="";}
if(mysqli_fetch_fields($export, $x)=='EMAIL'){
$fld=$emal;
} else {
if(mysqli_fetch_fields($export, $x)=='PHONE'){
$fld=$phne;
} else {
if(mysqli_fetch_fields($export, $x)=='VIP'){
$fld=$vips;
} else {
if(mysqli_fetch_fields($export, $x)=='UNITS'){
$fld=1;
} else {
$fld = mysqli_fetch_assoc($export,$i,mysqli_fetch_fields($export, $x));
}
}
}
}
$data.= '"'.$fld.'"'."\t";
}
$data.="\n";
}
?>
Here is where the code checks if the data is blank or not, and then exports the spreadsheet:
<?php
if ($data == "") {
$data = "\nNo records found for your search parameters.\n\n".$sql;
} else {
echo "should show data";
}
global $time;
$time = time();
header("Content-Disposition: attachment; filename=CargoManagementCustomReport-$time.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
?>
When the spreadsheet gets exported, I see "should show data". This tells me the $data variable obviously has data. It's just not getting into the spreadsheet.
If you'll notice in the above, I'm using mysqli_fetch_fields. This was used to replace mysql_field_name (in my attempt to update to MySQLi).
I also tried mysqli_fetch_field, but got the same results.
I am getting no errors, but the spreadsheet is still blank.
I can echo $sql to get the query, and I can run the query in the database and it returns data.
What am I doing wrong and how can I fix it?
That whole code is gibberish, so I hope I understood what it is that it was meant to do.
Here are the main problems:
mysqli_fetch_fields() takes only 1 argument and returns an array of objects. You can't cast an array to a string. I assume you wanted to get the field name.
mysqli_fetch_assoc() takes only 1 argument and returns an array of data in an associative array as the name suggests. It also moves the internal pointer to the next row every time it is called. You are trying to use it as if it was mysql_result().
Your nested loops are very messy. I replaced them with simple foreach loops and replaced the nested if statements with a switch. While I would normally stay away from such constructs, this is the easiest way to migrate this code.
After removing all the mysqli nonsense, the code is now readable. It iterates over every field of every row, applying some transformations to some fields and concatenating the result into a string.
Fixed code:
$conn = $session->connDB();
$export = mysqli_query($conn, $sql);
$pattern = '/[A-Z0-9][A-Z0-9._-]+#[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}/i'; //email
$phonpat = '/(\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4})(( |-|\.)?[ext\.]+ ?\d+)|\(?([0-9]{3})+\)?[\-\s\.]?([0-9]{3})+[\-\s\.]?([0-9]{4}))/i'; //telephone
$phPat = '/([0-9]{3})([0-9]{3})([0-9]{4})/';
$vippatt = '/VIP/i';
foreach (mysqli_fetch_fields($result) as $field) {
$header .= '"' . $field->name . '"' . "\t";
}
$data = '';
foreach ($export as $row) {
foreach ($rows as $fieldName => $value) {
switch ($fieldName) {
case 'EMAIL':
preg_match($pattern, $value, $matches);
$data .= '"' . (isset($matches[0]) ? strtolower($matches[0]) : '') . '"' . "\t";
break;
case 'PHONE':
preg_match($phonpat, $value, $phoneno);
$phne = "";
if (isset($phoneno[0])) {
$phne = preg_replace($phPat, '($1) $2-$3 ', formatPhone($phoneno[0], false, false));
}
$data .= '"' . $phne . '"' . "\t";
break;
case 'VIP':
preg_match($vippatt, $value, $vpmatch);
$data .= '"' . (isset($vpmatch[0]) ? strtolower($vpmatch[0]) : '') . '"' . "\t";
break;
case 'UNITS':
$data .= '"1"' . "\t";
break;
default:
$data .= '"' . $value . '"' . "\t";
break;
}
}
$data .= "\n";
}
I have a fairly specific scenario that I'm struggling with. The frustration is that it shouldn't even be complicated!
I have a SQL database and I'm connecting to it using PHP + SQLSRV. The database structure is quite straight forward and currently has 2 varchar columns (ColumnA and ColumnB for this example).
My task is to read the data from this table, allow the values to be edited in a form and write back to the database. This part is easy enough and is all working.
The challenge now is that the database values need to be written to my (currently empty) values.php file in the following format.
<?php
$output = array (
"Val1" => 'aaa',
"Val2" => 'bbb',
"Val3" => 'ccc',
);
?>
Ideally, I'd like this to be done at the time of the save to database command which I can do as part of an AJAX success process so that part shouldn't be an issue
I started working on the following process;
<?php
$sql = "SELECT ColumnA, ColumnB FROM Table";
$stmt = sqlsrv_query( $conn2, $sql );
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$test[]= array($row['ColumnA'] ." => '". $row['ColumnB']."',");
$values = implode(PHP_EOL,$test);
}
$content = '<?php
$output = array ("'.$values.'");
?>';
file_put_contents("c:/jjj/values.php", $content);
?>
This gives me a lot of "Array to string conversion" errors and the file gets populated with;
<?php
$output = array ("
Array
Array
);
?>
Where am I going wrong here?
EDIT
After the various comments below, the current code reads as;
<?php
$sql = "SELECT ColumnA, ColumnB FROM Table";
$stmt = sqlsrv_query( $conn2, $sql );
$test = [];
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$test[] = [$row['ColumnA'] => $row['ColumnB']];
}
$content = "<?php" . PHP_EOL;
$content .= "\t" . '$output = [' . PHP_EOL;
foreach($test as $key => $value) {
$content .= "\t\t\"{$key}\" => \"{$value}\"," . PHP_EOL;
}
$content .= "\t];" . PHP_EOL;
$content .= "?>";
file_put_contents("c:/jjj/values.php", $content);
?>
This results in 3 Array to string conversion errors (One for each row) and an output file that looks like this:
<?php
$output = [
"0" => "Array",
"1" => "Array",
"2" => "Array",
];
?>
I still can't find a way to export my DB table to a file in the correct format using PHP.
I think this will come closer to what you want:
To echo tabs as well you could use \t within the double quotes
$content = "<?php" . PHP_EOL;
$content .= "\t" . '$output = [' . PHP_EOL;
foreach($test as $key => $value) {
foreach ($value as $subkey => $subvalue) {
$content .= "\t\t\"{$subkey}\" => \"{$subvalue}\"," . PHP_EOL;
}
}
$content .= "\t];" . PHP_EOL;
$content .= "?>";
what i try to do ?
i try to make code to found if there duplicate data into database or not ,that data it text Content written by users .
so i make sql query to get result of row pagetext from database and clean it from any Signs And coordinate (font color, font size, and font type) and image links or any links , and same i do it for Variable $post['message'] that get Content of text area ..
than i check if Content of $post['message'] is same Content of pagetext or not !
Full code :
// FUNCTION TO CLEAN TEXT
function stripBBCode($text_to_search)
{
$pattern = '|[[\/\!]*?[^\[\]]*?]|si';
$replace = '';
return preg_replace($pattern, $replace, $text_to_search);
}
// MYSQL QUERY
$ckeck = $db->query_read(" SELECT pagetext FROM " . TABLE_PREFIX . " post ");
$ckeck_num = mysql_num_rows($ckeck);
while ($ckeckpagetext = $db->fetch_array($ckeck))
{
// RESULT FROM QUERY - here i try to make ARRAY BY [square brackets]
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $pagetext);
// Variable
$message = stripBBCode($post['message']);
$message = preg_replace('/[\s]+/mu','',$message );
$message = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $message);
// LOOP
for($x=0; $x<$ckeck_num; $x++)
{
// CHECK IF THERE duplicate TEXT OR NOT
if ($message == $pagetext[$x])
{
$ckeck_duplicate = 1;
}else{
$ckeck_duplicate = 2;
}
}
}
My problem ?
my code Almost correct , but my problem in those lines [square brackets] . when i try great array for my result
// RESULT FROM QUERY
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext[] = preg_replace('/[\s]+/mu','', $pagetext);
$pagetext[] = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $pagetext);
if i used only first line without used preg_replace the code works good .
when i use [square brackets] for a
$pagetext[] = stripBBCode($ckeckpagetext['pagetext']);
$pagetext = array();
while ($ckeckpagetext = $db->fetch_array($ckeck))
{
$tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
$tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
$tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $tmpCheckPageText);
$pagetext[] = $tmpCheckPageText;
}
And array is:
$pagetext = array( 'Plain text1', 'Plain text2' );
Try Your function stripBBCode and preg_replace on variable for example $tmpCheckPageText and after this put into array $pagetext
Code:
$tmpCheckPageText = stripBBCode($ckeckpagetext['pagetext']);
$tmpCheckPageText = preg_replace('/[\s]+/mu','', $tmpCheckPageText);
$tmpCheckPageText = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|$!:,.;]*[A-Z0-9+&##\/%=~_|$]/i', '', $tmpCheckPageText);
$pagetext[] = $tmpCheckPageText;
UPDATE:
Create array of pagetext from db using sql statement and purge it in while loop. For example you got this:
$pagetext = array( 'Purge Text 1', 'Purge Text 2', 'Purge text 3' );
After this, purge Your post field $message. Next check with in_array function.
echo in_array( $message, $pagetext ) ? 1 : 2;
I have a sitemap that's generated with PHP and actually its calling only the table retailers as below:
$query = "select * from retailers WHERE status='active' ORDER BY added DESC";
and construct as:
while ($row = mysql_fetch_array($result))
{
$i_url = SITE_URL.'loja/'.$row['slug_title'];
$year = substr($row['added'],0,4);
$month = substr($row['added'],5,2);
$day = substr($row['added'],8,2);
$i_date = ''.$year.'-'.$month.'-'.$day.'';
// you can assign whatever changefreq and priority you like
// changefreg - optional
// priority - optional
echo
'
<url>
<loc>'.$i_url.'</loc>
<lastmod>'.$i_date.'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
The problem is, only retailers page its coming, i need to get some more 3 tables, but i can't think on a way to call and construct more things inside that, maybe a PHP condition?
Thanks to everyone for your time!
I suggest you to create a function to handle the queries or subqueries you need
Like
Main Code
while ($row = mysql_fetch_array($result))
{
$i_url = SITE_URL.'loja/'.$row['slug_title'];
$year = substr($row['added'],0,4);
$month = substr($row['added'],5,2);
$day = substr($row['added'],8,2);
$i_date = ''.$year.'-'.$month.'-'.$day.'';
$data = subquery('what i need here', 'another param');
echo
'
<url>
<loc>'.$i_url.'</loc>
<lastmod>'.$i_date.'</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
';
}
function subquery($firstparam, $secondparam)
{
$myquery = "SELECT * FROM ".$firstparam;
//more code
$result = 'my query result';
return $result;
}
With this you can call a subquery based on the main query, you can create more funcionts or create only one with different types that, can able you to do different queries in one function.
Since the tables all have the same fields we need (slug_name and added), we can just loop through each table with the same procedure, then output to sitemap.xml file.
// Our Echo Buffer
$buffer = array();
// Table Names
$tables = array( 'retailers', 'table2', 'table3', 'table4' );
// Using MySQLi, cause it's Improved.
$conn = new MySqli( 'localhost', 'user', 'pass', 'database' );
// Iterate over $tables
foreach( $tables as $table )
{
// Build Query
$query = "SELECT `slug_name`, `added` FROM $table" .
" WHERE status='active' ORDER BY added DESC";
// Get Result
$result = $conn->mysqli_query( $query );
// Iterate over Result
while( $row = $result->fetch_assoc() )
{
// Chop up the Date
$date = substr($row['added'],0,4) . '-' .
substr($row['added'],5,2) . '-' .
substr($row['added'],8,2);
// Add page details to $buffer
$buffer[] = '<url>' .
'<loc>' . SITE_URL . 'loja/' . $row['slug_title'] . '</loc>' .
'<lastmod>' . $date . '</lastmod>' .
'<changefreq>daily</changefreq>' .
'<priority>0.8</priority>' .
'</url>';
}
// Free MySQLi Result
$result->close();
}
// Output the Buffer to view. Make sure it looks good.
echo implode( "\r\n", $buffer );
// Remove the echo above and uncomment below if it looks good.
// if( ( $xml = fopen( 'sitemap.xml', "w" ) ) !== FALSE )
// {
// fwrite( $xml, implode( "\r\n", $buffer ) );
// }
I'm trying to get my function to display my categories absolute url address for example http://www.example.com/cat/sub-1/sub-2/sub-3/ But I keep getting http://www.example.com/cat//sub-3/ can some one help me correct this problem. Please be detailed as possible since I'm farily new to PHP.
Here is my PHP function
function allCategories(){
global $parent_url;
global $url;
$nodeList = array();
$tree = array();
$query = mysqli_query(database(),"SELECT * FROM categories ORDER BY parent_id, category LIKE '%more%', category ASC");
while($row = mysqli_fetch_assoc($query)){
$nodeList[$row['id']] = array_merge($row, array('children' => array()));
}
mysqli_free_result($query);
foreach($nodeList as $nodeId => &$node) {
if(!$node['parent_id'] || !array_key_exists($node['parent_id'], $nodeList)){
$tree[] = &$node;
$url = $parent_url . $node['url'];
$url = str_replace('?cat=', '', $url);
echo '<li>' . strip_tags($node['category']) . '';
} else {
$nodeList[$node['parent_id']]['children'][] = &$node;
$url = $parent_url . $node['url'];
$cat_num = array('?cat=','&sub1=','&sub2=');
$url = str_replace($cat_num, '/', $url);
echo '<li>' . strip_tags($node['category']) . '';
}
echo '</li>';
}
echo '</ol>';
unset($node);
unset($nodeList);
}
allCategories();
I suspect your query is erroring out at the MySQL level, and you don't have anything set up to tell you so (especially if warnings are turned off in the php.ini file).
Try adding something like this to the line that starts with $query:
or die( "<h1>SELECT query failed!</h1> <p>Error: " . mysqli_error( $dbc ) . "</p>" );
$dbc needs to be replaced with whatever variable holds your database connection.
Obviously, this is for debugging only. You would replace die with some error-handling function on a production server.