Hi guys how i can delete 2 rows in one query from one table. my code is:
DELETE FROM panel_friends
WHERE friends_friend_id = ' . $k . ' AND friends_member_id = ' . $key2
AND
WHERE friends_friend_id = ' . $k2 . ' AND friends_member_id = ' . $key
DELETE
FROM panel_friends
WHERE (friends_friend_id = :k1 AND friends_member_id = :k2)
OR
(friends_friend_id = :k2 AND friends_member_id = :k1)
Related
I want to run an UPDATE query on a record after selecting it.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select(array('p.*', 'u.id', 'u.name' ,'pr.*'))
->from($db->quoteName('#__chichi_photos', 'p'))
->join('LEFT', $db->quoteName('#__users', 'u') . ' ON (' . $db->quoteName('p.userID') . ' = ' . $db->quoteName('u.id') . ')')
->join('LEFT', $db->quoteName('#__chichi_profile', 'pr') . ' ON (' . $db->quoteName('pr.userID') . ' = ' . $db->quoteName('u.id') . ')')
->where($db->quoteName('p.id') .'=' . $photoID .'');
$db->setQuery($query);
$results = $db->loadRow();
I want to run an UPDATE query on a field in #__chichi_photos table that has an id in the result and if possible do it with a single query statement.
Basically what i want to do is update "hits" column on the field after it is selected. Thank You.
For some reason the only row that is echoing is the user_id column, everything else is just blank. I tried a query with SELECT * and it still wasn't any different. Does anyone know what might be happening? I get the right results as it selects the one user in the table that meets the criteria. The problem is that the name and phone number are not echoing.
$result = mysqli_query($con,"SELECT fname, lname, user_id, phone FROM users WHERE `approved` = 0 AND `user_active` = 1 AND `declined` = 0");
while($row = mysqli_fetch_array($result)) {
echo '<div>' . $row['fname'] . ' ' . $row['lname'] . ' ' . '<a style="text-decoration:none;" href="../lookup/profile?' . $row['user_id'] . '">View Profile Here</a>' . ' ' . $row['phone'] . '</div>';
}
try this
$result = mysqli_query($con,"SELECT fname, lname, user_id, phone FROM users
WHERE `approved` = 0 AND `user_active` = 1 AND `declined` = 0 GROUP BY user_id");
Here is my double-minded query:
$Quest = "SELECT * FROM TOAWorkorders";
$FindTechResult = mysql_query($Quest, $cxn)
or die ('The easter bunny is watching you' . mysql_error());
while ($row = mysql_fetch_array($FindTechResult))
{
if (strpos($BBT, 0, 3) != 'Sys')
{
$IdNum = $row['IdNum'];
$BBT = $row['BBT'];
$BBTArray = explode("-", $BBT);
$TechNum = $BBTArray["0"];
$Title = $BBTArray["2"];
$Name = explode(" ", $BBTArray['1']);
$FirstName = $Name["0"];
$LastName = $Name["1"];
}
echo $TechNum . ' !! ' . $FirstName . ' !! ' . $LastName . ' !! ' . $Title . '<br>';
$Quest = "UPDATE TOAWorkorders SET TechNum = '$TechNum', FirstName = '$FirstName', LastName = '$LastName', Title = '$Title' WHERE IdNum = '$IdNum'";
$result = mysql_query($Quest, $cxn) or die(mysql_error());
}
Everything works for about 2/3s of the database. That leaves 33,000 rows that are not updated. I cannot find any difference between the data that works and the data that doesn't.
Since you're doing an UPDATE, and you say the rest of the code works (meaning, I hope, that you get 109,112 echo'ed results), it must be that the ID isn't being found (WHERE IdNum = '$IdNum').
Try preceding that command with "SELECT COUNT(*) from TOAWorkorders WHERE IdNum = '$IdNum'" and see if you get 33,000 zeros when the program runs. If you do, then you have missing IdNum values in your table.
If you don't, please provide details and I'll let you know.
I'm attempting to modify a mySQL query (that works) to return a more specific result. I've added a variable to the statement so that it looks for jobID AND UserName. Adding the $userName to the statement breaks it.
I've included the code below with the three variations of the SQL statement for comparison. I'm sure it's something obvious - to everyone but me...
Thanks in advance!
DB
// get all applicants from a User
public function GetAllMyApplications($from=false, $to=false, $user_name)
{
global $db;
$applicants = array();
if ($from >= 0 && $to > 0)
{
$sql_limit = ' LIMIT ' . $from .', ' . $to;
}
else
{
$sql_limit = '';
}
$user_name = "Bob Bobberton"; // reset this var for testing
$sql = 'SELECT * FROM '.DB_PREFIX.'job_applications WHERE job_id = '. $this->mJobId . ' ORDER BY name ASC ' . $sql_limit; // This was the original SQL that worked
$sql = 'SELECT * FROM '.DB_PREFIX.'job_applications WHERE job_id = '. $this->mJobId . ' AND name = ' . $user_name . ' ORDER BY name ASC ' . $sql_limit; // Added "and" $user_name - it breaks
$sql = 'SELECT * FROM '.DB_PREFIX.'job_applications WHERE job_id = '. $this->mJobId . ' AND name = "Bob Bobberton" ORDER BY name ASC ' . $sql_limit; // Replace var with value "Bob Bobberton" and it works
$result = $db->query($sql);
while ($row = $result->fetch_assoc())
{
$applicants[] = array('id' => $row['id'],
'job_id' => $row['job_id'],
'name' => $row['name'],
'email_address' => $row['email_address'],
'message' => str_replace(array("\r\n", "\r", "\n"), "<br />", $row['message']),
'resume_path' => base64_encode($row['resume_path']),
'created_on' => $row['created_on'],
'ip' => $row['ip']);
}
if (isset($applicants))
{
return $applicants;
}else{
return("");
}
}
change this
' AND name = ' . $user_name . ' ORDER BY name ASC '
to
" AND name = '" . $user_name . "' ORDER BY name ASC "
and it will work
The solution provided by Satya is not enough. You should escape your inputs properly.
Assume your $username contains a " character. That will break your SQL statement. So you should use prepared statements or, at least, use the function mysql_real_string_escape().
I am asking this question on behalf of a small group of my users that have this problem.
Once the script they are using gets to the 21st ID, it generates the following error:
The SELECT would examine more than
MAX_JOIN_SIZE rows; check your WHERE
and use SET SQL_BIG_SELECTS=1 or SET
SQL_MAX_JOIN_SIZE=# if the SELECT is
okay
I have researched this as much as possible and found something of an answer : http://dev.mysql.com/doc/refman/5.0/en/set-option.html
The problem is that they are on shared hosting so they cannot change their MySQL settings to fix the errors.
Is there anything I can write into my script so that they do not have this problem?
This is the function that generates the database query based on which modules are loaded:
$sql = 'SELECT a.id as id , a.address as address';
$query = 'SELECT'
. ' name AS module_name'
. ', databasename AS module_database'
. ', pregmatch AS module_pregmatch'
. ', pregmatch2 AS module_pregmatch2'
. ', html AS module_html'
. ', sqlselect AS database_sqlselect'
. ', sqljoin AS database_sqljoin'
. ', sqlupdatewithvalue AS database_sqlupdatewithvalue'
. ', sqlupdatenovalue AS database_sqlupdatenovalue'
. ' FROM #__aqsgmeta_modules'
. ' WHERE enabled = 1'
. ' ORDER BY id';
$db->setQuery($query);
$results = $db->loadObjectList();
if (count($results) != 0) {
foreach ($results as $result) {
$sqlselect .= ', ';
$sqlselect .= $result->database_sqlselect;
$sqljoin .= ' ';
$result->database_sqljoin = preg_replace('/\{DATABASENAME\}/Ui', $result->module_database, $result->database_sqljoin);
if (!(preg_match("/" . $result->database_sqljoin . "/Ui", $sqljoin)))
$sqljoin .= $result->database_sqljoin;
}
}
if ($use_sh404sef)
$sqlselect .= ', g.oldurl AS sefurl';
$sql .= $sqlselect;
$sql .= ' FROM #__aqsgmeta_address AS a';
$sql .= $sqljoin;
if ($use_sh404sef)
$sql .= ' LEFT JOIN #__redirection AS g ON g.newurl = a.address';
$sql .=
//. ' WHERE a.id IN (' . $cids . ')'
' WHERE a.id = ' . $id
. ' ORDER BY a.address asc,a.id '
;
$db->setQuery($sql);
$rows = $db->loadObjectList();
MAX_JOIN_SIZE is a safety catch commonly used on the shared hostings.
It won't let you accidentally run long queries which would hang the server.
Issue this command:
SET SQL_BIG_SELECTS = 1
before running the query you know to return lots of values.
The MAX_JOIN_SIZE gets hit when MySQL calculates the Cartesian product of a join, not the actual expected records back. Therefore, if you're joining a massive table to another massive table, this will creep up. Use indexes and views to pare down the possible table hits if it's really that large.
See more here: MySQL - SQL_BIG_SELECTS