Jump to content

I can't get MAX(id) into variabele with + 1


Tissle

Recommended Posts

Hello,

 

I want to get the max id of the table pages. I tried a lot of things but nothing seems to work :(

 

The code i now use is :

 

$select_maxid = mysql_query("SELECT MAX(id) FROM `pages`");

$row1 = mysql_fetch_row($select_maxid, MYSQL_NUM);

$max_id = $row1[0];

echo $max_id;

 

but i gives my no result.

 

can someone please help me out with this?

Link to comment
Share on other sites

First of all thanks for the quick reply

 

When i type this:

 

$select_maxid = mysql_query("SELECT MAX(id) FROM `pages`"); or die(mysql_error());

 

$row1 = mysql_fetch_row($select_maxid, MYSQL_NUM);

 

$max_id = $row1[0];

 

echo $max_id;

 

it gives me a blank page :(

 

I've tried the query in phpmyadmin and there it works.

so any suggestions ?

Link to comment
Share on other sites

make sure your mysql is connected and logged in

 

$select_maxid = mysql_query("SELECT MAX(id) FROM `pages`"); or die(mysql_error());
if($row1 = mysql_fetch_row($select_maxid, MYSQL_NUM)){
    $max_id = $row1[0];
    echo $max_id;
}else{
    $Error = mysql_error();
    echo("Failed: $Error");
}

Link to comment
Share on other sites

Thanks i now know what's the failure.

 

im now using this code

$select_maxid = mysql_query("SELECT MAX(id) FROM `pages`");
			if($row1 = mysql_fetch_row($select_maxid, MYSQL_NUM)){
			    $max_id = $row1[0];
			    echo $max_id;
			}else{
			    $Error = mysql_error();
			    echo("Failed: $Error");
			}

 

it says: Failed: No database selected.

 

but at the top of my document i require my connection with the database so how can i solve this problem?

Link to comment
Share on other sites

But i use more php in my document wich als require database connection and they work all just fine.

I tried putting the require connection also between those php tags but with no result.

so what can i do to make that connection with the database ?

Link to comment
Share on other sites

Hehe... Theres a type in my code. Try this..

 

if ($select_maxid = mysql_query("SELECT MAX(id) AS max_id FROM pages")) {
  $row = mysql_fetch_assoc($select_maxid) {
  echo $row['max_id'];
} else {
  echo mysql_error();
}

Link to comment
Share on other sites

Okey here is my whole document :

<?php require_once '../cms/includes/connection.php'; ?>
<!-- Hier wordt de content en de titel geupdated en als dit gebeurt krijgt de variabele m de waarde van 1 -->
<?php


if(isset($_POST['submit'])){

	$content	= $_POST['content'];
	$title		= $_POST['title'];



	$sql		= "INSERT INTO pages VALUES ('', '$title', '$content', '')";	
	$sql1		= "INSERT INTO nav VALUES ('', '$title', 'index.php?p=$title', '$title')";

	$result		= $conn->query($sql) or die(mysqli_error());
	$result1	= $conn->query($sql1) or die(mysqli_error());

}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Portfolio Thijs van Emmerik</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="../style/style.css"/>
<link rel="shortcut icon" href="../images/favicon.ico"/>
<!-- TinyMCE --> 
<script type="text/javascript" src="js/tiny_mce/tiny_mce.js"></script> 
<script type="text/javascript"> 
tinyMCE.init({
	// General options
	mode : "textareas",
	theme : "advanced",
	skin : "o2k7",
	skin_variant : "black",
	plugins : "pagebreak,style,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,autosave",

	// Theme options
	theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "styleprops,|,cite,abbr,acronym,del,ins,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,

	// Example word content CSS (should be your site CSS) this one removes paragraph margins
	content_css : "css/word.css",

	// Drop lists for link/image/media/template dialogs
	template_external_list_url : "lists/template_list.js",
	external_link_list_url : "lists/link_list.js",
	external_image_list_url : "lists/image_list.js",
	media_external_list_url : "lists/media_list.js",

	// Replace values for the template plugin
	template_replace_values : {
		username : "Some User",
		staffid : "991234"
	}
});
</script> 
<!-- /TinyMCE -->
</head>
<body>
<div id="container">
<div id="header">
	<ul class="nav">
	<!-- Hier wordt het menu uit de database gelezen en gelijk in list items gezet -->
			<?php
			include('../cms/includes/connection.php');
				$page 	= (isset($_GET['p'])) ? $_GET['p'] : "1";
				$sql	= "SELECT id,name,url,title FROM nav";
				$result	= $conn->query($sql) or die(mysqli_error());

				if($result){
					while($row = $result->fetch_object()) {
						echo "<li> <a href='../{$row->url}' title='{$row->title}'>{$row->name}</a> </li>";
					}
				}	
			?>
	</ul>
</div>
<div id="banner">
	<div id="THIJSVANEMMERIK">
	THIJS VAN EMMERIK
	</div>
</div>
<div id="content">
<div class="cmscontent">
<h2>Nieuwe pagina toevoegen</h2>
<br /><br />
<div class="adminlink"><a href="index.php"><-- admin panel</a></div><br /><br />
<form method="post" action="">
	<table border="0" width="700" cellpadding="2">
	<tr>
		<td valign="top"><label for="title">Title:</label></td>
			<td><input type="text" name="title" /> <br /> <br /></td>
	</tr>
	<tr>
		<td valign="top"><label for="content">Content:</label><br /></td>
			<td><textarea class="editpage" name="content"><?php
			if ($select_maxid = mysql_query("SELECT MAX(id) AS max_id FROM pages")) {
				  $row = mysql_fetch_assoc($select_maxid) {
				  echo $row['max_id'];
				} else {
				  echo mysql_error();
				}		
			 ?> </textarea> <br /> <br /></td>
	</tr>
	<tr>
		<td><input type="submit" name="submit" value="aanmaken" /></td>
	</tr>
	</table>
</form>

</div>

</div>

<div id="contentfoot"></div>

    
</div>
</body>
</html>

 

And i don't know where i can turn error_reporting to E_ALL and display error switched on

Link to comment
Share on other sites

This is exactly what is in my connection.php

<?php
$conn = new MySQLi("localhost", "username", "pass", "db") or die("Er kan geen verbinding worden gemaakt ".mysql_error());

 

exept for the username pass and db

Link to comment
Share on other sites

Tissle, your code is a mismatch of using mysqli and mysql.

 

Your connection is creating a mysqli object. You must use mysqli throughout the remainder of your code and in the places were you are using the proper mysqli_error() function you must supply the connection link as a parameter or simply use mysqli OOP instead of procedural function calls.

 

Your code is probably producing numerous errors do to the mismatch of functions. Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON in your master php.ini so that all the errors php detects will be reported and displayed. You will save a ton of time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.