Solved

php max record size

Posted on 2013-05-20
3
394 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …

895 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

16 Experts available now in Live!

Get 1:1 Help Now