chocopi Posted June 12, 2007 Share Posted June 12, 2007 how do you get the largest value from database ? Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/ Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 $fetch=mysql_fetch_assoc(mysql_query("SELECT data FROM table ORDER BY data DESC LIMIT 1")); That'll work but I believe there is actually a MySQL function for finding the highest number but I'm not completely sure. Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273051 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 well i have got this but it does not work $query = mysql_query("SELECT `post_num` FROM `zBoard_messages` WHERE board_id='$board' ORDER BY `post_num` DESC LIMIT 1") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $psot_num = $fetch['post_num']; $post_num++; it just inputs 1 into the database Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273054 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 How can it input 1 into the database? You're using a SELECT query which can only PULL data from the database! Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273055 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 i do then insert $post_num into the database, but accordingly to the code it is 1 when it should be 6 or 7 Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273057 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 Do you want to find the last entry in the table? btw, in answer to your maximum value question, I just found it: SELECT MAX(field) FROM table Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273059 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 cheers but it is still producing one this is my whole code <?php // Start Session session_start(); if(!isset($_SESSION['username']) && !isset($_SESSION['id']) && !isset($_SESSION['password'])) { header("Location: login.php"); } else { require_once('page_header.php'); $id = $_SESSION['id']; $username = $_SESSION['username']; if($_POST) { $poster = $id; $board = '1'; // get actual message $original = $_POST['message']; $text = $original; require_once('bbcode.php'); $date = date('YmdHia'); $query = mysql_query("SELECT MAX(post_num) FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch= mysql_fetch_assoc($query) or die(mysql_error()); $psot_num = $fetch['post_num']; $post_num++; $query = "INSERT INTO `zBoard_messages` (poster_id,board_id,post_num,subject,message,date) VALUES ('$poster','$board','$post_num','$subject','$original','$date')"; mysql_query($query) or die(mysql_error()); header("Location: thanks.php"); } else { ?> <html> <body> <form name="name" method="post" action="<?php $PHP_SELF ?>"> <center> <br /> <input type="text" name="subject" id="subject" value="<?php $subject ?>" /> <br /> <br /> <textarea name="message" value="<?php $original ?>" cols="60" rows="15"></textarea> <br /> <br /> <input type="submit" name="submit" id="submit" value="Submit" /> </form> </center> </body> </html> <?php } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273063 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 Seems you have a typo: $psot_num = $fetch['post_num']; $post_num++; You're using $psot_num instead of $post_num! $post_num will always be 0 because you're not giving it a value due to your typo which is why it will always increase to 1. Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273065 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 Also!!! Change your query to this: SELECT MAX(post_num) AS `postnum` FROM `zBoard_messages` WHERE board_id='$board' and this line: $post_num = $fetch['postnum']; Otherwise you'd have to use this: $post_num = $fetch[0]; Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273066 Share on other sites More sharing options...
chocopi Posted June 12, 2007 Author Share Posted June 12, 2007 CHEERS thats sorted it for me ;D ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273072 Share on other sites More sharing options...
Yesideez Posted June 12, 2007 Share Posted June 12, 2007 You're most welcome Quote Link to comment https://forums.phpfreaks.com/topic/55245-solved-mysql-max/#findComment-273073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.