Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 6th, 2008, 12:47 PM
Newbie
 
Join Date: Aug 2008
Posts: 9
Question Displaying SQL Results

Hi I have made a database with all different things (venue, team names etc), the team names display fine, but I am using different code for venues (counting) and these just won't display at all, I am really new to SQL so don't know what to do to fix it!

The code for getting the SQL is

$sql = 'select venue,count(venue) as frequency from matches group by venue limit 300';

which works fine in phpMyAdmin and displays what I want to, but I don't know how to display it on my page! I do have the sql connection info in the php page too, plus the code which is used for displaying all my other pages but doesn't want to work now! I would like a table with headers of Venue & Number if possible, then the venues and number next to them, as it displays in phpMyAdmin

Thanks
Zack :)
Reply
  #2  
Old August 6th, 2008, 07:32 PM
samikhan83's Avatar
Newbie
 
Join Date: Sep 2007
Posts: 31
Default

hi zack ....

be clear.... with ur question
and wat u want to retrive from ur table ......
Reply
  #3  
Old August 7th, 2008, 01:04 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,783
Default

Hi.

So the problem is that you don't know how to execute the query and display the results in your PHP code?

If so, then you should check out the PHP manual. This topic is well covered in the MySQL section.
Check out the examples for mysql_connect and mysql_query.
Reply
  #4  
Old August 7th, 2008, 06:18 PM
Newbie
 
Join Date: Aug 2008
Posts: 9
Default

Thanks, I got it working anyway in the end, part of it didn't copy.

This is part of my code
Expand|Select|Wrap|Line Numbers
  1. if (!$result) {
  2.     $message  = 'Invalid query: ' . mysql_error() . "\n";
  3.     $message .= 'Whole query: ' . $query;
  4.     die($message);
  5. }
  6.  
  7. while ($row = mysql_fetch_assoc($result)) {
  8. echo "<tr><td> ".$row['venue'] . "</td>"; 
  9. echo "<td> ".$row['frequency'] . "</td></tr>"; 
  10. }
  11.  
I have added a hyperlink just before venue, but as there are spaces in them only the first part is included in the link (e.g. for Old Trafford only "Old" is included, as there are a lot with "The" this won't work as a solution). Is there any way to take the spaces out for the link (or changes them to %20), but not for the actual listing, so the link is OldTrafford and the display is Old Trafford? That way I can make a new window open with the team names in. I would like a Javascript style box when you highlight over the text (like this forum for adverts) but they don't seem to let each link be customised. I used something I did before but it doesn't seem to work

Thanks
Zack
Reply
  #5  
Old August 7th, 2008, 08:03 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,783
Default

You can use urlencode to make sure your text will pass safely through the URL.
Expand|Select|Wrap|Line Numbers
  1. $text = "Old Trafford"
  2. $url = urlencode($text);
  3.  
  4. echo '<a href="details.php?v="'. $url .'">'. $text .'</a>';
  5.  
Which should link to "details.php?v=Old%20Trafford".

On the receiving side, however, fetching the value through the $_GET super-global should return the original version with the space.
Reply
  #6  
Old August 7th, 2008, 09:14 PM
Newbie
 
Join Date: Aug 2008
Posts: 9
Default

Thanks, I thought I was doing this right, but obviously not!

I'm getting "Parse error: syntax error, unexpected T_VARIABLE in /home/updates/public_html/matches/grounds.php on line 16"

If I make the "" into just " then line 15 has an error instead!

Expand|Select|Wrap|Line Numbers
  1. <div align=\"left\"><table border=\"1\" bordercolorlight=\"#000000\" bordercolordark=\"#000000\"><tr><td>Venue</td><td>Times Visited</td></tr>
  2. <?php
  3. mysql_connect("localhost", "", "") or die(mysql_error()); 
  4. mysql_select_db("") or die(mysql_error()); 
  5. $query = sprintf("select venue,count(venue) as frequency from matches group by venue ORDER BY `frequency` DESC limit 300");
  6. $result = mysql_query($query);
  7.  
  8. if (!$result) {
  9.     $message  = 'Invalid query: ' . mysql_error() . "\n";
  10.     $message .= 'Whole query: ' . $query;
  11.     die($message);
  12. }
  13.  
  14. while ($row = mysql_fetch_assoc($result)) {
  15. $text = "".$row['venue'] . ""
  16. $url = urlencode($text);
  17. echo "<tr><td>echo '<a href="details.php?v="'. $url .'">".$row['venue'] . "</a>';</td>"; 
  18. echo "<td> ".$row['frequency'] . "</td></tr>"; 
  19. }
  20.  
  21. mysql_free_result($result);
  22. ?>
  23. </table></div>
Reply
  #7  
Old August 7th, 2008, 11:07 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,783
Default

Line 15 is missing the end semi-colon.
And you don't need the "". $var .""; thing.
Expand|Select|Wrap|Line Numbers
  1. // This
  2. $var = "". $var1 ."";
  3. // And this
  4. $var = "$var1";
  5.  
  6. // Works exactly like
  7. $var = $var1;
  8.  
The reason I used it is because I was adding the variable to a single-quoted string.

And line 17 is all messed up. Your quotes don't add up, you have an echo command inside the string, and there is an extra </td> after you close the line.
Reply
  #8  
Old August 8th, 2008, 12:13 AM
Newbie
 
Join Date: Aug 2008
Posts: 9
Default

Thanks I missed that with line 15 that works now, just line 17 causing problems. It was partly what I copied off here. I am using dreamweaver trail which is useful for highlighting when things go wrong (unless Cpanel where it just looks like one big mess), but I can't get 17 to work, have tried lots of different ways and just get

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/updates/public_html/matches/grounds.php on line 17

I don't see what's wrong with this, nor does dreamweaver :s
Expand|Select|Wrap|Line Numbers
  1. echo "<tr><td><a href="details.php?v='. $url .'>".$row['venue'] ."</a></td>"; 
after all this I have probably made some kind of mistake which means I won't be able to have each venue linked differently using this code anyway! As you can tell I haven't got a clue with php!

Zack
Reply
  #9  
Old August 8th, 2008, 03:14 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Age: 22
Posts: 2,783
Default

The problem there are all the mismatching quote-tags.

If you open a string using a double-quote, PHP will close the string on the next double-quote. Same for single-quotes.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. $var = "<a href="page.php">Linkage</a>";
  2.  
You see the problem there?
PHP will open a string on the first double-quote, but then close it on the second, which is meant to be a part of the string, not the end of it.
So, the following text (page.php"...) will be executes as PHP code, which will fail with a parse error.

If you want to use double-quotes inside a string, the string must either start and end with a single-quote, or the extra quotes need to be escaped.
Like so:
Expand|Select|Wrap|Line Numbers
  1. $var = '<a href="page.php">Linkage</a>';
  2. $var = "<a href=\"page.php\">Linkage</a>";
  3.  
Both of these will work.

Now, when you add variables, like you do with your line, you need to close the string and add it using a dot, and then open it again. Like:
Expand|Select|Wrap|Line Numbers
  1. $var = '<a href="'. $url .'">'. $label .'</url>';
  2.  
Here the double-quotes are a part of the string, and all the single-quotes either close or open a string.

You can inject variables directly into a double-quote string, but lets no complicate the matter further.
Reply
  #10  
Old August 9th, 2008, 12:45 AM
Newbie
 
Join Date: Aug 2008
Posts: 9
Default

Thanks, understand that but have never really understood other examples so have just messed about until it's worked!

Thanks it's all working now, just one other thing but I will start a new topic when I get round to doing it, bit too much to tag onto this topic
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles