While loop does not show table record values - php

I have two pieces of code. The first code shows the value of a table column named 'title'. But the second piece of code with a while loop does not work. It is not showing the values of the table column named 'title'.
First Code:
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
$row = mysql_fetch_assoc($res)
echo $row["title"];
} // End If
?>
Second code :
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
if($res && mysql_num_rows($res)>0)
{
while($row = mysql_fetch_assoc($res));
{
echo $row["title"];
} // End While
} // End If
?>

Remove the semicolon from the end of the while.
And I can say that if command is completely unnecessary here and you can remove it without change.
<?php
$sql = "SELECT * FROM cl_banner ORDER BY id";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res))
{
echo $row["title"];
} // End While
?>

Related

Group By Not working - only displaying 1st entry

I have over 40'000 entries and each is assigned to a "list_name"
I am basically trying to get just the list_name value echo'd out
$groupq = mysqli_query($dbc, "SELECT * FROM `products-full` GROUP BY `list_name`");
$groupr = mysqli_fetch_assoc($groupq);
do {
echo $groupr['list_name'];
} while($groupr = mysqli_fetch_assoc($groupq));
however its only displaying 1 entry then no more ..
https://imgur.com/a/3rnXGet
Try this.
$groupq = mysqli_query($dbc, "SELECT * FROM `products-full` GROUP BY `list_name`");
while($groupr = mysqli_fetch_assoc($groupq)){
echo $groupr['list_name'];
}
$database = "sample" //replace your database name here
$conn=new mysqli("localhost","root","",$database); // here username is root and password is null , change it according to yours
if($conn->connect_error)
{
echo $conn->connect_error;
die("sorry database connection failed");
}
$sql = "SELECT * FROM products-full GROUP BY list_name";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row['list_name'];
}
}
thats it
try this
$groupq = mysqli_query($dbc, "SELECT list_name, count(*) FROM `products-full` GROUP BY
`list_name`");
while($groupr = mysqli_fetch_assoc($groupq)) {
echo $groupr['list_name'];
}

prevent insert same id if the user/student not put timeout

i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>

How I can navigate to another page and display user data?

I know how to fetch user data from database with the code below. Now I want to navigate to another page (using onclick) and to display this user data by id. This would be like StackOverflow or Facebook, when you click on a photo or ID, and the site takes you to the user's profile page.
Here is my code so far:
<?php
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users order by id DESC");
$id = $_SESSION['id'];
while($row = mysql_fetch_array($result)){
if($row['id'] !== $id){
echo "<table id='suggest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";
}
}
?>
$id = $_GET['id'];
if(!isset($id))
{
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users WHERE id = '"$id"'");
if(!$result)
{
die('user_not_found');
}
mysqli_fetch_row( $result );
echo "<table id='sugest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";
suppose you are in a page before clicking on a user profile,the link should be some thing like this 'site.com/userprofile.php?id=5'.
now in userprofile.php:
$id = $_GET['id'];
if(!isset($id))
die('user not found');
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users where id='".$id."'");
if (!$result) {
die('user not found');
}
$row = mysql_fetch_row($result);
echo "<table id='sugest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";

how to use two mysql_fetch_array() arrays in one while loop

Hi i have two arrays built by using mysql_fetch_array() i want to use them both in one while loop :
$username =$_SESSION["username"];
$password =$_SESSION["password"];
$query1= "SELECT * FROM subs WHERE username='$username' AND password='$password' ";
$res1 = mysql_query($query1);
$row_cnt1 = mysql_num_rows($res1);
if($res1 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row1 = mysql_fetch_array($res1);
if($row_cnt1 == 1){
$sid = $row1["id"];
}else{
die("there is more than one user with the same username and password");
}
$query2= "SELECT * FROM bookreservations ";
$res2 = mysql_query($query2);
$row_cnt2 = mysql_num_rows($res2);
if($res2 === FALSE) {
die(mysql_error()); // TODO: better error handling
}
$row2 = mysql_fetch_array($res2) ;
$query= "SELECT * FROM books";
$res = mysql_query($query);
$row_cnt = mysql_num_rows($res);
if($res === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while ($row = mysql_fetch_array($res)){
if($row2["sid"] == $sid && $row2["bid"] == $row["id"]){
continue;
}
$temp = $row["id"];
$temp1 = $row["title"];
echo "Title : ".$temp1."<br><br>";
$temp1 = $row["authors"];
echo "Authors : ".$temp1."<br><br>";
$temp1 = $row["copies"];
echo "Copies : ".$temp1."<br><br><br><br>";
echo "<div class=\"re\"><input type=\"radio\" value=\"reserve\" name=\"".$temp ."\">reserve</div> <br><br><br><br><br><br>" ;
}
I want to extract things from row array but i don't want them to have some values from row2 array so I have to use them both in one while loop ,THE SUMMARY row array is ok it jumps to the next row in each loop but row2 array is stuck it doesn't move to the next row what to do ?
Although I don't really understand your task well, I however, would recommend the following edits
My assumption: You are trying to fetch all rows from $res for each $res2.
Although inefficient technique, but I'll edit your code as below.
while($row2 = mysql_fetch_array($res2) ){
while ( $row = mysql_fetch_array($res) ){ //Both $row & $row2 will have equal incremented rows.
}
}//Outer loop

Undefined variable, unsure why

<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$sql1 = "SELECT * FROM topics WHERE id='$tid' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
}
$sql = "SELECT * FROM users WHERE id='$creator' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"];
}
echo $name;
?>
I'm a little new to PHP, but I've done things exactly like this, but this time I'm getting an error. Everything here works except for $name. I checked my SQL tables and made sure users exist and that there's first and a last area. I don't see what else could be wrong.
Notice: Undefined variable: name in * on line **
Thank you.
Try this code on for size:
<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$tid = mysqli_escape_string($connect, $tid);
$sql1 = "SELECT * FROM topics WHERE id='{$tid}' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
// Check for rows first.
if($res1 and mysqli_num_rows($res1)){
// Use if as while is pointless on LIMIT 1
if($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
$creator = mysqli_escape_string($connect, $creator);
$sql = "SELECT * FROM users WHERE id='{$creator}' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
// Check for rows first.
if($user_query and mysqli_num_rows($user_query)){
// Use if as while is pointless on LIMIT 1
if ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"]; // NO HIT!
}
echo $name;
}else{
echo 'no rows found (query 2).';
}
}
}else{
echo 'no rows found (query 1).';
}
?>
Variable $name is undefined because the $name = ...; line is not reached. So make sure you $sql query actually returns results. It has to in order to define $name.

Categories