I posted a code below about my website. In this code i want to update rows in my database, if the user changed the name of the topic on the website's form. Everything is working except the sql part. I mean the part where:"LIMIT 1 OFFSET '$x'" this part of the sql code is not good for some reason, but i don't know why. I tested it in xampp phpmyadmin and it works but here something just wrong.
<?php
$sql = "SELECT topicname, username, created, COUNT(commentid)
FROM user, topic, comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ";
$lekerdezes = mysql_query($sql);
$num_rows = mysql_num_rows($lekerdezes); ?>
<?php
if (isset($_POST['delete']))
{
if (!empty($_POST['forumnev']))
{
for ($x = 0; $x < $num_rows; $x++)
{
foreach ($_POST['forumnev'] as $selected)
{
$seged = mysql_query("SELECT created FROM topic WHERE
created IN (SELECT created FROM user, topic, comment WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner AND user.username = '" . $_SESSION['user_name '] . "'
GROUP BY topicname ORDER BY created)
LIMIT 1 OFFSET '$x'");
if (!$seged)
{
echo mysql_error();
}
$seged2 = mysql_fetch_array($seged);
$seged2 = $seged2[0];
if (!$seged2)
{
echo mysql_error();
}
$sql = mysql_query("UPDATE topic SET topicname = '$selected' WHERE created = '$seged2'");
}
}
header("Location: topicedit.php");
}
}
?>
Try updating as follows:(Hope your limit: 1 and offset: $x)
$seged = mysql_query("SELECT created FROM topic WHERE created IN (SELECT created
FROM user,topic,comment
WHERE topic.topicid = comment.whichtopic
AND user.userid = topic.owner
AND user.username = '". $_SESSION['user_name'] ."'
GROUP BY topicname
ORDER BY created)
LIMIT $x, 1");
Related
I am looking to count the number of times 'yes' in present for a user in a table, then post the result into anther table for that same user. Both tables have the username. I would like this done for each user. I have the following but it is not working.
$sql = $item_count = "SELECT SUM(if(strike='yes',1,0)) AS strike_total FROM weekpicks WHERE username = 'username'";
// execute SQL query and get result
$sql_result = mysql_query($sql) or die (mysql_error());
if (!$sql_result) {
echo "Something has gone wrong!";
}
else {
//loop through record and get values
while ($row = mysql_fetch_array($sql_result)) {
$item_result = ($row = #mysql_query($item_count)) or die(mysql_error());
$strike_total = ($row = #mysql_result($item_result,"strike_total"));
$strikes = ($row = $strike_total ['strike_total']);
$username = $row["username"];
// the following will insert number of strikes into table for each user.
$sql = "UPDATE authorize SET strikes = '($strikes)' WHERE username='$username'";
//mysql_query(" UPDATE authorize SET " . "strikes = '" . ($strikes) . "' WHERE username='$username' ");
$result = mysql_query($sql) or die (mysql_error());
Just one query should be enough
Update for single user..
UPDATE authorize SET strikes = (select count(*) from weekpicks WHERE username = '$username' and strike='yes') WHERE username='$username';
For bulk update all users
UPDATE authorize as A SET strikes = (select count(*) from weekpicks B WHERE strike='yes' and A.username=B.username group by B.username)
Isn't that simple.
I am trying to get data from database then fetch it again with different mysql_query using while() in both query , but the problem it is producing results more than one time because i used while in first query . So any answer to get all data without while() for first query ?
$AllFrnd = "SELECT friend , followers FROM frndlist WHERE userid = '".$_SESSION[' user_id ']."' ORDER BY id DESC";
$getfrnd = mysql_query($AllFrnd);
while($frnd = mysql_fetch_array($getfrnd)) {
$query2 = "SELECT * FROM post WHERE (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC";
$rs = mysql_query($query2);
while($row = mysql_fetch_array($rs)) {
echo ''.$row['content'].'';
you can try this :
// Associative array
$frnd =mysql_fetch_array($getfrnd,MYSQL_ASSOC);
then get your data as like :
echo $frnd ["friend"]
echo $frnd ["followers"]
And you should use mysqli_fetch_array instead of mysql_fetch_array
Hope it helps
You can use a query just like this :
SELECT *
FROM post
WHERE userid IN (
SELECT followers
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid IN (
SELECT friend
FROM frndlist
WHERE userid = '" . $_SESSION['user_id'] . "'
)
OR userid = '" . $_SESSION['user_id'] . "'
ORDER BY id DESC
You can easyly handle by using this function
function sel($table,$field="*", $condition="1",$sort="" ){
if($sort!='') $sort="order by $sort ";
//echo "select $field from $table where $condition $sort ";
$sel_query=mysql_query("select $field from $table where $condition $sort ");
//$sel_result=array();
while($temp_res=#mysql_fetch_array($sel_query))
{
$sel_result[]=$temp_res;
}
return isset($sel_result)?$sel_result: 0;
}
while($frnd = mysql_fetch_array($getfrnd)) {
$temp_res=sel("post","*"," (userid ='".$frnd['friend']."') OR (userid = '".$frnd['followers']."') OR (userid = '".$_SESSION[' user_id ']."') ORDER BY id DESC");
if($temp_res)foreach($temp_res as $row){
echo $row['content'];
}
}
I have problem with PHP and MySQL please help..
$lokalita_s = $_POST['lokalita_s'];
$query = "SELECT nazov, lokalita FROM reality WHERE lokalita = '".$lokalita_s."' ORDER BY id";
............
But if ($lokalita_s == "nezáleží")... then i want to select every thing from database..
something like this :
$query = "SELECT nazov, lokalita FROM reality ORDER BY id";
............
This is not working :
$lokalita_s = 0;
$lokalita_s = NULL;
$lokalita_s = *;
I really dont want to use it like if else.. because i want to use more variables in that query and it won't be effective
Try
$lokalita_s = $_POST['lokalita_s'];
$wherClause = null;
if($lokalita_s != "nezáleží") {
$wherClause = "WHERE lokalita = '" . $lokalita_s . "'";
}
$query = "SELECT nazov, lokalita FROM reality $wherClause ORDER BY id";
Something along these lines? (This works in Oracle)
$lokalita_s = $_POST['lokalita_s'];
$query = "
SELECT nazov, lokalita FROM reality WHERE lokalita = '". $lokalita_s."'
UNION
select nazov,lokalita from reality where '". $lokalita_s. "' = 'nezáleží'
order by id
"
I've got a MySQL query based on a database of ship information, this includes a field ship_name and the key ship_id.
I've written a query which uses the current_ship_id of the page, and finds the next ship based on the alphabetical list of ship_names.
This all works fine, HOWEVER, I'm trying to create a link using the below code in the 'IF' statement.
header("Location: shipinfo.php?ship_id=$next_ship_id");
What I don't know how to do is define the variable next_ship_id. I tried the line:
$next_ship_id = ($ship_id);
In theory, I want to get the result of the query $sql (of which I know there is only one result) and find it's ship_id.
How do I do that please?
$sql = " SELECT ship_infomation.ship_id
FROM ship_infomation
INNER JOIN (
SELECT ship_name
FROM ship_infomation
WHERE ship_id = $current_ship_id
) As current_ship
ON ship_infomation.ship_name < current_ship.ship_name
ORDER BY ship_infomation.ship_name ASC
LIMIT 1";
// echo "<br /><br />$sql<br /><br />";
$ships = mysql_query($sql, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
$next_ship_id = ($ship_id);
if ($totalRows_ships = 1)
{
header("Location: shipinfo.php?ship_id=$next_ship_id");
}
else
{
// remain on current page
}
For the next ship use:
SELECT ship_id
FROM ship_infomation
WHERE ship_id = (SELECT min(ship_id)
FROM ship_infomation
WHERE ship_id > $current_ship_id)
LIMIT 1
For the previous ship use:
SELECT ship_id
FROM ship_infomation
WHERE ship_id = (SELECT max(ship_id)
FROM ship_infomation
WHERE ship_id < $current_ship_id)
LIMIT 1
And at your code change this:
$ships = mysql_query($sql, $ships) or die(mysql_error());
$row_ships = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
$next_ship_id = ($ship_id);
if ($totalRows_ships = 1)
{
header("Location: shipinfo.php?ship_id=$next_ship_id");
}
else
{
// remain on current page
}
To this:
$ships = mysql_query($sql, $ships) or die(mysql_error());
$row = mysql_fetch_assoc($ships);
$totalRows_ships = mysql_num_rows($ships);
if ($totalRows_ships = 1)
{
header("Location: shipinfo.php?ship_id=" . $row['ship_id']);
}
else
{
// remain on current page
}
For the next and previous, on your code:
$go = isset($_GET['go']) ? $_GET['go'] : 'next';
if ($go == 'next')
$sql = "SELECT ship_id FROM ship_infomation WHERE ship_id = (SELECT min(ship_id) FROM ship_infomation WHERE ship_id > ". mysql_real_escape_string($current_ship_id) . ") LIMIT 1";
else
$sql = "SELECT ship_id FROM ship_infomation WHERE ship_id = (SELECT max(ship_id) FROM ship_infomation WHERE ship_id < ". mysql_real_escape_string($current_ship_id) . ") LIMIT 1";
And on your URL have it like this for the next ship:
http://mysite.com/ships.php?current_ship_id=15&go=next
And like this for the previous:
http://mysite.com/ships.php?current_ship_id=15&go=previous
If the go is not specified it will go to the previous ship by default.
There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
I am getting a bunch of id's from the database - for each id, I want to do a mysql query to get the relating row in another table. This works fine although I also need to get similiar data for the logged in user. With the mysql query I am using - it duplicates the logged in user data according to how many id's it originally pulled. This makes sense although isnt what I want. I dont want duplicate data for the logged in user and I need the data to come from one query so I can order things with the timestamp.
<?php
mysql_select_db($database_runner, $runner);
$query_friends_status = "SELECT DISTINCT rel2 FROM friends WHERE rel1 = '" . $_SESSION ['logged_in_user'] . "'";
$friends_status = mysql_query($query_friends_status, $runner) or die(mysql_error());
$totalRows_friends_status = mysql_num_rows($friends_status);
while($row_friends_status = mysql_fetch_assoc($friends_status))
{
$friends_id = $row_friends_status['rel2'];
mysql_select_db($database_runner, $runner);
$query_activity = "SELECT * FROM activity WHERE user_id = '$friends_id' OR user_id = '" . $_SESSION['logged_in_user'] . "' ORDER BY `timestamp` DESC LIMIT 15";
$activity = mysql_query($query_activity, $runner) or die(mysql_error());
$totalRows_activity = mysql_num_rows($activity);
echo "stuff here";
}
?>
You can try this single query and see if it does what you need
$userID = $_SESSION['logged_in_user'];
"SELECT * FROM activity WHERE user_id IN (
SELECT DISTINCT rel2 FROM friends
WHERE rel1 = '$userID')
OR user_id = '$userID' ORDER BY `timestamp` DESC LIMIT 15";
You probably want something like this:
$user = $_SESSION['logged_in_user'];
$query_friends_status = "SELECT * FROM friends, activity WHERE activity.user_id = friends.rel2 AND rel1 = '$user' ORDER BY `timestamp` DESC LIMIT 15";
You can replace * with the fields you want but remember to put the table name before the field name like:
table_name.field_name
I hope this helps.
<?php
require_once "connect.php";
$connect = #new mysqli($host, $db_user, $db_password, $db_name);
$result = $connect->query("SELECT p.name, p.user, p.pass, p.pass_date_change,p.email, p.pass_date_email, d.domain_name, d.domain_price, d.domain_end, d.serwer, d.staff, d.positioning, d.media FROM domains d LEFT JOIN persons p
ON d.id_person = p.id AND d.next_staff_id_person != p.id");
if($result->num_rows > 1)
{
while($row = $result->fetch_assoc())
{
echo '<td class="box_small_admin" data-column="Imię i nazwisko"><div class="box_admin">'.$row["name"].'</div></td>';
echo '<td class="box_small_admin" data-column="Domena"><div class="box_admin">'.$row["domain_name"].'</div></td>';
}}
?>