Solved

php max record size

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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

696 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