I'm trying to connect to and import data into a WordPress 3.5.1 page template from an external database which is hosted on the same localhost server.
I've Googled this extensively and have come up with the most preferred solution for this:
<?php
/*
Template Name: Import Sign data
*/
?>
<?php echo "Import Sign Data Page<br>"; ?>
<?php
$mydb = new wpdb('myname', 'mypassword', 'mydb', 'localhost');
$mydb->show_errors();
$signs = $mydb->get_results("SELECT * FROM signs");
print_r($signs);
foreach ($signs as $sign) {
echo $sign->title . '<br />';
}
?>
The code above only gives me the first echo, 'Import Sign Data Page' and nothing else. No errors, nothing. I've checked all my syntax many times now. Can any see where I might have gone wrong here or perhaps suggest some debugging tips?
TIA
Using the wpdb class is by definition only going to work with the installed wordpress database. You should use whatever functions that pertain to your external database.
Related
I'm using the unyson Framework for WordPress and currently using page options which work just fine on that particular page.
But I want to be able to access those options when on another page.
Here is my php -
$page = fw()->theme->get_options( 'service-settings' );
<?php echo wp_kses_post($page['service']['options']['service-box']['options']['description']; ?>
But this doesn't allow me the content from the array.
Using the following I can see the array, but cannot get the data.
fw_print($page);
Thanks guys
You're receiving just options array by calling fw()->theme->get_options('slug') - literally what you typed in the framework-customizations/theme/options/slug.php file.
If you want to receive the actual data you need to use DB helpers.
$data = fw_get_db_settings_option();
// or even refer to individual options, for performance's sake
$individual_option = fw_get_db_settings_option('option_id');
fw_print($data);
fw_print($individual_option);
I am trying to get data (data) from the following URL.
http://www.example.dk/page/?data=1
I use the following code for getting the data to the wordpress page:
<?php $_GET['data']; ?>
But I don't get the data to the page. Isn't it the normal way to get data from URL with php?
As Matt pointed out, you'll need to echo the the result. Like:
<?php echo $_GET['data']; ?>
If your server is setup to use PHP shorthand statements you might also be able to use
<?= $_GET['data'] ?>
I am trying to develop a custom plugin in word-press. I have created a form which calls the following script:
<?php
global $wpdb;
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
echo $sql;
//$wpdb->show_errors();
$wpdb->insert("wp_wbp_unpublished",array('uid'=>null,'pid'=>$_POST['bibkey'],
'author'=>$_POST['author'],'title'=>$_POST['title'],'year'=>$_POST['date'],
'note'=>$_POST['note'],'keyword'=> $_POST['keywords'],'abstract'=>"null"));
$wpdb->query($sql);
//$wpdb->print_error();
?>
As you can see, I have tried two different ways to insert data into my db but nothing worked.
The sql string is correct, I have checked it. But something wrong with the insert()/ query() commands. Any suggestions of what is wrong?
Honestly, I do not see anything wrong with your code. At the end of the day I always blame Wordpress for the fact that it sucks asshoe. I am having the same issue, except only after the user logs into the index file and tries to make changes to db. Why hasn't anyone answered this question? Wtf
You have to remove semicolon(); in your custom or first insert query
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
And other thing is when you make plugin, then you have to put "upgrade.php" file in you function...
I hope it will work.
I have used the following solution, which seems to be the best:
$wpdb->insert( $table, $data, $format );
For instance:
$wpdb->insert(
$prefix.'wp_wbp_unpublished',
array('bibkey'=>$_POST['bibkey'],'author'=>$author),
array('%s','%s')
);
More information: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row
I am not sure if this is an issue with my current setup, or what I want to do.
I have a module that is programatically creating nodes in my Drupal 6 site, and within each I have to provide links in between various nodes.
I basically have a few foreach loops, and within each I have the current path.
For instance:
foreach ($page->category as $category) {
$category_link = "category/" . $category['id'];
// generate category pages
...
$content = "<a href='$category_link'>".$category['name']."</a>";
_create_node($content);
foreach ($category->article as $article) {
$article_link = $category_link . "/article/" . $article['id'];
// generate article page
$content = "<a href='$category_link'>".$category['name']."</a>";
$content .= "<a href='$article_link'>".$article['name']."</a>";
_create_node($content);
}
}
The issue that I'm seeing is that the link seems to be continually built up.
For instance, in the main category pages it is fine (I'll see category/1234), and the article link will be fine, but the category link will seem to be longer than it should. Basically, I'll end up seeing:
category/1234/article/5678/category/1234
My first thought was to make use of $base_url and just create absolute paths, however whenever I try printing that variable from my module it is completely empty. This is on a local server, however when I move it to production Drupal isn't installed at the root, so I can't simply add a slash to the front of the link.
Try using $GLOBALS['base_path'] to get the base path.
$GLOBALS['base_path'] will work, but you are accessing a global variable that ALSO contains some things like your database connection info and some other important stuff. So with a slip of the finger you could muck up other things. I prefer base_path() which does the same thing but is a modicum safer.
Use
global $base_url;
For path to themes folder use
path_to_theme()
You can use base_path() but that will not provide you with the domain name.
Base url will provide you the complete url like : www.example.com
base_path() will give you : /
path_to_theme() will give you : sites/all/themes/yourthemename
I have these two file in my project that I am migrating from php. I have developed most of it by seeing the functionality but there are these two files which I don't know about. If somebody could have a look and help me converting these, I would really be thankful.
Menu.ctp
<?php
Configure::write('debug', 0);
echo($_GET['callback'].'(');
echo ($javascript->object($profiles));
echo(');');
?>
Retrieve.ctp
<?php
Configure::write('debug', 0);
echo($_GET['callback'].'(');
echo ($javascript->object($profile));
echo(');');
?>
Configure::write('debug', 0);
This tell application to put debug mode to off. If you in debug mode, any application error message will be displayed which is not appropiate for production or ajax call.
echo($_GET['callback'].'(');
$_GET is a global variable which hold all GET request parameter. For example, request to index.php?callback=some_callback will set $_GET['callback'] to some_callback.
echo ($javascript->object($profiles));
This is javascript helper in CakePHP which turn PHP array($profiles) into JSON.
echo($_GET['callback'].'(');
echo ($javascript->object($profile));
echo(');');
This will otuput : callback_parameter(JSON form of profiles);