I would like to know what the fastest way is to make the following SQL call using PHP. I am using procedural mySQLi.
$dcount = "SELECT max(id) FROM deposits";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(id)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
//second query
$ucount = "SELECT max(int) FROM anothertable";
$result2 = mysqli_query($conn,$ucount);
if (mysqli_num_rows($result2) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(int)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
Is there a way to make the execution faster than like this, maybe to echo the results from both queries after the first if statement?
It just seems rather long to me.
Thanks in advance.
SELECT max(id) as max_id, (SELECT max(int) as max_int FROM anothertable) as max_int
FROM deposits
Not tested, but something like it should work
$dcount = "
SELECT max(id) as max,'deposit' as table_name FROM deposits
UNION
SELECT max(id) as max,'another' as table_name FROM anothertable
";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row["table_name"].":".$row["max"]."\n";
}
} else {
echo '0';
}
SELECT d.max(id) as d_max_id, a.max(int) as a_max_int FROM deposits as d JOIN anothertable as a ON d.id = a.id;
is what you need for multiple tables
$row['d_max_id']
will give you deposits.max(id) now
You have to edit the d.id = a.id accordingly to what you want the two tables to match on
If you cant join try this:
SELECT max(id) as max_id, (SELECT max(int) FROM anothertable) as max_int FROM deposits;
SELECT max(id) as max_id, max(int) as max_int FROM deposits ,anothertable
Related
Hi i'm quite newbie in php language and i need to find same values in DESCRIPTION column in my database table.
id-------DESCRIPTION
1-------Final
2-------Exam
3-------Test
4-------Test
5-------Mid
6-------Quiz
7-------Quiz
As output it needs to be like:
Final
Exam
Test
Test
Mid
Quiz
Quiz
If a value repating just change them style is enough but i'm really don't know how to do it.
<?php
$check = mysqli_query($connection,"SELECT * FROM EXAMS");
while($test=mysqli_fetch_array($check))
{
echo $test["DESCRIPTION"];
}
?>
Use an EXISTS subquery to look if the same value exists for another id:
$result = $connection->query("
SELECT e.id, e.DESCRIPTION, EXISTS(
SELECT *
FROM EXAMS e2
WHERE e2.DESCRIPTION = e.DESCRIPTION
AND e2.id <> e.id
) as is_duplicate
FROM EXAMS e
ORDER BY e.id
");
Then check in PHP if it is a duplicate (if ($row['is_duplicate'] == 1)) and mark it bold:
while($row = $result->fetch_assoc())
{
if ($row['is_duplicate'] == 1) {
echo "<strong>$row['DESCRIPTION']</strong><br>";
} else {
echo "$row['DESCRIPTION']<br>";
}
}
PHP solution
$result = $connection->query("SELECT * FROM EXAMS");
$data = $result->fetch_all(MYSQLI_ASSOC);
$counts = array_count_values(array_column($data, 'DESCRIPTION'));
foreach($data as $row)
{
if ($counts[$row['DESCRIPTION']] > 1) {
echo "<strong>$row['DESCRIPTION']</strong><br>";
} else {
echo "$row['DESCRIPTION']<br>";
}
}
I need some help. The database is getting hammered with queries and I think this might be one of them. How can I run this only one time so it does not keep running every time the page loads?
$siteqry_rs = mysql_query($siteqry);
if (mysql_num_rows($siteqry_rs) > 0) {
while ($siters = mysql_fetch_array($siteqry_rs)) {
$tourId = $siters["Id"];
//****************** end old *******
$qry = "SELECT contentgroup.Id as
setid,contentgroup.Directory,contentgroup.Title,
contentgroup.extrafields_PHP,
DATE_FORMAT(contentgroup.AppearDate,'%d-%m-%Y') AS
add_date,DATE_FORMAT(contentgroup.AppearDate,'%m-%d-%Y') AS
add_date_format,contentgroup.SEOname AS setseoname,
contentgroup.PreviewXML_PHP ,contentgroup.Description,
SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT
plg_contentasc.ModelName),',',3) AS ModelName,
SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT
plg_contentasc.SEOname),',',3) AS modelseoname,
SUBSTRING_INDEX(GROUP_CONCAT( DISTINCT
plg_contentasc.Id),',',3) AS ModelId,
contentgroup.Id,
IF(tbl_set_top_rate.num_views IS
NULL,0,tbl_set_top_rate.num_views) AS setrating
FROM contentgroup INNER JOIN plg_contentascasc ON
contentgroup.Id=plg_contentascasc.ContentId
INNER JOIN plg_contentasc ON
plg_contentasc.Id=plg_contentascasc.ModelId
LEFT JOIN tbl_set_top_rate ON
contentgroup.Id=tbl_set_top_rate.set_id
INNER JOIN sites_contentgroup ON
sites_contentgroup.contentgroup=contentgroup.Id
WHERE contentgroup.websiteid='$websiteid' AND
sites_contentgroup.siteid='" . $tourId . "' AND (contentgroup.AppearDate
BETWEEN '2015-01-01' AND CURRENT_DATE)
GROUP BY contentgroup.Id
ORDER BY contentgroup.AppearDate DESC
limit 0,4";
$small = 0;
$count = 0;
$resRated = mysql_query($qry);
$show_li_counter = 0;
$shoimgcounter = 0;
if (mysql_num_rows($resRated) > 0) {
while ($mainArray = mysql_fetch_array($resRated)) {
$rowRated[] = $mainArray;
}
}
}
}
I want to run this only one time.
Use Session to store and validate your data like below
if(empty($_SESSION['mydata'])){
//your query and prepare the result into $resultArray
$_SESSION['mydata'] = $resultArray;
}
How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;
How can I select data in the same query from two different databases into the same server?
This is what I'm doing, but my query doesn't works:
$sqlquery = "SELECT * FROM database_2.table_2 WHERE database_1.table_1.data_1 LIKE database_2.table_2.data_2";
$result = mysql_query($sqlquery);
$number = mysql_numrows($result);
$i = 0;
if ($number < 1) {
print "DOH";
}else{
while ($number > $i) {
$content = mysql_result($result,$i,"database_2.table_2.data_3");
print "$content";
$i++;
}
}
The problem is not about different databases.
Your WHERE clause references the field database_1.table_1.data_1 which was not supplied in the FROM clause.
Didn't you mean something like
SELECT *
FROM database_2.table_2
JOIN database_1.table_1
ON (database_2.table_2.some_field = database_1.table_1.some_other_field)
WHERE database_1.table_1.data_1 LIKE database_2.table_2.data_2
?
Also,
echo mysql_error();
after your failed query - this will give you a clue about what's wrong.
isn't numrows underscored?
$number = mysql_num_rows($result);
http://php.net/manual/en/function.mysql-num-rows.php
try this
SELECT * FROM database_2.table_2 t2 INNER JOIN database_1.table_1 t1
ON t1.data_1 = t2.data_2
So, I am wondering how I can compare the result of one query to the result of another query in a if-statement. Like this:
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
if($team==$lplayer){
//Do something
}
else{
//Do something else
}
This does not work... Why?
Now, why doesnt this work:
$tleague = mysql_query("SELECT teamId from team
WHERE leagueId=(SELECT leagueId FROM league WHERE leagueName='$leagueName')");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId']){}
else{}
You need to use mysql_fetch_assoc() on the query, and compare the returned values. Something like the below. What you're comparing is the two returned resource objects:
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$t = mysql_fetch_assoc($team);
$p = mysql_fetch_assoc($tplayer);
if($t['teamId'] ==$p['teamId']){
//Do something
}
else{
//Do something else
}
However, you shouldn't be using mysql_* methods, instead look at using MySQLi // Tutorial.
You are not fetching any result from queries. Do something like
$team_result = mysql_fetch_array($team);
$tplayer_result = mysql_fetch_array($tplayer);
Then use fetched result to make your if condition
if($team_result['teamId'] == $tplayer_result['teamId'])
{
//do something
}
Also please stop using mysql as it is deprecated, switch to PDO or mysqli for new projects
Update
The new query have mistake. Why don't you use a join
$tleague = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` WHERE b.`leagueName` = '$leagueName'");
$tplayer = mysql_query("SELECT `teamId` FROM `player` WHERE `playerName`='$playerName'");
$row1 = mysql_fetch_array($tleague);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId'])
{
// do something
}
else
{
// do something else
}
UPDATED AGAIN
I merged all queries in one and i encapsuled data in the query '".$playerName."' and '".$leagueName."'
$query = mysql_query("SELECT a.`teamId` from `team` a LEFT JOIN `league` b ON a.`leagueId` = b.`leagueId` LEFT JOIN `player` c ON b.`teamId` = c.`teamId` WHERE b.`leagueName` = '".$leagueName."' and c.`playerName`= '".$playerName."'");
if($row = mysql_fetch_array($query))
{
echo 'Found: ' . $row['teamId'];
}
else
{
echo 'Not Found.';
}
It does not work, because it's not the result of the query. You need to pass to a variable the mysql_result i.e.:
$result1 = mysql_result($team, 0);
$result2 = mysql_result($tplayer, 0);
if ($result == $result2) { ...
Try like this
$teamQuery = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$lplayerQuery = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$team = mysql_fetch_assoc($teamQuery);
$lplayer = mysql_fetch_assoc($lplayerQuery);
if($team['teamId']==$lplayer['teamId']){
//Do something
}
else{
//Do something else
}
do it like that
$team = mysql_query("SELECT teamId FROM team WHERE teamName='$teamName'");
$tplayer = mysql_query("SELECT teamId FROM player WHERE playerName='$playerName'");
$row1 = mysql_fetch_array($team);
$row2 = mysql_fetch_array($tplayer);
if($row1['teamId']==$row2['teamId']){
//Do something
}
else{
//Do something else
}
EDIT:
on your second edit your problem is in the query itself .
try this one
$tleague = mysql_query("SELECT t.teamId from team inner join league l On t.leagueId = l.leagueId
WHERE t.leagueName='".$leagueName."' ");