I'm trying to get the data from my database according to the certain id if the database.
Here is my code. But they are not working else
$selectquery,$resultsgetdata,$countprodu
<?php
$profrom = $_GET['id'];
$selectquery = "SELECT * FROM tbl_name WHERE proid = '$profrom'";
$resultsgetdata = mysql_query($selectquery);
$countprodu= mysql_num_rows($resultsgetdata);
if($countprodu>0)
{
$proidid = $row['proid'];
$proidName = $row['proName'];
$proidDescription = $row['proDescription'];
$Category = $row['proCategory'];
$Price = $row['Price'];
$Photo1name = $row['Photo1name'];
}
echo $proidid;
?>
your forgot to fetch the value try this
<?php
$profrom = $_GET['id'];
$selectquery = "SELECT * FROM tbl_name WHERE proid = '$profrom'";
$resultsgetdata = mysql_query($selectquery);
$countprodu= mysql_num_rows($resultsgetdata);
if($countprodu>0)
{
while($row = mysql_fetch_assoc($resultsgetdata){
$proidid = $row['proid'];
$proidName = $row['proName'];
$proidDescription = $row['proDescription'];
$Category = $row['proCategory'];
$Price = $row['Price'];
$Photo1name = $row['Photo1name'];
}
}
?>
You need to use mysql_fetch_assoc().
<?php
$profrom = $_GET['id'];
$selectquery = "SELECT * FROM tbl_name WHERE proid = '$profrom'";
$resultsgetdata = mysql_query($selectquery);
$countprodu= mysql_num_rows($resultsgetdata);
if($countprodu>0)
{
while($row = mysql_fetch_assoc($resultsgetdata))
{
$proidid = $row['proid'];
$proidName = $row['proName'];
$proidDescription = $row['proDescription'];
$Category = $row['proCategory'];
$Price = $row['Price'];
$Photo1name = $row['Photo1name'];
echo $proidid;
}
}
?>
Try this I hope it will work,
<?php
$profrom = $_GET['id'];
$selectquery = "SELECT * FROM tbl_name WHERE proid = '$profrom'";
$resultsgetdata = mysql_query($selectquery);
$countprodu= mysql_num_rows($resultsgetdata);
if($countprodu>0)
{
while($row = mysql_fetch_array($resultsgetdata))
{
$proidid = $row['proid'];
$proidName = $row['proName'];
$proidDescription = $row['proDescription'];
$Category = $row['proCategory'];
$Price = $row['Price'];
$Photo1name = $row['Photo1name'];
echo $proidid;
}
}
?>
Related
How to add the result of the while loop which is stored on the variable $profit? results are 5 and 70
the code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
if($row[0] == null){
$row[0] = 0;
}
}?>
Try this code
<?php
$sql2 = "SELECT * from `products`";
$result2 = $link->query($sql2);
$totalProfit = 0;
while($row2 = $result2->fetch_assoc())
{
$sql3 = "SELECT * from `orders` where product_id = '".$row2['prod_id']."'";
$result3 = $link->query($sql3);
$row3 = $result3->fetch_assoc();
$sql = "SELECT SUM(product_qty) from orders where product_id = '".$row2['prod_id']."'";
$result = $link->query($sql);
$row = mysqli_fetch_array($result);
$res = bcmul($row2['prod_price'], $row[0]);
$profit = $res - $row2['prod_cost'];
$totalProfit = $totalProfit + $profit
if($row[0] == null){
$row[0] = 0;
}
}?>
now you can use $totalProfit variable value as total.
With my code below i have array item with multiple record i which to update each record into database with one query but only the last item of each array record was updated here is my code
:
<?php
require("init.php");
$sql = "SELECT item_name, quantity
FROM books WHERE book = 1644445";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query))
{
$da = $row["item_name"];
$qty = $row["quantity"];
$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
$productCount = mysqli_num_rows($sql);
if($productCount > 0)
{
while ($row = mysqli_fetch_array($sql))
{
$id = $row["id"];
$type = $row["name"];
$code = $row["recharge"];
}
}
$set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$coe=substr(str_shuffle($set), 0, 12);
if(preg_match('/(65265)/i', $type))
$type = "20";
if(preg_match('/(562546)/i', $type))
$type = "13";
if(preg_match('/(MTN)/i', $type))
$type = "12";
if(preg_match('/(56556)/i', $type))
$type = "16";
$disp = str_split($code, $type);
for($b = 0; $b<$qty; $b++){
$pin = "$disp[$b]";
$gam = array(0 => array("post" => $pin));
foreach($gam as $gg)
{
$pp = $gg["post"];
$go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
if($go)
{
echo "<br/> $pp";
echo "<br/> $coe";
}
}
}
}
?>
i appliciate your impact
Try this:
<?php
require("init.php");
$sql = "SELECT item_name, quantity
FROM books WHERE book = 1644445";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_array($query))
{
$da = $row["item_name"];
$qty = $row["quantity"];
$sql = mysqli_query($conn, "SELECT * FROM promo WHERE code = '$da' LIMIT 1");
$productCount = mysqli_num_rows($sql);
if($productCount > 0)
{
while ($row = mysqli_fetch_array($sql))
{
$id = $row["id"];
$type = $row["name"];
$code = $row["recharge"];
}
$set="123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$coe=substr(str_shuffle($set), 0, 12);
if(preg_match('/(65265)/i', $type))
$type = "20";
if(preg_match('/(562546)/i', $type))
$type = "13";
if(preg_match('/(MTN)/i', $type))
$type = "12";
if(preg_match('/(56556)/i', $type))
$type = "16";
$disp = str_split($code, $type);
for($b = 0; $b<$qty; $b++){
$pin = "$disp[$b]";
$gam = array(0 => array("post" => $pin));
foreach($gam as $gg)
{
$pp = $gg["post"];
$go = mysqli_query($conn, "UPDATE promo SET recharge='$coe$pp' WHERE id=$id");
if($go)
{
echo "<br/> $pp";
echo "<br/> $coe";
}
}
}
}
}
?>
The update is only executed if $productCount > 0.
pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out.
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
?>
<?php
}
?>
<?php
echo $id;
echo $body;
?>
Either print the values in while loop or store them in an array to print them outside the loop.
while($row = mysql_fetch_assoc($query)){
echo $row["id"];
echo $row["body"];
}
condb.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
index.php
<?php
include 'database/condb.php';
$query = "SELECT * FROM posts";
$res = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($res)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo $body;
}
?>
For storing data, sometimes using array becomes helpful. I usually recommend it:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
$array[$id] = ['username'=>$username, 'body'=>$body, 'date_added'=>$date_added, 'hasttags'=>$hasttags];
}
?>
<?php
foreach($array as $id => $value){
echo $id; //Prints id
echo "<br>";
echo $value['username'].", ".$value['body'].", ".$value['date_added'].", ".$value['hasttags'];
}
?>
Or you can simply do this:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
?>
<?php
echo $id.", ".$username.", ".$body.", ".$date_added.", ".$hasttags;
}
?>
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo "<br>";
echo $body;
?>
<?php
}
?>
As an answer to the question "pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out."
Put echo $id, echo $body be inside the loop.
I have a number pages namely
change=1.php
change=2.php
change=3.php
They all have similar coding but leaving 1 or 2 variable values.
And I know its a very bad idea! How can I make a link work like below:
change.php?id=1
change.php?id=2
change.php?id=3
http://oi62.tinypic.com/708gfm.jpg
<?php
include 'connection.php';
session_start();
include 'details.php';
/*$pkmn_id = $_SESSION['pkmn_id'];
$poke = $_SESSION['path'];*/
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE team = 1 AND user_id = '".$id."' ");
while($rows = mysql_fetch_array($data))
{
$rep_id = $rows[0];
$pkmn_id = $rows['pkmn_id'];
$path = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
$poke = mysql_result($path, 0, "path");
echo $poke;
echo "<br />";
$level = $rows['level'];
echo $level;
echo "<br />";
$exp = $rows['exp'];
echo $exp;
echo "<br />";
echo "<br />";
}
$data = mysql_query(" SELECT * FROM user_pokemon_db WHERE user_id = '".$id."' AND team = 0");
while($rows = mysql_fetch_assoc($data))
{
$db_id = $rows['id'];
$array[] = $db_id;
$level = $rows['level'];
$array1[] = $level;
$exp = $rows['exp'];
$array2[] = $exp;
$pkmn_id = $rows['pkmn_id'];
$data1 = mysql_query(" SELECT * FROM pokemons WHERE pk_id = '".$pkmn_id."' ");
while($rows = mysql_fetch_assoc($data1))
{
$poke = $rows['path'];
$array3[] = $poke;
}
}
$team = 1;
$_SESSION['team'] = $team;
$_SESSION['rep_id'] = $rep_id;
?>
My PHP code.
You probably want to use GET variables, for which you need to combine all the files into one, named change.php. In this file you need the line $foo = $_GET["id"] which will get the value of the variable "id" in the url change.php?id=1.
if (isset($_GET["id"])) {
$foo = $_GET["id"];
//your code here
}else{
echo 'ERROR!!! No id in URL';
}
You can have several variables in the URL like this: change.php?id=1&a=bar&b=toofoo
You can get current script's file name and parse integer.
__FILE__
gives current script's name. Then,
$myStr = preg_replace('/\.php$/', '', __FILE__);
$result = preg_replace('/change=$/', '', $myStr);
echo $result; // it's your id
Full error:
Parse error: syntax error, unexpected T_VARIABLE in /home/u572186424/public_html/safe.php on line 56
I have been staring at line 56 and cannot figure it out...
exit();
The whole file follows:
<?php
include_once("connect.php");
?>
<?
$sql = "SELECT * FROM users WHERE id='" . mysql_real_escape_string($_SESSION['user_id']) . "'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$username = htmlspecialchars($row->username);
$password = htmlspecialchars($row->password);
$account_type = htmlspecialchars($row->account_type);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$req_exp = htmlspecialchars($row->req_exp);
$level = htmlspecialchars($row->level);
$health = htmlspecialchars($row->health);
$max_health = htmlspecialchars($row->max_health);
$lastactive = htmlspecialchars($row->lastactive);
$energy = htmlspecialchars($row->energy);
$max_energy = htmlspecialchars($row->max_energy);
$will = htmlspecialchars($row->will);
$max_will = htmlspecialchars($row->max_will);
$brave = htmlspecialchars($row->brave);
$max_brave = htmlspecialchars($row->max_brave);
$strength = htmlspecialchars($row->strength);
$agility = htmlspecialchars($row->agility);
$guard = htmlspecialchars($row->guard);
$labor = htmlspecialchars($row->labor);
$iq = htmlspecialchars($row->iq);
$rank = htmlspecialchars($row->rank);
?>
<?php
$sql = "SELECT * FROM sitestats WHERE id='1'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$admins = htmlspecialchars($row->admins);
$mods = htmlspecialchars($row->mods);
$hdo = htmlspecialchars($row->hdo);
$admins_ip = htmlspecialchars($row->admins_ip);
$mods_ip = htmlspecialchars($row->mods_ip);
$admin_array = explode("-", $admins);
$mod_array = explode("-", $mods);
$hdo_array = explode("-", $hdo);
$admin_ip_array = explode("-", $admins_ip);
$mod_ip_array = explode("-", $mods_ip);
?>
<html>
<body>
<?
if(isset($_SESSION['user_id'])) {
$sql = "UPDATE users SET lastactive=NOW() WHERE id='" . mysql_real_escape_string($_SESSION['user_id']) . "'";
mysql_query($sql);
}
else{
header("Location: logout.php");
exit(); // Error here
}
$query = "SELECT account_type,rank FROM users WHERE username= "$username";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['account_type'] == 1){
$row['rank'] = "Player";
$rank = "Player";
}
elseif($row['account_type'] == 2){
$row['rank'] = "VIP";
$rank = "VIP";
}
elseif($row['account_type'] == 3){
$row['rank'] = "HDO";
$rank = "HDO";
}
elseif($row['account_type'] == 4){
$row['rank'] = "Moderator";
$rank = "Moderator";
}
elseif($row['account_type'] == 5){
$row['rank'] = "Admin";
$rank = "Admin";
}
elseif($row['account_type'] == 6){
$row['rank'] = "Owner";
$rank = "Owner";
}
?>
</body>
</html>
How can I fix this problem?
The syntax highlighting shows you. Problem is the extra quote " here:
$query = "SELECT account_type,rank FROM users WHERE username= "$username";
Try:
$query = "SELECT account_type,rank FROM users WHERE username= '$username'";
There are no dots when you concatenate the query and $username:
$query = "SELECT account_type,rank FROM users WHERE username= "$username";