Adding a record to mysql reusing a phpbb connection - mysql

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

Related

Using wpdb to display non-wordpress database data not working

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;"

get sql query from collection model

I know that we can get sql query from magento model collection using this,
getSelect();
But I can get query from only the model collections, not worked in others or May be I dont know how to use it.
Here I want to know what query is running behind this,
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("28");
}
If I use this,
echo $productModel->getSelect(); exit;
I'm just get the one part the query only, like,
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Update:
This is my full code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
$color_label = $attr->getSource()->getOptionText("28");
}
$productModel->printlogquery(true);exit;
Please help me guys,
Your order condition is not visible in the query. The reason your order isn't showing is because the orders are added to the query during the load() method.
See Varien_Data_Collection_Db::load()
Try calling load(true) to see the complete SQL containing the order by clause.
$productModel->load(true);
$productModel->printLogQuery(true);
Hope it helps.
If you want to see what is the exact query, then you can get this by using:
$productModel->printlogquery(true);exit;
Use this code after you have loaded your model's object and applied all conditions.
I hope this will help you.
Magento collecting data with lot of internal queries - models and lot of checks, and may be more than 1 table. So it is not possible to get the query like what I'm looking for.

Issue With a Mysql Query Displaying A columns Data

I am trying to run this query from a drop down menu, the category and the destination data are working fine, just not the subcategory, it's probably an issue with my Html or Javasscript but i wanted to get the opinion of an expert with MYSQL if poss? I am quite new to PHP and MYSQL, but learning fast, Not sure my HTML will post here so i will just ask if someone can confirm that my query should work ok. Thanks guys.
$category=$_POST['Category'];
$subcategory=$_POST['Subcategory'];
$destination=$_POST['Destination'];
$result = mysql_query("SELECT * FROM travel WHERE Category='$category' AND Subcategory='$subcategory' AND Destination='$destination'")
or die(mysql_error());
$row = mysql_fetch_array( $result ) ;
Not sure if it's a culprit in your case, but you have to escape data coming from a user before passing it to the query :
$category=mysql_real_escape_string($_POST['Category']);
$subcategory=mysql_real_escape_string($_POST['Subcategory']);
$destination=mysql_real_escape_string($_POST['Destination']);
....

Database input sanitation

Sorry if this question has somewhat been answered, but everywhere I look answers seems to change..
I'm really not 100% sure of how to deal with data into and from a db.
For instance: A UTF8 mysql database and a form that takes user input.
When dealing with the post - should I htmlspecialchars('data',ENT_QUOTES) to the data before it's saved to the database or do I just save it raw.. the data will most likely contain umlouts and the like of special chars. so if no sanitization is done I assume < and > and all types of quotes would be stored exactly as they are.
Also do I need to addslashes or mysql_real_escape_string before saving.
OR should all this stuff be done on the output and just use filtering for input, I use zend so I have filters added to most elements.
Not sure its relevant magic_quotes but are off.
EDIT 2:
I believe my queries use prepared statements ->where('fqha.form_question_has_answer_form_id = ?', $result[$input->formpage]['form_id'])
So does this mean I can ignore any further manipulation to data and just save what the post vars contain. Only worrying about sanitizing for output to the page?
EDIT:
I'm using doctrine 1.2 and from what I can tell this is pdo.. but regarding this and the above question any advice would be greatly appreciated
protected function _initDoctrine()
{
require_once 'Doctrine/Doctrine.php';
require_once 'Doctrine/Overloadable.php';
require_once 'Doctrine/Connection/Profiler.php';
$this->getApplication()
->getAutoloader()
->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$cacheDriver = new Doctrine_Cache_Apc();
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection('mysql://user:pass#localhost/db_name', 'doctrine');
$conn->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$conn->setCharset('utf8');
$conn->exec('SET NAMES utf8');
$profiler = new Imind_Profiler_Doctrine_Firebug();
$conn->setListener($profiler);
return $conn;
}

reading from a database using WordPress

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