sysadmin:kb:php_scripts

PHP Scripts

<?php
 
    $user = 'username'; //your username
    $pass = 'password'; //your password
    $host = 'localhost'; //localhost 99% of the time
 
    //You shouldn't need to touch anything down here.
 
    $conn = @mysql_connect($host,$user,$pass) //mysql info
    or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>"); //gives error neater than error_reporting does
 
    $rs1= mysql_list_dbs($conn);
    for($row=0; $row < mysql_num_rows($rs1); $row++)
    {
        $this_db = mysql_tablename($rs1, $row);
        $list .= "<strong>".$this_db."</strong><br />";
        if($this_db != "mysql") //we don't want to list tables for this db for security reasons
        {
            $rs2= mysql_list_tables($this_db);
            for($num=0; $num < mysql_num_rows($rs2); $num++)
            { $list .= mysql_tablename($rs2,$num).",<br />"; } //remove the comma from here if you wish
        }
    }
    echo($list);
?>
<?php
 
    ///////////////////////////////////////////
    //                                       //
    //             Edit these                //
    //                                       //
    ///////////////////////////////////////////
 
    $user = 'username'; //your mysql username
    $pass = 'password'; //your mysql password
    $host = 'localhost'; //localhost 99% of the time
    $database = 'database'; //your database
 
    ///////////////////////////////////////////
    //                                       //
    //         I would advise against        //
    //      touching anything down there     //
    //                                       //
    ///////////////////////////////////////////
 
    //Before we do anything, make sure we can connect to the  mysql server.
    $conn= @mysql_connect($host,$user,$pass)
    or die("<strong>Fail!</strong><br /><em>".mysql_error()."</em>"); //throws an error if something went wrong
 
    if($_POST['submit']){
        $query = $_POST['query']; //naming the query
 
        mysql_select_db($database) or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>");
        mysql_query($query,$conn) or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>");
 
        echo("Success! <a href='javascript:javascript:history.go(-1)' />Go back?</a>");
 
    }
 
    else{
        ?>
 
<html>
<form name="queryeditor" method="post">
<strong>Enter in your query:</strong><br />
<textarea name="query" rows="10" cols="45"></textarea><br />
<input type="submit" name="submit" value="Allons-y!" />
</form>
</html>
<?
};
 
?>

Disclaimer: These scripts are for demonstration purposes only. They are not suitable to use in a production environment.

<title>Word Count Script</title>
<?
if($_POST['submit']){ //gets data from form
    $text = $_POST['text']; //names text
    $result = "<strong>Your text:</strong><br />\"".nl2br($text)."\"<br /><br />"; //tells you what you told us
    $result.= "<strong>Contains:</strong><br />";
    $result.= strlen($text)." characters<br />"; //counts characters
    $result.= str_word_count($text)." words."; //counts words
    $result.= "<br /><br />"; //breaks before the form again
    echo( $result );
    $form = "<form name=\"wordcount\" method=\"post\">
    <textarea name=\"text\" rows=\"10\" cols=\"50\">".$text."</textarea>
    <input type=\"submit\" name=\"submit\" value=\"Count!\" />
    </form>";
    echo( $form ); //gives you a new form *with* your text input
}else{ ?>
    <form name="wordcount" method="post">
    <strong>What do you want to count?</strong><br />
    <textarea name="text" rows="10" cols="50"></textarea><br />
    <input type="submit" name="submit" value="Count!" />
    </form>
<?   }; ?>
  • sysadmin/kb/php_scripts.txt
  • Last modified: 2019/10/01 14:44
  • by mjorgensen