HomeSunday, June 24, 2018, 01:44:38
1

Random MySQL Query

Tuesday, February 10, 15, 00:45:40, 3 Yaers Ago Via wain, Hits: 6320
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

Link Base 1.1. - Free PHP Link Index Script

Manage your links using a MySQL database. Features easy install, user submission, and an admin panel to add, edit and remove categories and links.

ShoutBox 1.1. PHP Script

ShoutBox allows your users to post short messages on your site. The script is lightweight and very easy to install and use.

PHP filesize

The filesize function returns the size of the specified file.

PHP Fwrite : fwrite

php write to file - how to write data to a file using the php frwite function

Upload Image with PHP [Example2]

[Example2] Tutorial - How to upload and resize image with PHP

Tell A Friend with Captcha

This form allows your visitors to suggest your site to a friend. It features an HTML email format and an admin notification email.

Adodb Recordcount Example

RecordCount returns the number of rows in the record set.

Inserting form data in MySql table

Inserting form data in MySql table with PHP

RSS, MySQL & Smarty

Smarty > Publishing MySQL Data in RSS 2.0

Comments 1.1. :: PHP Script

This script allows your visitors to add comments to any single page on your web site.
About Author

wain

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