The below code isn't printing out the lowest number in the database table. There is currently 4 entries - only three print out. The lowest ID number, doesn't print.
Code:
### Run Query
$query = "SELECT * from `Reminders`";
$run = mysqli_query($connection, $query);
$check = mysqli_fetch_array($run);
### Fetch Data
if($check > 0)
{
while($row = mysqli_fetch_assoc($run))
{
$ID = $row['ID'];
$Text = $row['rText'];
$Number = $row['rNumber'];
$Date = $row['rDate'];
$Time = $row['rTime'];
echo "'$Text' to '$Number' on $Date at $Time.<br/>";
}
}
Remove the $check part, and that's where the lowest one gone. For counting number of rows returned, use mysqli_num_rows() function.
### Run Query
$query = "SELECT * from `Reminders`";
$run = mysqli_query($connection, $query);
// Change here... :)
$check = mysqli_num_rows($run);
### Fetch Data
if ($check > 0) {
while ($row = mysqli_fetch_assoc($run)) {
$ID = $row['ID'];
$Text = $row['rText'];
$Number = $row['rNumber'];
$Date = $row['rDate'];
$Time = $row['rTime'];
echo "'$Text' to '$Number' on $Date at $Time.<br/>";
}
}
From php manual :- http://php.net/manual/en/mysqli-result.num-rows.php
mysqli_num_rows() function is used for returning the number of rows
in the result set.
So, Just Replace this of code,
$check = mysqli_fetch_array($run);
with this line,
$check = mysqli_num_rows($run);
Related
I have a 2-column table which I create like this (each user has a table for himself):
$sql = "CREATE TABLE $user_name (
driver TINYTEXT NOT NULL,
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
I have (manually, using phpMyAdmin) inserted data into one such a table. reg_date was set at current time and some random string (numerical, say 01234) was inserted.
I try accessing the rows like this:
$mysql_qry2 = "SELECT * FROM $username";
$result2 = mysqli_query($conn, $mysql_qry2);
$ans = "";
if(mysqli_num_rows($result2) > 0) {
while ($row = mysqli_fetch_assoc($result2)) {
$ans = $ans . $row["driver"];
$ans = $ans . " ";
$x = $row["driver"];
echo "$x";
echo "a";
}
$ans = $ans . "asd";
echo "$ans";
} else {
echo "b";
}
edit: after fixing my mistaken variable name, the "SELECT *" query is returning zero rows despite 3 rows of data being there.
You checked count of wrong variable mysqli_num_rows($result) > 0.
$mysql_qry2 = "SELECT * FROM $username";
$result2 = mysqli_query($conn, $mysql_qry2);
$ans = "";
if ($result2->num_rows > 0) {
// output data of each row
while($row = $result2->fetch_assoc()) {
$ans = $ans . $row["driver"];
echo "$ans";
echo "a";
}
} else {
echo "b";
}
I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.
I am getting data from a MySQL database using an array (via PHP).
I was wondering, is it okay to subtract array values, like this (on line 7):
$num = 30;
$result1 = mysql_query('SELECT * FROM table1');
$result2 = mysql_query('SELECT * FROM table2');
while($row1 = mysql_fetch_array($result1) && $row2 = mysql_fetch_array($result2)) {
$sub = $row1['number'] - $row2['number'];
if($sub<=$num) {
echo $row1['person'];
}
I'm actually not getting any results back (just blank). So I was wondering if that line or any parts of my code is logically correct?
Try like this
$num = 30;
$result = mysql_query('SELECT number.table1 as n1, number.table2 as n2, colName.tableName FROM table1 JOIN table2 ON id.table1 = table1_id.table2');
while($row = mysql_fetch_array($result)) {
$sub = $row['n1'] - $row['n2'];
if($sub<=$num) {
echo $row['person'];
}
}
I have this query. However, it does not work properly. The echo returns always a 1, but there are 3 rows in the db
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) FROM profiles";
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num == 0)
echo "0";
echo $num;
mysqli_close($con);
?>
You're doing an aggregate query, which means you'll ALWAYS get one row of results - one row containing the count() value you requested. Even if that count() is 0, you'll STILL get one row of results.
If you want to check the value of the count, you have to fetch that row and check the field's value, e.g.
$sql = "SELECT COUNT(id) AS cnt FROM profiles";
$result = mysqli_query($con, $sql) or die(mysqli_error($con));
$row = mysqli_fetch_assoc($result);
if ($row['cnt'] == 0) { die("No profiles"); }
Your query returns 1 row with value 3
To see what you do expect you need something like:
<?php
include "db_connect.inc.php";
$sql = "SELECT COUNT(id) myCount FROM profiles";
$res = mysqli_query($con, $sql);
if ($row = mysqli_fetch_array($res, MYSQLI_ASSOC) ) {
echo $row['myCount'];
} else {
echo "0";
}
mysqli_close($con);
?>
Based on my codes, i need to restrict the insertion of the data by 3, i mean is like after the insertion of 3 data row, it will be restricted from inserting in data. Is that possible? For more information, is like the borrow inserting 3 times, then it cannot be inserted anymore. Is there anyway to do so? I am still learning php by the way, thank you.
if(isset($_POST['selector']))
$id=$_POST['selector'];
else
$id = '';
$member_id = $_POST['member_id'];
$due_date = $_POST['due_date'];
$isbn = $_POST['due_date'];
if ($id == '' ){
//header("location: borrow.php");
if(isset($_POST['isbn'])){
$isbn = $_POST['isbn'];
$query = mysql_query("select book_id from book WHERE isbn = '$isbn'")or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 0){
$row = mysql_fetch_array($query);
$bookid = $row['book_id'];
$date = date('Y-m-d');
}
mysql_query("insert into borrow (member_id,book_id,date_borrow,due_date) values ('$member_id','$bookid','$date','$due_date')")or die(mysql_error());
}
else{
header("location: borrow.php");
}
}else{
mysql_query("insert into borrow (member_id,date_borrow,due_date) values ('$member_id',NOW(),'$due_date')")or die(mysql_error());
$query = mysql_query("select * from borrow order by borrow_id DESC")or die(mysql_error());
$row = mysql_fetch_array($query);
$borrow_id = $row['borrow_id'];
}else{
mysql_query("insert into borrow (member_id,date_borrow,due_date) values ('$member_id',NOW(),'$due_date')")or die(mysql_error());
$query = mysql_query("select * from borrow order by borrow_id DESC")or die(mysql_error());
$row = mysql_fetch_array($query);
$borrow_id = $row['borrow_id'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
mysql_query("insert borrowdetails (book_id,borrow_id,borrow_status)
values('$id[$i]','$borrow_id','pending')")or die(mysql_error());
}
header("location: borrow.php");
}
You just have to count number of user row before to make a new insert :
$query = mysql_query("SELECT COUNT(*) AS count FROM borrow WHERE member_id = '".$member_id."'");
$row = mysql_fetch_assoc($query);
if ( $row['count'] >= 3 )
echo('Max insert');
Also, check this : Why shouldn't I use mysql_* functions in PHP?
I'm not sure I understand you correctly.
You can restrict the number of rows returned by SELECT query using the LIMIT clause.
Make sure you either put an ORDER BY clause in there or determine that you don't care 'which' 3 rows will get inserted.
See here:
http://dev.mysql.com/doc/refman/5.0/en/select.html