Jump to content

Can't Store Value In Variable ???


aabid

Recommended Posts


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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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.