Jump to content

Recommended Posts

This question is too ambiguous, it's really related to the data the query's bringing back.  Look at these two queries:

SELECT id FROM table_name WHERE unique_value = 1 LIMIT 1

SELECT * FROM table_name

Now imagine that my table has 45 million rows in it 20 columns.  The first piece of code is going to execute relatively quickly and is returning one piece of information, the second will take a very long time as it's trying to return 900 million pieces of information.

I don't see that there's a problem with having lots of calls to mysql_query() on a page, but try to keep your code efficient.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/29081-mysql_query/#findComment-133334
Share on other sites

Instead of selecting from 7 differnet tables, you could try to merge statments.

Example:

Lets say you have a table accounts and a table users and accounts is id name password and users is id accountid and username.
Lets also say you are pulling the id from accounts in a query then pulling everything from users for that id.  Instead, you could do:
SELECT * FROM accounts,users where accounts.id = '{$id} AND users.accountid = accounts.id
Link to comment
https://forums.phpfreaks.com/topic/29081-mysql_query/#findComment-133802
Share on other sites

OK, so do a table join, I'm just starting to learn that, and would like to try it.


Well... Here is what I currently have (the bottom portion).

[CODE]
<div class="sub_content">
<h2>Friends</h2>
<div class="user_content">
<?php
$sqlgetnfriends = mysql_query("SELECT * FROM friends WHERE status='1' AND (friend_id='$_SESSION[userid]' OR owner_id='$_SESSION[userid]')");
$num_friends_row = mysql_num_rows($sqlgetnfriends);
$sqlget = mysql_query("SELECT * FROM friends WHERE status='1' AND
(friend_id='$_SESSION[userid]' OR owner_id='$_SESSION[userid]') LIMIT 28");
if(!$num_friends_row){
echo'No Friends Yet.';
}else{
echo'<h5>'.$num_friends_row.' Friends</h5>';
}
echo'<table>';
echo'<tr>'."\n";
$display = 0;
for($i=0;$i<4;$i++){
while($check=mysql_fetch_array($sqlget)){
if($check['owner_id'] == $_SESSION['userid']){
$friends_sql = mysql_query("SELECT * FROM friends WHERE status='1' AND friend_id='$check[friend_id]'")or die(mysql_error());
$user = 1;
}else{
$friends_sql = mysql_query("SELECT * FROM friends WHERE status='1' AND owner_id='$check[owner_id]'")or die(mysql_error());
$user = 2;
}
$friends_row = mysql_fetch_array($friends_sql);

if($user==1){
$friends_rows_sql = mysql_query("SELECT * FROM users WHERE user_id='$friends_row[friend_id]'")or die(mysql_error());
}else{
$friends_rows_sql = mysql_query("SELECT * FROM users WHERE user_id='$friends_row[owner_id]'")or die(mysql_error());
}
$friends_rows_row = mysql_fetch_array($friends_rows_sql);

if($user==1){
$friends_rows_default = mysql_query("SELECT * FROM default_images WHERE owner_id='$friends_row[friend_id]'")or die(mysql_error());
}else{
$friends_rows_default = mysql_query("SELECT * FROM default_images WHERE owner_id='$friends_row[owner_id]'")or die(mysql_error());
}
$friends_rows_default = mysql_fetch_array($friends_rows_default);

if(!$friends_rows_default && $friends_rows_row['gender']=="male"){
echo'<td style="text-align:center"><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'"><img style="margin-top:2px;" src="images/m_no_photo90.gif"></a>'."\n";
echo'<br><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'">'.$friends_rows_row['first'].'</a></td>'."\n";

}elseif(!$friends_rows_default && $friends_rows_row['gender']=="female"){
echo'<td style="text-align:center"><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'"><img style="margin-top:2px;" src="images/f_no_photo90.gif"></a>'."\n";
echo'<br><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'">'.$friends_rows_row['first'].'</a></td>'."\n";
}else{
echo'<td style="text-align:center"><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'"><img style="margin-top:2px;" src="user_images/mini/'.$friends_rows_default['file_name'].'"></a>'."\n";
echo'<br><a href="user_profile.php?user_id='.$friends_rows_row['user_id'].'">'.$friends_rows_row['first'].'</a></td>'."\n";
}
$display++;
}
echo'<tr>';
}
echo'</table>';
?>
</div>
</div>[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/29081-mysql_query/#findComment-133807
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.