Ad block is installed and active. Please support us by disabling it.
HomeThursday, September 21, 2017, 03:23:32
1

Random MySQL Query

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

Preg_Match - PHP Email Validation

Validate email with preg_match in php

Advanced Contact Form - PHP Script

Advanced Contact Form with email and form field validation. Features: HTML Output(Response), Captcha Image.

PHPBB3 Shoutbox

Vesion: 1.1. Released: 08/08/10, Licence GNU, Price: Free

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.

Adsense crawler errors and robots.txt

How to fix Google AdSense crawler errors in robots.txt

Jquery - Formatting International Phone Numbers

How to format international telephone numbers with jquery

MySQL Basic Functions

MySQL Basic Functions - select, delete, update..

Getimagesize

Getimagesize function will determine the size of any given image file and return the dimensions along with the file type and a height/width text string to be used inside a normal HTML IMG tag and the correspondant HTTP content type.

Show / Hide Div with CSS

CSS Display and Visibility:The visibility property specifies whether or not an element is visible.
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.