aabid Posted March 14, 2011 Share Posted March 14, 2011 It seems that in the following script I am not able to make $page contain some values as it always acts as it contains 0. The script will give you more idea about what I wanna do with it. <?php include('dbinfo.inc'); session_start(); $connect= mysql_connect($host,$dbuser,$password) or die("connect to database fails"); mysql_select_db($dbname, $connect); //This will set the number of messages we want to display on each page $rows_per_page = 5; function show_msg() { echo "<tr align='center'> <td>$row[message]</td> </tr> <tr><td><strong>Sent By: $_SESSION[username] ON Date: $row[date]</strong></td></tr> <tr><td></td></tr>"; } //This will set the necessay variables required to set the page layout function page_vars() { $sql = "select * from msgs"; $result = mysql_query($sql, $connect); $numrows = mysql_num_rows($result); $total_pages = ceil($numrows/$rows_per_page); //Check whether page is given or not if(isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; } if($page < 1): { $page = 1; } elseif($page > $total_pages): { $page = $total_pages; } endif; } if($_SESSION[loggedin] == TRUE && $_SESSION[actype] == lecturer) { echo "<form action='postmsg.php' method='POST'>"; echo "<table width='80%' border='1'><tr align='center'><td><strong>To send a new message type it in the below box and press SUBMIT</strong> </td></tr>"; echo "<tr align='center'> <td><textarea name='msgarea' cols='40' row='5'></textarea></td> </tr> <tr align='center'> <td><input type='submit' value='Send Message' /></td> </tr> </table>"; $limit .= 'LIMIT '.($page - 1)*$rows_per_page.', '.$rows_per_page; echo $limit; $sql = "select * from 'msgs' $limit"; $result = mysql_query($sql); echo "<table width='80%' border='1'>"; $row = mysql_fetch_assoc($result); while($row) { show_msg(); } } else { page_vars(); $limit .= 'LIMIT '.($page - 1)*$rows_per_page.', '.$rows_per_page; $sql = "select * from msgs $limit"; $result = mysql_query($sql,$connect); echo "<table width='80%' border='1'>"; while($row = mysql_fetch_assoc($result)) { show_msg(); } echo "<tr align='center'> <td> <a href='$_SERVER[php_self]?page=1'>First Page</a> <a href='$_SERVER[php_self]?page=($page - 1)'>$page</a> <a href='$_SERVER[php_self]?page=$page'>$page</a> <a href='$_SERVER[php_self]?page=($page + 1)'>$page</a> <a href='$_SERVER[php_self]?page=($total_pages)'>>>>Last Page</a> </td> </tr>"; echo "</table>"; } ?> now when i echo $limit it always shows "-5, 5" , that's not what i want from this script. Instead I want limit to store "0, 1" with the same concept not direct because it will change according to what $_GET holds. Hope I will get the solution here.... Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/ Share on other sites More sharing options...
AbraCadaver Posted March 14, 2011 Share Posted March 14, 2011 Vars that you define outside of a function are not available to a function and vars that you define inside of a function are only available inside the function. You can use globals to do this, or you can pass vars into and return from the functions: http://php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187434 Share on other sites More sharing options...
aabid Posted March 15, 2011 Author Share Posted March 15, 2011 Now there seems to be another problem in that script. I don't know why but that query is not executing well i think. I have echoed the $sql variable and all seems to be right there, but yet also it showing error as, mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\message.php on line 62 Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187742 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2011 Share Posted March 15, 2011 When you get that error, it means that your query has invalid syntax and is failing. On your query do something like: <?php $q = "your query here"; $rs = mysql_query($q) or die("Problem with the query $q<br>" . mysql_error()); ?> This will tell you what the problem is. Ken Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187746 Share on other sites More sharing options...
aabid Posted March 15, 2011 Author Share Posted March 15, 2011 Okay thanks for helping dude, Here is what i got when i used mysql_error() to know what's happening. "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''msgs' LIMIT 0, 5' at line 1" Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187752 Share on other sites More sharing options...
AbraCadaver Posted March 15, 2011 Share Posted March 15, 2011 Okay thanks for helping dude, Here is what i got when i used mysql_error() to know what's happening. "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''msgs' LIMIT 0, 5' at line 1" You quote ' data in a query. For column names and tables you use the back-tick `. You don't use either around integers used in the LIMIT or anywhere else for that matter. Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187807 Share on other sites More sharing options...
kenrbnsn Posted March 15, 2011 Share Posted March 15, 2011 If you echo your query before the error message, the error message becomes much clearer. Ken Link to comment https://forums.phpfreaks.com/topic/230616-cant-store-value-in-variable/#findComment-1187823 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.