I am very new to WordPress, I have very little knowledge with PHP.
I read about PODS and I know how to create one and use pages / templates to display data.
The issue I am havingis, the PODS I was creating use static data entered via the WP dashboard, what I want is to read data from a database, I am using MySql (same DB that wordpress is using). is there a way to use PODS and read the data from the DB, or wordpress has a better way to handle data coming from the DB ?
Thanks
You should Look into the $wpdb variable (and class)
http://codex.wordpress.org/Class_Reference/wpdb
Do remember to declare it a global:
<?php global $wpdb; ?>
I am however not sure what you want.
I advise staying close to wordpress.
If you want to create your own custom post types without using code use moretypes
Usual way to read from database in WordPress is the following:
get global variable $wpdb
global $wpdb
prepare the output and SQL command
$output = "";
$sql = "SELECT ".$wpdb->prefix."posts.post_title,
".$wpdb->prefix."posts.post_name FROM ".
$wpdb->prefix."posts WHERE ".$wpdb->prefix.
"posts.post_status='publish' AND ".$wpdb->prefix.
"posts.post_parent=0 AND ".$wpdb->prefix.
"posts.post_type='sometype'";
method get_results() retrieves values from db
$posts = $wpdb->get_results($sql);
$output .= '';
foreach ($posts as $post) {
$output .= 'post_name).
'">'.strip_tags($post->post_title).'';
}
$output .= '';
echo $output;
Wordpress CSM has a very good class to work with db, i think the better bet on this is learn how db connects and get data from mysql
Related
I have created a section in my functions.php file and registered a shortcode which is intended to echo some data from a custom mysql database table.
I have an external system which will insert data into the database.
My wordpress install should be able to read from this database, and I would like to echo some rows on a wordpress page via shortcode.
The problem I am having is that the table data is not being echoed out. I can do echo "test"; and the word test will get displayed but the problem I having is not showing table data. Heres my code.
function vstp_feed_function(){
//$x++;
//$alternatingclass = ($x%2 == 0)? 'whiteBackground': 'graybackground';
$mydb = new wpdb('root','cencored','testdb','localhost');
$rows = $mydb->get_results("select * from services");
echo "<ul>";
foreach ($rows as $obj) :
echo "<li>".$obj->headcode."</li>";
endforeach;
echo "</ul>";
}
add_shortcode('vstp_feed', 'vstp_feed_function');
So the above code will create a new variable mydb, with the database credentials and information. A new variable called $rows will store the sql query which uses the mydb variable. I get all columns from services table. Thereafter I am echoing an unordered list, running a for loop to display the contents of the column "headcode" in list item html.
Once the select query is done, the for loop ends and so does the html list.
The shortcode is registered as [vstp_feed] which is tested and working with static html.
Any help would be greatly appreciated. Worked with sql before but not with new wordpress db instances. Thanks.
Just to confirm, its apache ubuntu. Its also worth noting that my ##datadir is stored on a different disk seperate from my OS disk, for faster IO and freedom to expand storage on the fly
Place your connection code inside wp-config.php file ( $mydb = new wpdb('root','cencored','testdb','localhost'); )
And use it in your function file as "global $mydb;"
I'm trying to get a result from a MySQL database and display selected values into a .tpl template file.
This is what I tried so far:
{php}
$clienthosting = $this->get_template_vars(service);//Here is where the exception is thrown
$dbid = $clienthosting['id'];
$query = mysql_query("SELECT dedicatedip FROM tblhosting WHERE id = $dbid");
$result = mysql_fetch_array($query);
$dedicatedip = $result["dedicatedip"];
$this->assign("dedicatedip", $dedicatedip);
{/php}
But it generated the following error:
Something went wrong and we couldn't process your request.
What am I doing wrong?
Thanks.
WHMCS recommends not use {php} inside tpl files, you can use hooks instead to add variables and use them in the TPL file.
But you can enable it in the settings: Setup > General Settings > Security > Allow Smarty PHP Tags.
Also, if you're running PHP 7 the mysql extension is removed, you can't use functions like mysql_query and mysql_fetch_array.
You should use Capsule and Eloquent as recommended at Interacting with the Database page
I am trying to connect to a MySQL database.
I found this script and I am trying to use it on my PC and web host, but it doesn't show any output.
Please have a look at this code. I am running perl at xampp.
#!C:\xampp\perl\bin\perl.exe
print "Content-type: text/html\n\n";
use DBI;
use strict;
my $driver = "localhost";
my $database = "data";
my $dsn = "DBI:$driver:database = $database";
my $userid = "root";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password ) or die $DBI::errstr;
I am using the same database with PHP.
but it doesn't show any output.
Judging by the CGI header you're displaying, I assume that you're running this as a CGI program. In which case, it's no surprised that you're not seeing any output - you're not sending any output.
If you ran it as a command line program (and it's often a good idea to get stuff working on the command line before leaping into CGI programming), then you would see the "Content-Type" header. Alternatively, you could add some output to your program and see if that appears in your browser. Something simple like:
print 'It works!';
I'd also like to add, that CGI looks rather outdated these days and there are far better (by which I mean easier and more powerful) ways to write web applications with Perl. You might like to read CGI::Alternatives to get an idea of what is available.
Update:
I've just seen this question asked on Facebook (please don't cross-post without telling people) and I've noticed that your $driver variable is wrong. If you're connecting to MySQL, then $driver should be "mysql" (so that DBI loads "DBD::mysql").
A DSN for the MySQL driver looks like this
DBI:mysql:database=$database;host=$hostname;port=$port
where the host and port fields are optional. (The database is also optional, but you don't want to leave that out.) There are several more esoteric options too, but they're irrelevant here
But you're supplying
DBI:localhost:database = data
Which doesn't even specify a MySQL connection, so I'm not surprised if it doesn't work! I don't know whether the spaces are legal, but I would leave them out to keep in line with the documentation.
You should change that statement to
my $dsn = "DBI:mysql:database=$database;host=$driver"
You may remove ;host=$driver if you wish (why have you called the host name "driver"?) as localhost is the default. A DSN that specifies just a database name and uses the default for all the other fields may be contracted to just
my $dsn = "DBI:mysql:$database"
It may be easier to just write print statements at first to generate some output. You will want to print a MIME content type header of text/plain instead of text/html. Try print "$DBI::errstr\n" for now instead of die, as the latter writes to stderr which won't appear in your browser
you could add:
if ($dbh) {
print 'connect ok';
} else {
print 'connect failed';
}
I want take user table data in a variable using CakePHP Auth.
This is my code:
$result = $this->Auth->User(['name']['email']);
pr($result);
exit;
How I will take data in variable?
Explore all elements first:
$result = $this->Auth->User();
pr($result);
exit;
Now you can collect which data you need.
Otherwise ,you can read it from session.
Do like this
$userDetails = $this->Session->read('Auth.User');
debug($userDetails);
P/S: Always state your CakePHP version for better answer received.
I have integrated PHPbb to my website and I am using the phpbb connection to read data from an additional table I created on the database. below is the read code to query the databse:
$sql = 'SELECT tetsin, prod_desc_short, prod_price_old, prod_price_new, prod_img
FROM ' . tet_product;
$result = $db->sql_query($sql);
$data = array();
while ($row = $db->sql_fetchrow($result))
{
$data[] = $row;
}
What I am having trouble with is finding the function to add records to the databse, can someone direct me on what technology I may be using and unaware? I saw something about the Data Absraction layer and was a little more than sketchy on if there are some "variants" on functions or something like that, anyway I know the quer function above is "$db->sql_query" so any help in the insert query would be greatly appriciated. thanks!
See the official wiki, it has guides to most things you will need https://wiki.phpbb.com/Queries_in_phpBB3