Archive for the ‘mysql’ Category
-
Very simple NodeJS MySQL select query example
This basic example shows how to run a query in MySQL when using node-mysql.
Ensure you have installed node-mysql using npm else you will need to edit the mysql require line.
Copy the entire code & paste to testmysql.js then run — node testmysql.js — Test by browsing to http://127.0.0.1:3000
var sys = require('sys'); http = require('http'); var Client = require('mysql').Client, client = new Client(); client.port = '/var/run/mysqld/mysqld.sock'; // change this to a hostname if you don't want to use sockets client.user = 'root'; // change this client.password = 'password'; // change this client.connect(); // use the correct database client.query('USE test'); // change this http.createServer(function(req, res){ res.writeHead(200, {'Content-Type': 'text/plain'}); // Basic Select query client.query('SELECT * FROM test', // change this function selectCb(err, results, fields) { if (err) { throw err; } // console.log(results); // console.log(fields); // console.log(JSON.stringify(results)); // left these in so you can do some console debugging //For each item do something with the result for (var i in results){ var result = results[i]; res.write(result.test+":"); // Writes to the web browser the value of test then a : to seperate values } res.end(); // end the request. } ); }).listen(3000,"127.0.0.1"); sys.puts("Server running on port 3000");Thanks to Tom Hudson for the help!
-
PHP to create Elgg pages from a mysql database
Installation:- Save to below script to the root of your Elgg install as importelgg.php
- Change the bits in bold to suit your environment.
- Log into your elgg site as the user you want to upload as then browse to the script.
Code:<?php// Modified from edit.php from Elgg doc’s by John McLear – www.mclear.co.ukset_time_limit(0); // We dont want the script to time outrequire_once ‘engine/start.php’;// Load configurationglobal $CONFIG;$dbhost = ‘localhost‘; // The MySql Hostname$dbuser = ‘root‘; // MySQL Username$dbpass = ‘yourpassword‘; // MySQL Password$conn = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die (‘Error connecting to mysql’); // TRUE so we dont overwrite connection$dbname2 = ‘mydatabase‘; // The databasenamemysql_select_db($dbname2);$sql=”SELECT title,contents FROM pages“; // Get the content from the source database$result=mysql_query($sql);// Go through the records and create what you need from thatwhile ($row=mysql_fetch_array($result)) {$title=$row["title"]; //Set the title value from the mysql title field$content=$row["contents"]; // Same as above but for contentsgatekeeper();set_context(‘pages’);// Get group fields$input = array();foreach($CONFIG->pages as $shortname => $valuetype) {$input[$shortname] = get_input($shortname);if ($valuetype == ‘tags’)$input[$shortname] = string_to_tag_array($input[$shortname]);}// Get parent$parent_guid = (int)get_input(‘parent_guid’, 0);// New or old?$page = NULL;$pages_guid = (int)get_input(‘pages_guid’);if ($pages_guid){$page = get_entity($pages_guid);if (!$page->canEdit())$page = NULL; // if we can’t edit it, go no further.}else{$page = new ElggObject();if (!$parent_guid)$page->subtype = ‘page_top’;else$page->subtype = ‘page’;// New instance, so set container_guid$container_guid = get_input(‘container_guid’,’8849‘); // Set the container GUID to a static user$page->container_guid = $container_guid;}// Have we got it? Can we edit it?if ($page instanceof ElggObject){// Save fields – note we always save latest description as both description and annotationif (sizeof($input) > 0){foreach($input as $shortname => $value) {if ((!$pages_guid) || (($pages_guid) && ($shortname != ‘title’)))$page->$shortname = $value;}}// Validate create$page->title = $title;if (!$page->title){register_error(elgg_echo(“pages:notitle”));forward($_SERVER['HTTP_REFERER']);exit;}// Access ids (Make it public)$page->access_id = 2;// Get the content$page->description = $content;// Write access id (Make it public)$page->write_access_id = 2;// Set parent$page->parent_guid = $parent_guid;// Ensure ultimate owner$page->owner_guid = ($page->owner_guid ? $page->owner_guid : $_SESSION['user']->guid);// finally saveif ($page->save()){// Now save description as an annotation$page->annotate(‘page’, $page->description, $page->access_id);system_message(elgg_echo(“pages:saved”));//add to riveradd_to_river(‘river/object/page’,'create’,$_SESSION['user']->guid,$page->guid);echo “$title – DONE<br/>”;}elseregister_error(elgg_echo(‘pages:notsaved’));}else{register_error(elgg_echo(“pages:noaccess”));}usleep(250000); // Just a little rest not to be overspammy}?>
Latest Videos
- Why I am not a fan of phonics February 9, 2012 malbell
- Using Cashback Sites February 8, 2012 Ian Addison
- Yaca Paca! February 8, 2012 Tim
- Safer Internet Week February 8, 2012 Mr. Mayoh
- Perhaps it is about time you joined the K Team! February 8, 2012 chickensaltash
- Moonshot Thinking: Solve For X February 8, 2012 malbell
- Picture Book Maker February 8, 2012 simon




