string klasse (stmt achtig)
Dit is een klasse, die ongeveer hetzelfde werkt als de mysqli_stmt klasse.. het verschil is, dat het dus met strings werkt.. De functies: prepare - gebruik: $var->prepare("zin"); de zin die je gaat veranderen enz. (je kunt in die zin vraagtekens (?) gebruiken om een variabele aan te geven.) bind_param - gebruik: $var->bind_param([$var1, [$var2, [enz.]]]); deze variabele vervangen de vraagtekens, zijn er geen vraagtekens, gebruik deze functie dan zonder variabele (zorg dat er evenveel variabele in staan als dat er vraagtekens in de prepare zin staan!) param_count - gebruik: $var->param_count(); geeft het aantal vraagtekens in de prepare zin terug execute - gebruik: $var->execute(); geeft de uiteindelijke zin terug free_result - gebruik: $var->free_result(); maakt de resultaten helemaal leeg close - gebruik: $var->close(); uitgebreidere versie van free_result, zet alles terug naar de beginwaarden (wist dus ook de error's) De variabel die van buitenaf aanspreekbaar zijn: error - gebruik: $var->error; geeft het errorbericht terug ("No error's yet!" als er geen error's zijn) errno - gebruik: $var->errno; geeft het errornummer terug * 0 - geen error * 1000 - als prepare al gebruikt is * 1001 - voor een lege zin bij de prepare functie * 1002 - als je dingen met een niet gepreparede string wilt doen * 1003 - als de variabele niet gezet zijn * 1004 - te weinig argumenten in bind_param * 1005 - te veel argumenten in bind_param de string die opgegeven word in prepare wordt volledig gescaped zodat het geen schade aan je script kan geven, dit wordt ook gedaan met de variabele uit bind_param ik hoop dat het wat is, gr. leroy
stringClass.php
[code]<?php
/*----------------------------------------------
-------------------------------------------------
DONT REMOVE THIS BLOCK!!!
--------------------------------------------------------
Copyright
Created by: libia
Created on: 15-09-07 (DD-MM-YY)
--------------------------------------------------------
You can use this script,
but you may NOT call it YOUR script!
--------------------------------------------------------
-----------------------------------------------------*/
//the class
class String
{
//some variables
public $error;
public $errno;
protected $string;
protected $finalString;
protected $aantArgs;
protected $bindArgs;
protected $prepared;
protected $bonden;
protected $paramAant;
//setting values for the variables
function __construct()
{
$this->error = "No error's yet";
$this->errno = 0;
$this->string = '';
$this->finalString = '';
$this->aantArgs = 0;
$this->bindArgs = '';
$this->prepared = false;
$this->bonden = false;
$this->paramAant = 0;
}
//setting the first sentance
function prepare($prepareString)
{
if($this->errno == 0)
{
if(!$this->prepared)
{
if(!empty($prepareString))
{
$this->string = htmlspecialchars($prepareString, ENT_QUOTES);
$this->prepared = true;
}
else
{
$this->error = "String is empty!";
$this->errno = 1001;
}
}
else
{
$this->error = "String is already prepared!";
$this->errno = 1000;
}
}
}
//add the user values
function bind_param()
{
if($this->errno == 0)
{
if($this->prepared)
{
$this->bindArgs = func_get_args();
$this->aantArgs = count($this->bindArgs);
if($this->param_count() == $this->aantArgs)
{
$this->finalString = $this->string;
for($i = 0; $i < $this->aantArgs; $i++)
{
$this->finalString = preg_replace("/\?/", htmlspecialchars($this->bindArgs[$i], ENT_QUOTES), $this->finalString, 1);
}
$this->bonden = true;
}
elseif($this->param_count() > $this->string)
{
$this->error = "Not enough arguments given in the bind_param() function (" . $this->aantArgs . " of " . $this->param_count() . ")";
$this->errno = 1004;
}
else
{
$this->error = "To much arguments given in the bind_param() function (" . $this->aantArgs . " of " . $this->param_count() . ")";
$this->errno = 1005;
}
}
else
{
$this->error = "String not prepared yet!";
$this->errno = 1002;
}
}
}
//return the string
function execute()
{
if($this->errno == 0)
{
if($this->prepared && $this->bonden)
{
return $this->finalString;
}
elseif(!$this->prepared)
{
$this->error = "String not prepared yet!";
$this->errno = 1002;
}
else
{
$this->error = "Params are not bonden yet!";
$this->errno = 1003;
}
}
}
//gives the amount of question marks (?) is the sentence
function param_count()
{
if($this->errno == 0)
{
$this->paramAant = 0;
if($this->prepared)
{
for($i = 0; $i < strlen($this->string); $i++)
{
if($this->string[$i] == '?')
{
$this->paramAant += 1;
}
}
return $this->paramAant;
}
else
{
$this->error = "String not prepared yet!";
$this->errno = 1002;
}
}
}
//clears the results
function free_result()
{
$this->finalString = '';
$this->aantArgs = 0;
$this->bindArgs = '';
$this->string = '';
}
//sets everything to it's standard value
function close()
{
$this->__construct();
}
}
?>[/code]
voorbeeld.php
[code]<?php
/*----------------------------------------------
-------------------------------------------------
DONT REMOVE THIS BLOCK!!!
-------------------------------------------------
Copyright
Created by: libia
Created on: 15-09-07 (DD-MM-YY)
-------------------------------------------------
You can use this script,
but you may not call it your script!
-------------------------------------------------
----------------------------------------------*/
error_reporting(E_ALL);
//include the class!
include("stringClass.php");
//and a little example
$string = new String();
$string->prepare("The ? was ? over the ?");
if($string->errno == 0)
{
echo 'There were ' . $string->param_count() . ' params setten<br />';
if($string->errno == 0)
{
$string->bind_param("dog", "jumping", "airplane");
if($string->errno == 0)
{
echo $string->execute() . '<br />';
if($string->errno == 0)
{
$string->bind_param("Wolf", "rolling", "lava");
if($string->errno == 0)
{
echo $string->execute();
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
$string2 = new String();
$string2->prepare("Lol loL ?");
if(trim($string2->errno) == 0)
{
$string2->bind_param($string->execute());
if($string2->errno == 0)
{
echo '<br /><br />' . $string2->execute() . '<br />';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
}
else
echo '<b>(error: line ' . __line__ . ') ' . $string->errno . ': ' . $string->error . '</b>';
$string2->close();
//you may use close or free_result + close, it's just what you prefer!
$string->free_result();
$string->close();
?>[/code]
Reacties
0