Solved

php max record size

Posted on 2013-05-20
3
386 Views
Last Modified: 2013-05-21
Could someone please give me some example code to do the following:

Select the MAX integer size from a mysql database column

Create a variable from that

echo the variable on a php page
0
Comment
Question by:doctorbill
3 Comments
 
LVL 12

Accepted Solution

by:
adrian_brooks earned 300 total points
ID: 39182273
$sql = "SELECT MAX(column_name) AS max_num FROM table_name";
$db = mysql_connect('localhost', 'username', 'password') or die("Could not connect to database server");
mysql_select_db('database_name') or die(mysql_error());

$res = mysql_query($sql);
if(!$res) {
  die('Invalid query: ' . mysql_error());
}

$row = mysql_fetch_row($result);

echo $row[0]; // prints out the max from the designated column in the SQL query

Open in new window


~AB
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39182624
You may find that this gives different results on 32-bit and 64-bit machines.  On a 32-bit machine this script will cause an overflow.
<?php // RAY_integer_overflow.php
error_reporting(E_ALL);
echo "<pre>";

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$dbcx = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $dbcx))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}

// CREATE A TABLE AND ALTER IT TO A HIGH INDEX NUMBER
$sql = "CREATE TEMPORARY TABLE noise ( id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=MEMORY";
if (!$res = mysql_query($sql)) die(mysql_error());

$sql = "ALTER TABLE noise AUTO_INCREMENT = 2147483646";
if (!$res = mysql_query($sql)) die(mysql_error());


// INSERT DATA TO ADD TO THE AUTO_INCREMENT INDEX
$kount = 0;
while ($kount < 15)
{
    $sql = "INSERT INTO noise () VALUES ()";
    if (!$res = mysql_query($sql)) die(mysql_error());
    $nid = mysql_insert_id($dbcx);
    var_dump($nid);
    $kount++;
}

Open in new window

Best regards, ~Ray
0
 

Author Closing Comment

by:doctorbill
ID: 39183888
Thanks very much - solution
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now