Difference between revisions of "Php"

From Wiki2
Line 24: Line 24:
</syntaxhighlight>  
</syntaxhighlight>  


====howto -  MYSQL -> JSON====
====howto -  MYSQL -> JSON PDO====
<syntaxhighlight>
<syntaxhighlight>
$sth = mysql_query("SELECT ...");
$sth = mysql_query("SELECT ...");
Line 64: Line 64:
echo '{"error":{"text":'. $e->getMessage() .'}}';  
echo '{"error":{"text":'. $e->getMessage() .'}}';  
}
}
</syntaxhighlight>
====howto - PDO insert====
</syntaxhighlight>
<?php
// configuration
$dbtype = "sqlite";
$dbhost = "localhost";
$dbname = "test";
$dbuser = "root";
$dbpass = "admin";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
$title = 'PHP Security';
$author = 'Jack Hijack';
// query
$sql = "INSERT INTO books (title,author) VALUES (:title,:author)";
$q = $conn->prepare($sql);
$q->execute(array(':author'=>$author,
                  ':title'=>$title));
?>
</syntaxhighlight>
</syntaxhighlight>



Revision as of 19:44, 18 January 2013

bits of code I always forget

howto - store data in object instead of array

howto - change an object to an array

<syntaxhighlight> $thefile=file_get_contents("php://input"); echo $thefile; $json = $thefile; $return_obj->setJson($json); //the true parameter forces it to decode to array instead of object //$thearr = (array) json_decode($thefile) only converts the outer object $thearr = json_decode($thefile,true); </syntaxhighlight>

howto - read command line arguments

<syntaxhighlight> <?php echo count($argv); if (count($argv)<2){ //if no command line argument echo("script requires command line argument with name of db to backup"); exit(); } $db = $argv[1]; ?> </syntaxhighlight>

howto - MYSQL -> JSON PDO

<syntaxhighlight> $sth = mysql_query("SELECT ..."); $rows = array(); while($r = mysql_fetch_assoc($sth)) {

   $rows[] = $r;

} print json_encode($rows); </syntaxhighlight> or better from rwky <syntaxhighlight> <?php $pdo=new PDO("mysql:dbname=database;host=127.0.0.1","user","password"); $statement=$pdo->prepare("SELECT * FROM table"); $statement->execute(); $results=$statement->fetchAll(PDO::FETCH_ASSOC); $json=json_encode($results); ?> </syntaxhighlight> as in http://stuff2get.sitebuilt.net/services/getfoodlist.php?repo=tpn&list=groceries&need=1 which gets you:

{"items":[{"stuff":"Blueberries imported or domestic org.","id":"430"},{"stuff":"Rasberries","id":"992"},{"stuff":"Strawberries, org","id":"420"},{"stuff":"Teccino herbal. Coffee, original","id":"876"},{"stuff":"Unsweetened almond milk","id":"110"}]}

<syntaxhighlight> $repo = $_GET['repo']; $list = $_GET['list']; $need = $_GET['need']; ChromePhp::log("stuff="); $sql = "SELECT stuff, id FROM `lists` WHERE repo=? AND list=? AND need=? ORDER BY stuff"; try { $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $dbh->prepare($sql); $stmt->execute(array($repo, $list, $need)); $needlist = $stmt->fetchAll(PDO::FETCH_OBJ); $dbh = null; echo '{"items":'. json_encode($needlist) .'}'; } catch(PDOException $e) { echo '{"error":{"text":'. $e->getMessage() .'}}'; } </syntaxhighlight>

howto - PDO insert

</syntaxhighlight> <?php // configuration $dbtype = "sqlite"; $dbhost = "localhost"; $dbname = "test"; $dbuser = "root"; $dbpass = "admin"; // database connection $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); // new data $title = 'PHP Security'; $author = 'Jack Hijack'; // query $sql = "INSERT INTO books (title,author) VALUES (:title,:author)"; $q = $conn->prepare($sql); $q->execute(array(':author'=>$author,

                 ':title'=>$title));

?> </syntaxhighlight>

howto - see if a character is in a string

you need the ! to test if $somechar==";" (strcmp returns 0 if true)

  • if(!strcmp($somechar, ";")) {
as seen in assess/wikiquiz/wikivo2db.php

to get from text area w/o losing linebreaks

  • $apage=nl2br(stripslashes($_POST[apage]));

to iterate through the lines in a string

  • foreach (preg_split("/(\r?\n)/", $apage) as $line)

howto - populate multi-dimensional arrays

/*multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

$firephp->log($a, 'nearrtest');

misc

multidimensional array

hosted/vocab/quiz/db2egg.php: uses multidimensional arrays

does sorting

refs

good class tutorial

get meta tag function

[http://sitebuilt.net/w/index.php?title=Comp&action=edit&section=9 authentication script