Tissle Posted July 1, 2010 Share Posted July 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/ Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Have you tried checking your query actually succeeds before using any result it produces? Any debugging at all? What does mysql_error() have to say? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079614 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079623 Share on other sites More sharing options...
Ruzzas Posted July 1, 2010 Share Posted July 1, 2010 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079627 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 if ($select_maxid = mysql_query("SELECT MAX(id) AS max_id FROM pages")) { $row = mysql_fetch_assoc($select_maxid) { else $row['max_id']; } else { echo mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079629 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079633 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Obviously the connection isn't being made. PS: That code is floored. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079637 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079645 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Use the code I posted above and post the exact output. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079648 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 When i use you code it shows me a blank page with no error. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079650 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079652 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 still blank Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079655 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 Your going to need to post all your relevant code. Also, make sure you have error_reporting set to E_ALL and display errors switched on. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079659 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079662 Share on other sites More sharing options...
trq Posted July 1, 2010 Share Posted July 1, 2010 The rest of your code is using some database abstraction layer, and maybe the mysqli extension. What exactly is within your connection.php file? Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079663 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079667 Share on other sites More sharing options...
PFMaBiSmAd Posted July 1, 2010 Share Posted July 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079727 Share on other sites More sharing options...
Tissle Posted July 1, 2010 Author Share Posted July 1, 2010 Thanks for the reply i didn't know that. How can i make a php.ini with all the errors ON ? Sorry I'm a noob with php but willing to learn a lot about it. Quote Link to comment https://forums.phpfreaks.com/topic/206378-i-cant-get-maxid-into-variabele-with-1/#findComment-1079738 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.