Why it only reads the first row? - php

i'm trying to use if condition in a href. my problem is why it only reads the first row in database. I've also tried to use while. Here is my code...
<?php
include 'dbconfig/config.php';
$select = "SELECT * FROM schedule_tbl";
$select_result = mysqli_query($con, $select);
if(mysqli_num_rows($select_result)> 0) {
$row = mysqli_fetch_assoc($select_result);
if($row['status'] == "close") { ?>
<li>Enroll Now Here!</li>
<?php } else { ?>
<li>Enroll Now Here!</li>
<?php }
}
?>
schedule.tbl
Using a while loop...

Try to use
if (mysqli_num_rows($select_result) > 0)
{
while ($row = mysqli_fetch_array($select_result))
{
if ($row['status'] == "close")
{
//
}
else
{
//
}
}
}

Related

php else statement for empty search

I have a search function which matches keywords from a database and echo's out some html but I'm missing how to enable the handling of empty searches. Can I use an else statement or do I have to redefine the same parameters and use !isset for if not set?
<?php
$con = mysqli_connect("localhost", "database", "password", "table");
if (isset($_GET['search'])) {
$search_query = $_GET['search_query'];
global $con;
$get_item = "select * from database where keywords like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $search_query";
} // working fine up to here
} else {
echo "Search not found for $search_query";
}
?>
You can use isset() and mysqli_num_rows() to check empty result. ANd use mysqli_real_escape_string for fire your query
if (isset($_GET['search'])) {
if (isset($_GET['search_query']) && $_GET['search_query'] != "") {/// check variable is set or not
$search_query = $_GET['search_query'];
$$search_query = mysqli_real_escape_string($con, $search_query);//
$get_item = "select * from `database` where `keywords` like '%$search_query%'";
$run_item = mysqli_query($con, $get_item);
$row_cnt = mysqli_num_rows($run_item); // count number of rows
if ($row_cnt > 0) {
while ($row_item = mysqli_fetch_array($run_item)) {
$item_keywords = $row_item['item_keywords'];
echo "Search found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
} else {
echo "Search not found for $item_keywords";
}
}
Change your condition to:
if (isset($_GET['search']) && trim($_GET['search_query']) != '') {
}
You can set condition for empty search like this.
if (isset($_GET['search']) && trim($_GET['search']) !='') { ... }
Modify your if condition
if (isset($_GET['search']) && !empty($_GET['search_query'])) {
}
You can do it by using the empty function:
if (!empty($_GET['search'])) {
//your code
}

Comparing stored data in a column to an existing value

I have a column called favid. I am trying to pull and compare the data in that column to an existing value:
<?php $query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid=$favid");
while ($row = mysql_fetch_assoc($query)) {
echo $row['favid']; };?>
I also have an existing value:
$x
But when I do something like this it doesn't work:
<?php if($row['favid'] == $x){?>
Do this...
<?php } else { ?>
Do nothing...
<?php}?>
I realize the data in the column somehow isn't pulled out. What should be done for this to work?
Try this, I assume you already connected to DB.
<?php
$x = 1;
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='$favid'") or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($query))
{
if ($row["existing_column_name"] == $x)
{
echo "Yes";
} else
{
echo "No";
}
}
} else
{
echo "Nothing was found";
}
?>
<?php
$x = 100500; // integer for example
$CID = mysql_connect("host","user","pass") or die(mysql_error());
mysql_select_db("db_name");
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='{$favid}'", $CID);
while ($row = mysql_fetch_assoc($query)) {
if (intval($row["some_existing_column_name"])==$x){
print "Is equals!";
} else {
print "Is different!";
}
}
?>
Please be informed that mysql_connect and other functions with the prefix of mysql_ is deprecated and can be removed in the next versions of PHP.

in php table tag not working

I want the table tag to come before the display of records in cart() function but it is being displayed after it rather?
How to correct that and in cart() function in the display of records when I am trying <tr> and <td> tags to display its not working
<?php
session_start();
$page = 'index.php';
$connection = mysqli_connect("localhost","root","","cart");
if(isset($_GET['add']))
{
if(array_key_exists('cart_'.$_GET['add'], $_SESSION))
$_SESSION['cart_'.$_GET['add']]+= 1;
else
$_SESSION['cart_'.$_GET['add']] = 0;
header("Location: cartindex.php");
}
if(isset($_GET['remove']))
{
$_SESSION['cart_'.$_GET['remove']]--;
header("Location: cartindex.php");
}
if(isset($_GET['delete']))
{
$_SESSION['cart_'.$_GET['delete']]=0;;
header("Location: cartindex.php");
}
function cart()
{
global $connection;
$total = 0;
?>
<table class="table table-striped"><tr><th>ID</th><th>Name </th><th>Price Per Item</th><th>Cost</th><th>Add</th><th>Substract</th><th>Delete</th></tr>
<?php foreach ($_SESSION as $key => $value) {
if($value > 0)
{
$id = substr($key,5,strlen($key)-1);
$result = mysqli_query($connection ,'select id,name,price from products where id ='.$id);
while($row = mysqli_fetch_assoc($result))
{
$cost = $row['price'] * $value;
echo $row['id'].' '.$row['name'].'#'.$row['price'].'*'.$value.'='.$cost.'[+]'.'[-]'.'[delete]'.'<br>';
$total = $total + $cost;
}
}
}
?></table><?php
if($total==0)
{
///
}
else
{
$dis="'payment made'";
echo 'Total cost is '.$total.'<br>';
echo '<br><button type="button" class="btn btn-success" onclick="alert(\'Payment accepted\');">Success</button>';
}
}
function product()
{
$connection = mysqli_connect("localhost","root","","cart");
if(mysqli_connect_errno())
{
die("not connected to db ".mysqli_connect_error());
}
$get = mysqli_query($connection , "select id,name,description,price from products where quantity > 0 order by id DESC");
while($row = mysqli_fetch_assoc($get))
{
echo '<div class="boxed">'.$row['name'].'<br>'.$row['price'].'<br>'.$row['description'].'<br>ADD'.'<br>'.'</div>';
}
}
?>
Try replace "echo" with "return" inside functions.

php ajax search does not display results

I'm trying to make a php ajax search for the articles posted in database, but it gives me only : No results message. I've tested the connection to database and the select query, and it works.
This is the code:
<?php
require_once('dbconn.php');
$s = $_GET["s"];
$livesearch = '';
if (strlen($s) > 0)
{
$result = mysql_query("select * from articles where art_sts='1' ORDER BY title");
if ($result != FALSE)
{
foreach($result as $row)
{
if (stristr($row['title'], $s))
{
if ($livesearch == '')
{
$livesearch = ' '.htmlentities($row["title"], ENT_QUOTES, "UTF-8").'';
}
}
}
}
}
if ($livesearch == '')
{
$respond="No results...";
}
else
{
$respond = $livesearch;
}
echo $respond;
?>
You havent actually fetched the data that you are trying to output. You will need to add something like this...
while($row = mysql_fetch_assoc($result)) {
if (stristr($row['title'], $s))
{
if ($livesearch == '')
{
$livesearch = ' '.htmlentities($row["title"], ENT_QUOTES, "UTF-8").'';
}
}
}

User levels with PHP

What I'm trying to do is make it so that when a specific user level is set in my Database it shows up specific text. Here is my code.
$userget = mysql_query("SELECT * FROM `usertable` WHERE `username`='".$_SESSION['user']."'");
$user = mysql_fetch_array($userget);
function getUserlevel() {
if($user['userlevel'] == '1') { echo "Regular User"; }
elseif($user['userlevel'] == '2') { echo "Moderator"; }
elseif($user['userlevel'] == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}
I know the function is working, because the Echo of Undefined works, which I set up as a test but the others are not working despite my user level being set to 3 and only echoing Undefined. A session is also set for when the user is logged in.
This is when the function is called.
<div class="userData">Welcome Back, <?php echo $_SESSION['user']; ?><hr /><?php
getUserlevel(); ?></div>
Ok, I'm going to take a stab at:
getUserlevel($user['userlevel']);
function getUserlevel($usrlvl) {
if($usrlvl == '1') { echo "Regular User"; }
elseif($usrlvl == '2') { echo "Moderator"; }
elseif($usrlvl == '3') { echo "Administrator"; }
else { echo "Undefined"; }
}
I just included query into function, and its working :P
<?php
function getUserlevel() {
$userget = mysql_query("SELECT * FROM `users` WHERE `username`='".$_SESSION['username']."'");
$user = mysql_fetch_array($userget);
if($user['user_level'] == 1) { echo "Regular User"; }
elseif($user['user_level'] == 2) { echo "Moderator"; }
elseif($user['user_level'] == 3) { echo "Administrator"; }
else { echo "Error"; }
}
?>
You can not access $user as $user[userlevel] because mysql_fetch_array return an array. Does username is unique if not use an id instead of using username. So you should write the function as below
function getUserlevel()
{
foreach($user as $u)
{
if($u['userlevel'] == 1){echo "Regular User";}
else if($u['userlevel'] == 2){echo "Moderator";}
else if($u['userlevel'] == 3){echo "Administrator";}
else{echo "Undefined";}
}
}
If it does not work make sure you have write a vaild mysql query

Categories