Ad block is installed and active. Please support us by disabling it.
HomeWednesday, April 25, 2018, 08:52:27
1

Random MySQL Query

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

PHP Cookies and Sessions Tutorial

PHP Cookies and Sessions Tutorials

Password Protect With PHP

Password protect tith PHP - tutorial

Link Base Pro :: Free Link Index Script

This script allows you to manage your links using a MySQL database. It features easy install, user submission, and an admin panel to add, edit and remove categories and links.

Meta Tags Generator 1.1. PHP Script

This script will allow your visitors to create meta tags online.

Math Captcha V6

PHP Math Captcha Image Version 6

Link Generator PHP Script

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

Email to Friend - PHP Script

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

Inserting form data in MySql table

Inserting form data in MySql table with PHP

Adsense crawler errors and robots.txt

How to fix Google AdSense crawler errors in robots.txt

Preg_Match - PHP Email Validation

Validate email with preg_match in php
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.