Im trying to get some values from mysql and echo each value inside an article. Everything is working fine , I just wanna know how to limit page articles to 15 per page and when the limit is reached create page #2 etc , Here is the php code im using
$query = "SELECT `text` FROM `items` ORDER BY 'id'";
if ($query_run = mysql_query($query))
while ($query_row = mysql_fetch_assoc($query_run)) {
$text = $query_row['text'];
echo "<article>$text</article>";
}
} else {
echo mysql_error($conn_error);
}
Any suggestions? Thanks .
This has nothing to do with HTML. Add a LIMIT clause to your SQL query.
select `text` from `items` order by 'id' limit 15
Read up on SQL Pagination.
In addition to #meagar's answer: mysql is deprecated. You should use the mysqli improved API.
Example:
$mysqli = new mysqli('host', 'user', 'password', 'databasename');
$query = "SELECT `text` FROM `items` ORDER BY 'id' LIMIT 15";
if($result = $mysqli->query($query)){
while ($row = $result->fetch_assoc()) {
$text = $row['text'];
echo "<article>$text</article>";
}
}
}
Related
I got a table that contains ID, Names, message, and time, I want to select from the table only one message query from each ID, Currently I select all the messages, this is my code
$query= mysql_query("SELECT * FROM `table` ORDER BY `time`")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo '</br>';
echo $msg;
}
That Shows all messages ordered by time, Is there is a way to select only one message query from each ID?
Thanks,
Klaus
If you want only one message you can use LIMIT like this
SELECT * FROM table ORDER BY time LIMIT 1
Or if you want only one message from some id then you can use GROUP BY
SELECT * FROM table ORDER BY time GROUP BY id
Sure, pretty straightforward
This will fetch all messages given ID:
$id = 10 //Get your id in any way you need
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id")or die(mysql_error());
while($arr = mysql_fetch_array($query)){
$num = mysql_num_rows($query);
$msg = $arr['message'];
echo $msg;
}
and this will fetch only the first message given ID
$id = 10
$query= mysql_query("SELECT `message` FROM `table` WHERE `ID` = $id LIMIT 1")or die(mysql_error());
$row = mysql_fetch_array($query));
if($row){
$msg = $row['message'];
echo $msg;
}else{
echo 'no messages for id '.$id;
}
Im a "noob" in smarty. I need to execute following code in one of my .tpl files:
<? // SELECT sql query
$sql = "SELECT 'id' , 'title' FROM `forum_posts` WHERE bid = '1' ORDER BY 'date' DESC LIMIT 4";
// perform the query and store the result
$result = query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
while($row = $result->fetch_assoc()) {
echo '<tr>
<td>'. $row['title']. ' </td>
</tr> ';
}
}
else {
echo 'No news';
}
?>
I've been trying for 3 hours now, surfing all over the web but without success.
Help please!
You are using quotes instead of backtick for column name, simply change them to avoid the error
SELECT `id` , `title` FROM `forum_posts` WHERE `bid` = '1' ORDER BY `date` DESC LIMIT 4";
require('../libs/SmartySQL.class.php');
$smarty = new SmartySQL( array('pdo_dsn' => 'mysql:dbname=db_name;host=localhost',
'pdo_username' => 'username',
'pdo_password' => 'password',
'pdo_driver_options' => array() ) );
$smarty->display('index.tpl');
I am needing to run more than one Mysql Query using PHP. I have a site and pull all the information from the databse
$sql = "SELECT * FROM $table WHERE ID=$escape";
$query = mysql_query($sql) or die(mysql_error());
$rentals = mysql_fetch_assoc($query);
Now I have two other queries I need to also run for Previous and Next Buttons
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < '$curId' AND `catId` = '$curCat'
ORDER BY `id` DESC LIMIT 1;
$sqlNext = 'SELECT `id` FROM `table`
WHERE `id` > '$curId' AND `catId` = '$curCat'
ORDER BY `id` ASC LIMIT 1;
I have the coding right when I run these in PHP MyAdmin, however when I try to execute them via the website I get a mysql error!
Problems are the " instead of ' at $sqlPrev and $sqlNext. {$curID} only works with "".
And there's no end " or '.
mysql_query can only execute one query at a time.
Basically you just need to have 3 calls to mysql_query.
$sql = "SELECT * FROM $table WHERE ID=$escape";
$query = mysql_query($sql) or die(mysql_error());
$rentals = mysql_fetch_assoc($query);
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < ' . $curId . ' AND `catId` = ' . $curCat . '
ORDER BY `id` DESC LIMIT 1';
$sqlNext = 'SELECT `id` FROM `table`
WHERE `id` > ' . $curId . ' AND `catId` = ' . $curCat . '
ORDER BY `id` ASC LIMIT 1';
$resultPrev = mysql_query($sqlPrev);
$resultNext = mysql_query($sqlNext);
// todo: check that the above queries executed successfully
// if (!$resultPrev) echo mysql_error();
if (mysql_num_rows($resultPrev)) {
$prev = mysql_fetch_array($resultPrev);
$prevId = $prev['id'];
} else {
$prevId = null; // there is no previous item
}
if (mysql_num_rows($resultNext)) {
$next = mysql_fetch_array($resultNext);
$nextId = $next['id'];
} else {
$nextId = null; // there is no next item
}
You probably need to add the concatenation operator (.) between the string literals and the variables. (It's required in Perl; I do the same thing in PHP.)
$sqlPrev = 'SELECT `id` FROM `table`
WHERE `id` < '.$curId.' AND `catId` = '.$curCat.'
ORDER BY `id` DESC LIMIT 1';
Echo the SQL text that is being sent to the database. That will reveal the problem.
For more than one query, you need to use multi query or close the result before calling another query. For example if you write in the object oriented style.
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$result=$conn->query($sql);
echo $result->num_rows;
$sql="SELECT id FROM mytable";
$result2=$conn->query($sql);
echo $result2->num_rows; // does not work because result was not closed.
To get it to work, write this instead.
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$result=$conn->query($sql);
echo $result->num_rows;
$result->close(); //*********notice this new line. result
//needs to be closed before calling another query
$sql="SELECT id FROM mytable";
$result2=$conn->query($sql);
echo $result2->num_rows; // **this does work because previous result
// was closed.
If you need to do multiple queries at once or need to do a query before you finish outputting rows for the previous one, use multi query. I use multi query for the second reason, getting another query before outputting all the rows of the previous one. This is important for nested queries, such as one in the middle of another, such as in multiple nested threads in a forum, or for nested replies used on some web pages.
Here is an example of one query done in the middle of another query.
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM mytable";
$conn->multi_query($sql);
$result=$conn->store_result();
$count=0;
while($row = $result->fetch_assoc()) {
echo "$result['id'] $result['name'];
if ($count==0) {
// now you can do another query in the middle of this one
$sql="SELECT id FROM mytable";
$conn->multi_query($sql);
$result2=$conn->store_result();
$row2=$result2->fetch_assoc();
echo "result of second query is: $row2['id'] $row2['name']";
}
$count=$count+1;
}
My page displays an image, and I want to display the previous and next image that is relevant to the current one. At the moment I run the same query 3x and modify the "where" statement with =, >, <.
It works but I feel there must be a better way to do this.
The image id's are not 1,2,3,4,5. and could be 1,2,10,20,21 etc. But if it is much more efficient I am willing to change this.
mysql_select_db("database", $conPro);
$currentid = mysql_real_escape_string($_GET['currentid']);
$query ="SELECT * FROM database WHERE id ='".$currentid."' LIMIT 1 ";
$result = mysql_query($query,$conPro) or die(mysql_error());
$affected_rows = mysql_num_rows($result);
if ($affected_rows==1)
{
$row = mysql_fetch_array($result)or die ('error:' . mysql_error());
$current_id = $row['id'];
$current_header = $row['title'];
$current_description =$row['desc'];
$current_image = "http://".$row['img'];
$current_url = "http://".$row['id']."/".$db_title."/";
$current_thumb = "http://".$row['cloud'];
}
mysql_select_db("database", $conPro);
$query ="SELECT * FROM database WHERE id <'".$currentid."' ORDER BY id DESC LIMIT 1 ";
$result = mysql_query($query,$conPro) or die(mysql_error());
$affected_rows = mysql_num_rows($result);
if ($affected_rows==1)
{
$row = mysql_fetch_array($result)or die ('error:' . mysql_error());
$previous_id = $row['id'];
$previous_header = $row['title'];
$previous_description =$row['desc'];
$previous_image = "http://".$row['img'];
$previous_url = "http://".$row['id']."/".$db_title."/";
$previous_thumb = "http://".$row['cloud'];
}else{
$previous_none = "true"; //no rows found
}
mysql_select_db("database", $conPro);
$query ="SELECT * FROM database WHERE id >'".$currentid."' ORDER BY id ASC LIMIT 1 ";
$result = mysql_query($query,$conPro) or die(mysql_error());
$affected_rows = mysql_num_rows($result);
if ($affected_rows==1)
{
$row = mysql_fetch_array($result)or die ('error:' . mysql_error());
$next_id = $row['id'];
$next_header = $row['title'];
$next_description =$row['desc'];
$next_image = "http://".$row['img'];
$next_url = "http://".$row['id']."/".$db_title."/";
$next_thumb = "http://".$row['cloud'];
}else{
$next_none = "true"; //no rows found
}
mysql_close($conPro);
Thank you for your time
You don't have to do select_db each time. Once you 'select' a db, it stays selected until you select something else.
You can't really get away from doing two separate queries to get the next/previous images, but you can fake it by using a union query:
(SELECT 'next' AS position, ...
FROM yourtable
WHERE (id > $currentid)
ORDER BY id ASC
LIMIT 1)
UNION
(SELECT 'prev' AS position, ...
FROM yourtable
WHERE (id < $currentid)
ORDER BY id DESC
LIMIT 1)
This would return two rows, containing a pseudofield named 'position' which will allow you to easily identify which row is the 'next' record, and which is the 'previous' one. Note that the brackets are required so that the 'order by' clauses apply to the individual queries. Without, mysql will take the order by clause from the last query in the union sequence and apply it to the full union results.
You can get the "previous" one first WHERE id <'".$currentid."' ORDER BY id DESC, and then query for two "above" it: SELECT * FROM database WHERE id >= '".$currentid."' ORDER BY id ASC then it takes only two queries instead of three.
I am trying to show all of the data in the 'status' column of my table but am having troubles. What am I doing wrong:
<?php
$query1 = "SELECT id, status FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1);
while ($row = mysql_fetch_array($result))
{
echo $row['status'] ;
}
?>
Try this:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id LIMIT $start, $limit ";
$result = mysql_query($query1) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo $row['status'];
}
Also, make sure that:
$_SESSION['customerid'], $start and $limit are not empty. You can test the constructed query with echo $query1;
Note: Addition of mysql_error() in in the mysql_query will allow you to see if there is an error in the query.
I am trying to show all of the data in
the 'status' column of my table
If you want to show all the rows, your query should be:
$query1 = "SELECT id, `status` FROM alerts ORDER BY id";
But if you want to show for a specific customer, your query should be:
$query1 = "SELECT id, `status` FROM alerts WHERE customerid='".$_SESSION['customerid']."' ORDER BY id";