nested if loop partially working - php

What I am trying to do is simply display the row values. Now suppose if the field 'head_office' dont have the value 'H.O' then I want to display the values of the last row. I tried but cant find any solution. Here is my code: (I have only blocked the php part)
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else{
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>

<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_password = '123';
$mysql_database = 'sdbms';
$setup_page = './myinstitute.php';
$db = mysql_connect($mysql_host, $mysql_user, $mysql_password);
mysql_select_db($mysql_database, $db);
$row = array();
if(isset($_REQUEST['id'])) {
$id = (int) $_REQUEST['id'];
if(!empty($id)) {
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
} else {
$sql = 'SELECT * FROM institute WHERE head_office = "H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(!isset($_REQUEST['id']) && empty($row))
$sql = "SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n == 0) {
header('Location: ' . $setup_page);
} else {
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
?>

As $_REQUEST['id'] can only have 2 status, isset and !isset, the else statement will never be used.

I don't understand very well how do you want to do, but it's illogic: the three step don't execute ever. Try it:
if(isset($_REQUEST['id'])){
$id=$_REQUEST['id'];
$sql = "SELECT * FROM institute WHERE id =$id";
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
else if(!isset($_REQUEST['id'])){
$sql = 'SELECT * FROM institute WHERE head_office ="H.O"';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
if(count($row)<=0) {
$sql="SELECT * FROM institute";
$result = mysql_query($sql, $db);
$n = mysql_num_rows($result); //counting number of rows
if($n==0){
header('Location: '.$setup_page);
}
else{
$sql = 'SELECT * FROM institute ORDER BY id DESC LIMIT 1';
$result = mysql_query($sql, $db);
$row = mysql_fetch_array($result);
}
}
Enjoy your code.

Related

PHP for Looping MySql id

I am Using Below Code to First Fetch all 'id' from MySql table.
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "**";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$slug = $_GET["category"];
$sql = "SELECT * FROM table WHERE category = '1'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$dealid = $row["dealid"];
}} else {}
$conn->close();
?>
$dealid should return with all id's but it is returning with only 1.
Now below code is to show data with those id's:-
<?php
$num_rec_per_page=52;
mysql_connect('localhost','**','**');
mysql_select_db('**');
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $num_rec_per_page;
$sql = "SELECT * FROM table2 WHERE id = $dealid ORDER BY id DESC LIMIT $start_from, $num_rec_per_page";
$rs_result = mysql_query ($sql);
while ($row = mysql_fetch_assoc($rs_result)) {
?>
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/includes/dealbox.php' ); ?>
<?php
};
?>
But its only showing 1 data because returning id is only 1. I am not able to understand what the issue is. Any help is appreciable and will gift if someone help me out with this issue.
Dear friend all Id's are set into one array and return that array,
eg:
while ($row = mysql_fetch_assoc($rs_result))
{
$dealid[]= $row['id']; //array creation
}
and
return ($dealid);

android - php fetch mysql data by user id

This my PHP URL for fetching the data from MySQL. I need to make mysqli_fetch_array code saying if filled uid in the app-data table is the same with uid in users table fetch the data from all row in app-data to the uid like every user show his items from app-data table.
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
I think this is the correct answer:
<?php
$host = "";
$user = "";
$pwd = "";
$db = "";
$con = mysqli_connect($host, $user, $pwd, $db);
if(mysqli_connect_errno($con)) {
die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM app_data WHERE u_id=".$_POST['posted_uid']." ORDER By id";
$result = mysqli_query($con, $sql);
$rows = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$rows[] = $row;
}
mysqli_close($con);
echo json_encode($rows);
Little tip for the future:
Don't search exactly what you want, only search in parts.

to retrieve a mysql data in php and echo the retrieved data

<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
mysql_close();
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
?>
This is my code i want to display the roll number of the currently logged in user and
when i run this code i get no database selected.
First of all, you should code properly, means database connection and database selection should be on top:
<?php
$username = "root";
$password = "password";
$database = "xxxx";
$link = mysql_connect("localhost", $username, $password);
mysql_select_db('xxxx', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
Also moved mysql_close(); on last
One other main point was, now mysql_ is deprecated, please use mysqli_
Remove this statement from line number 10
mysql_close();
just remove these two lines before while that will solve ur problem
$result = mysql_query($query) or die(mysql_error());
$rows = array();
Use this code and use mysql_close function in the last.
<?php
$username = "root";
$password = "password";
$database = "xxxxxx";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db($database, $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
print_r($rows);
mysql_close();
?>
You have closed the connection from MySQL before the mysql_query mysql_close();
try this
<?php
$username = "root";
$password = "password";
$database = "dfsdftwsdgdfgdfsgsdf";
$link = mysql_connect("localhost", $username, $password);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('xxxxxxx', $link);
$result = mysql_query($query) or die(mysql_error($link));
$num = mysql_num_rows($result);
$rows = array();
$result = mysql_query($query) or die(mysql_error());
$rows = array();
while($r = mysql_fetch_row($result))
{
$rows[] = $r[0];
}
mysql_close();
print_r($rows);
?>
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
The order should be like this
mysql_select_db('meipolytechnic', $link);
$query = "SELECT rollno FROM users where username = '".$_SESSION['MM_Username']."'";
mysql_select_db('meipolytechnic', $link);

PHP script returning JSON not working

The below returns "\nQuery was empty" as I run it simply from my server with the URL in the Browser.
This is the PHP code:
<?
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = file_get_contents("php://input");
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
No need to use file_get_content and you have to put one white space after table name.
<?php
$databasehost = "server";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";
$query = "SELECT * FROM `Tailor` LIMIT 0 , 30";
$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$sth = mysql_query($query);
if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
Your query has a problem you need whitespace after tablename
SELECT * FROM `Tailor` LIMIT 0 , 30
first
$query = "SELECT * FROM `Tailor`LIMIT 0 , 30";
but you overwrite it
$query = file_get_contents("php://input");
// $query like aaa=ggg&bbb=kkk
then you
$sth = mysql_query($query);
// LIKE $sth = mysql_query('aaa=bbb&ccc=lll'); //it not sql query format

mysql_connect to PDO connection

I've been trying to convert a mysql_connect connection into a PDO connection with no success, here is what I have:
$host = 'localhost';
$user = 'root';
$pwd = '';
$db = 'jdlferreira';
$connection = mysql_connect($host, $user, $pwd) or die("Could not connect");
mysql_select_db($db) or die("Could not select database");
$query = "SELECT COUNT(*) FROM blog";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
$pages->paginate();
$query = "SELECT id, title, resume, date
FROM blog
ORDER BY date DESC $pages->limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
//do stuff
}
And what I tried to do with PDO:
include_once 'inc/db.inc.php';
$db = new PDO(DB_INFO, DB_USER, DB_PASS);
mysql_select_db("jdlferreira") or die("Could not select database");
$query = "SELECT COUNT(*) FROM blog";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;
$pages->items_total = $num_rows[0];
$pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
$pages->paginate();
$query = "SELECT id, title, resume, date
FROM blog
ORDER BY date DESC $pages->limit";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_row($result)) {
//do stuff
}
I'm getting a "Could not select database" error, I don't really care for the 'or die' cases, I would just like to make this connection functional on PDO, any help would be great.
You cant use PDO and then exepect to use mysql_* functions they arent related.
Theres no need to select a db like that with pdo because its included in the DSN which is the contructors first argument:
$db = new PDO('mysql:host=localhost;dbname=jdlferreira', DB_USER, DB_PASS);
Then you need to use the PDO interface to interact with the DB, not the mysql ones:
$stmt = $db->prepare("SELECT COUNT(*) FROM blog");
$stmt->execute();
$num_rows = $stmt->fetchColumn();
$stmt->closeCursor();
$pages = new Paginator;
$pages->items_total = $num_rows;
$pages->mid_range = 9; // Number of pages to display. Must be odd and > 3
$pages->paginate();
$query = "SELECT id, title, resume, date
FROM blog
ORDER BY date DESC $pages->limit";
$stmt = $db->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
// do stuff
}

Categories