Count Number of results with Limit - php

How do I count number of results with limits? Obviously the code I have is not correct. Any help?
$result = mysqli_query ($conn, "select SQL_CALC_FOUND_ROWS * from $sql $limit");
$db_count = mysqli_query ("SELECT FOUND_ROWS()");
ANSWER: $sql = "$db_name.$db_table $where $where_search $db_sort";
$result = mysqli_query ($conn, "select * from $sql $limit");
$rec = mysqli_fetch_array(mysqli_query($conn, "SELECT COUNT(*) as totalCount FROM $sql"));
$db_count = $rec['totalCount'];

Related

SELECT PHP and MySQL not working

I have this code for the select and printing values.
mysql_numn_rows returns null :/
<?php
mysql_connect('localhost','root','');
mysql_select_db("db2517");
$username = "Cristoforo";
$query = mysql_query("SELECT * users WHERE username='$username' ");
$numberOfRows = mysql_num_rows($query);
echo "num: $numberOfRows";
?>
First I would like to suggest you user PDO or MYQLI
In your query you have missed the from so replace your query:
From
$query = mysql_query("SELECT * users WHERE username='$username' ");
To
$query = mysql_query("SELECT * from users WHERE username='$username' ");
Better use mysqli or pdo instead of mysql.here you missed from in your query so your Query should be
$query = mysql_query("SELECT * from users WHERE username='$username' ");

Update the selected random row PHP

I'm trying to update the selected random row in database here is my php code
$offset_result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `tbl_combi` WHERE `clear` = 0 ");
$offset_row = mysqli_fetch_object( $offset_result );
$offset = $offset_row->offset;
$result = mysqli_query($conn, "SELECT * FROM `tbl_combi` LIMIT $offset, 1");
$result_fetch = mysqli_fetch_array($result);
echo $result_fetch[1];
You never perform any update. Here is something you could try (it might need some tuning to fit your need) :
$offset_result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `tbl_combi` WHERE `clear` = 0 ");
$offset_row = mysqli_fetch_object( $offset_result );
$offset = $offset_row->offset;
$updateSql = 'UPDATE tbl_combi SET my_field="my_value" WHERE offset=' . $offset;
mysqli_query($conn, $updateSql);
$result = mysqli_query($conn, "SELECT * FROM `tbl_combi` LIMIT $offset, 1");
$result_fetch = mysqli_fetch_array($result);
echo $result_fetch[1];

How to select rows in MySQL/PHP?

I am trying to display my first 10 users and their information by uid. When I use the code below, it only returns a single uid:
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
echo $result->fetch_object()->uid;
What function am I supposed to use to display rows correctly?
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($var = $result->fetch_object()->uid){
echo $var;
}
Should do the trick
If you get a list of object you should use a loop for show them
include "db_conx.php";
$sql = ('SELECT uid,username,country FROM users ORDER BY uid DESC LIMIT 10');
$result = mysqli_query($db_conx, $sql);
while($row = mysqli_fetch_object($result)) {
$row->uid;
}

Random Image PHP/SQL

I have 2 database tables. loppe containing username and loppeID, and billeder containing loppeID and billedeNavn.
I would like to have a div containing 2 random billedeNavn from the same username. I have tried some things but I can only figure out how to get 2 billedeNavn from the same loppeID (username). I'm pretty new to all this. So would me grateful for any help.
<?php
$frontimage = ("SELECT * FROM loppe RIGHT JOIN billeder ON loppe.loppeID=billeder.loppeID WHERE loppe.username = '$username' ORDER BY RAND()");
$st = $db->prepare($frontimage);
$st->execute();
$row = $st->fetch();
$loppeid = $row['loppeID'];
$loppe = $loppeid;
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";
$stmt = $db->prepare($sql);
$stmt->execute(array("loppeID" => $loppe));
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$fname = $row['billedeNavn'];
echo '<div class="col-xs-6 loppe-pic-outer"><img class="img-responsive loppe-pic" src="loppebilleder/'.$fname.'"></div>';
}
?>
Have you tried this change your
$sql = "SELECT * FROM billeder WHERE loppeID = :loppeID order by rand() limit 2";?
to this
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
A good way to figure out what is going on when using php and database is after you create your query do something like
$sql = "SELECT * FROM billeder WHERE loppeID = '.$loppeid.' order by rand() limit 2";
die($sql);
So with this you will see what the database is really querying and you can check for in your sql string or query.

MySQL Query using NOT in it, possible?

I have a database of order status' and I'd like to return the amount of rows for which the value of that row is anything but Complete.
How would I go about this? I have the following which shows how much rows has 'New' as the status for the order. But I'd like to show all rows except ones with the od_status of Complete.
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status = 'New'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
<?php
$query = "SELECT od_status FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
echo "$num_rows";
?>
$query = "SELECT od_status FROM tbl_order WHERE od_status <> 'Complete'";
$query = "SELECT COUNT(*) FROM tbl_order WHERE od_status != 'Complete'";
$result = mysql_query($query);
var_dump($result);
COUNT to count rows and != to select where is not equal to Complete.

Categories