Ad block is installed and active. Please support us by disabling it.
HomeSaturday, January 20, 2018, 16:49:36
1

Random MySQL Query

Tuesday, February 10, 15, 00:45:40, 3 Yaers Ago Via wain, Hits: 3314
Random MySQL query, requirement(s): PHP Server, MySQL
Download source code:

http://phpform.net/download.php?f=random.zip

config.php

Code:
<?php
//MySQL Configuration
//DB Host (Normally 'localhost')
$dbhost = 'localhost';
//DB Database Username
$dbusername = 'root';
//DB Database User Password
$dbpassword = 'mypassword';
//DB Database Name
$dbname = 'mydbname';
//mysql_connect function
$conn=mysql_connect($dbhost, $dbusername, $dbpassword);
if(!$conn) :
die('Could not connect: ' . mysql_error());
endif;
$db=mysql_select_db($dbname, $conn);
if(!$db) :
die ('Cant connect to database : ' . mysql_error());
endif;
?>


install.php

Code:
<?php
//make a MySQL connection
include('config.php');
//create a MySQL table in the selected database
mysql_query("CREATE TABLE `text` (
`textid` int(11) NOT NULL auto_increment,
`textvalue` text collate utf8_unicode_ci,
PRIMARY KEY (`textid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci AUTO_INCREMENT=1")
or die(mysql_error());
?>


index.php

Code:
<title>Index Page</title>
<center><h2>Index</h2></center>
<?php
include('config.php');
$query = mysql_query("SELECT MAX(textid) FROM text");
$results = mysql_fetch_array($query);
$maxresults = $results['MAX(textid)'];
if($maxresults <= '5'){
echo "<a href="insert.php">Insert Something</a><br />";
}
echo "Max results in MySQL: " . $maxresults . "<br />";
$random = rand(1,$maxresults);
echo "Displaying Result: " . $random . "<br />";
$query = "SELECT * FROM text WHERE textid ='$random'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
echo $row['textvalue'];
?>


insert.php

Code:
<title>Insert Page</title>
<center><h2>Insert Page</h2>
<a href="insert.php">Insert Something</a><br />
<a href="index.php">Index</a>
</center>
<?php
//make a MySQL connection
include('config.php');
//to check if a submit button was clicked, use this...
if(isset($_POST['submit']))
{
$textvalue = $_POST['textvalue'];
//The INSERT INTO statement is used to add new records
//to a database table.
mysql_query("INSERT INTO text (textvalue)
VALUES ('$textvalue')")
or die(mysql_error());
}else{
?>
<table><tr><td>
<form method="post" action="insert.php">
Textvalue:
</td><td>
<input name="textvalue" size="60" maxlength="255">
</td></tr>
<tr><td>
<input type="submit" name="submit" value="submit">
</form>
</td></tr></table>
<?php
}
?>

Sign In

Sponsor

Related

View content in a zip file with PHP

List all files in a zip archive with PHP

Adsense crawler errors and robots.txt

How to fix Google AdSense crawler errors in robots.txt

PHP Mail

Sends an email.

MySQL – Select from two tables

Select data from two tables, PHP MySQL

Creating a CAPTCHA with PHP :: PHP Tutorial

Captcha Image, This tutorial will show you how to create a form with Captcha.

Create dynamic Web Pages with PHP & MySQL

Create dynamic Web Pages with PHP & MySQL, Requirement(s): PHP Server, MySQL

PHP form with file upload

Advanced Contact Form with email and form field validation. It allows your visitors to send you an email directly from your website.

PHP DateTimeZone

PHP representation of time zone

HTML Button Generator

This script will allow your visitors to create HTML Buttons online. To use the "html button generator" script completely free , the "powered by" link at the bottom of the pages must stay intact.

PHP Cookies - setcookie

Defines a cookie to be sent along with the rest of the HTTP headers.
About Author

wain

Date Registered: Sun, Feb 8, 15
Last Time Online: Thu, Jun 15, 17
So far has created 115 entries.
wain
This author has not yet filled in any details.