PHP Scripts

From jrgnsn.net Wiki
Jump to navigation Jump to search

These are silly PHP scripts I wrote many years ago. I'm keeping them around because I just can't bring myself to leave them behind.

MySQL PHP Scripts

List Tables

 1 <?php
 2 
 3     $user = 'username'; //your username
 4     $pass = 'password'; //your password
 5     $host = 'localhost'; //localhost 99% of the time
 6     
 7     //You shouldn't need to touch anything down here.
 8     
 9     $conn = @mysql_connect($host,$user,$pass) //mysql info
10     or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>"); //gives error neater than error_reporting does
11     
12     $rs1= mysql_list_dbs($conn);
13     for($row=0; $row < mysql_num_rows($rs1); $row++)
14     {
15         $this_db = mysql_tablename($rs1, $row);
16         $list .= "<strong>".$this_db."</strong><br />";
17         if($this_db != "mysql") //we don't want to list tables for this db for security reasons
18         {
19             $rs2= mysql_list_tables($this_db);
20             for($num=0; $num < mysql_num_rows($rs2); $num++)
21             { $list .= mysql_tablename($rs2,$num).",<br />"; } //remove the comma from here if you wish
22         }
23     }
24     echo($list);
25 ?>

Queries

 1 <?php
 2     
 3     ///////////////////////////////////////////
 4     //                                       //
 5     //             Edit these                //
 6     //                                       //
 7     ///////////////////////////////////////////
 8     
 9     $user = 'username'; //your mysql username
10     $pass = 'password'; //your mysql password
11     $host = 'localhost'; //localhost 99% of the time
12     $database = 'database'; //your database
13     
14     ///////////////////////////////////////////
15     //                                       //
16     //         I would advise against        //
17     //      touching anything down there     //
18     //                                       //
19     ///////////////////////////////////////////
20     
21     //Before we do anything, make sure we can connect to the  mysql server.
22     $conn= @mysql_connect($host,$user,$pass)
23     or die("<strong>Fail!</strong><br /><em>".mysql_error()."</em>"); //throws an error if something went wrong
24     
25     if($_POST['submit']){
26         $query = $_POST['query']; //naming the query
27         
28         mysql_select_db($database) or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>");
29         mysql_query($query,$conn) or die("<strong>FAIL!</strong> <em>".mysql_error()."</em>");
30         
31         echo("Success! <a href='javascript:javascript:history.go(-1)' />Go back?</a>");
32         
33     }
34     
35     else{
36         ?>
37  
38 <html>
39 <form name="queryeditor" method="post">
40 <strong>Enter in your query:</strong><br />
41 <textarea name="query" rows="10" cols="45"></textarea><br />
42 <input type="submit" name="submit" value="Allons-y!" />
43 </form>
44 </html>
45 <?
46 };
47 
48 ?>

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

Word Count Script

 1 <title>Word Count Script</title>
 2 <?
 3 if($_POST['submit']){ //gets data from form
 4     $text = $_POST['text']; //names text
 5     $result = "<strong>Your text:</strong><br />\"".nl2br($text)."\"<br /><br />"; //tells you what you told us
 6     $result.= "<strong>Contains:</strong><br />";
 7     $result.= strlen($text)." characters<br />"; //counts characters
 8     $result.= str_word_count($text)." words."; //counts words
 9     $result.= "<br /><br />"; //breaks before the form again
10     echo( $result );
11     $form = "<form name=\"wordcount\" method=\"post\">
12     <textarea name=\"text\" rows=\"10\" cols=\"50\">".$text."</textarea>
13     <input type=\"submit\" name=\"submit\" value=\"Count!\" />
14     </form>";
15     echo( $form ); //gives you a new form *with* your text input
16 }else{ ?>
17     <form name="wordcount" method="post">
18     <strong>What do you want to count?</strong><br />
19     <textarea name="text" rows="10" cols="50"></textarea><br />
20     <input type="submit" name="submit" value="Count!" />
21     </form>
22 <?   }; ?>