Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 02-06-2007, 04:47 AM
KingPuyol KingPuyol is offline
 
Join Date: Oct 2006
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Showing MySQL rows in a PHP file

How can I show MySQL rows in a PHP file?

Like if I have a table called "coders" and in it I have "coderid", "codername", and "codercountry"

and the values are:
1 - John - USA
2 - Mary - England
3 - Jordi - Spain
etc etc.

I want to show ALL THE ROWS in the following format:

<a href="coder.php?show="'$coderid'">'.$codername.' ('.$codercountry.')</a>
Or whatever way you can write it.

So I want the HTML output to be:
Code:
<a href="coder.php?show=1">John (USA)</a>
<a href="coder.php?show=2">Mary (England)</a>
<a href="coder.php?show=3">Jordi (Spain)</a>
So who can this be done, I want to show ALL THE ROWS.
Reply With Quote
  #2  
Old 02-06-2007, 05:32 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm presuming you're using the vBulletin backend in the following example.
PHP Code:
// cache data
$tabledata $vbulletin->db->query_read("SELECT * FROM `coders`");
$tabledata $vbulletin->db->fetch_array($tabledata);

// display rows
foreach ($tabledata AS $row)
{
echo 
'<a href="coder.php?show=' $row['coderid'] . '">' $row['codername'] . '  (' $row['codercountry'] . ')</a>';

Reply With Quote
  #3  
Old 02-06-2007, 10:42 AM
KingPuyol KingPuyol is offline
 
Join Date: Oct 2006
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks but can you show me something without the vBulletin backend, I understood how to do it but if you can show me another example without the vBulletin backend then it will be much easier.
Reply With Quote
  #4  
Old 02-06-2007, 02:23 PM
firstrebel's Avatar
firstrebel firstrebel is offline
 
Join Date: Dec 2005
Location: West London
Posts: 380
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
<?php

// change hostname, username and password, dbname

if(!isset($query) || empty($query))
	($query= "select coderid,codername,codercountry from coders order by coderid asc");	
	$query = stripslashes($query);
	$db_link = mysql_connect('hostname', 'username', 'password')
  		or die("Could not connect to database. Please try again later.");
	mysql_select_db('dbname') or 
		die("could not connect to database");
	$result = mysql_query($query) or	
		die( mysql_error() );
	$number_cols = mysql_num_fields($result);
	
	echo "<table width=740  border=1 align=center cellspacing=0 cellpadding=5>\n";
	echo "<tr align=center>\n"; 
	echo "<font face=arial>";
	echo "<font size=-1>";	
	for ($i=0; $i<$number_cols; $i++)
	{
		echo "<th>" .mysql_fieldname($result, $i). "</th>\n"; 
	}
	echo "</tr>\n";
	
	while ($row = mysql_fetch_row($result))
	{
		echo "<tr align=left>\n";

		for ($i=0; $i<$number_cols; $i++)
		{
			echo "<td>";
			echo "<font face=verdana>";
			echo "<font size=-1>";		
			if (!isset($row[$i]))
				{echo "n.a.";}
			else 
				{echo $row[$i];}
			echo "</td>\n"; 
		}
		echo "</tr>\n";
	}
	echo "</table>";
	echo "</font>";
      mysql_close($db_link);
?>
Reply With Quote
  #5  
Old 02-07-2007, 09:00 AM
KingPuyol KingPuyol is offline
 
Join Date: Oct 2006
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks and can you please explain what is $i and can you also tell me:
1- How can I limit the number of rows to show, for example: showing the last 20 rows.
2- Paginating rows, for example, showing first 20 rows and then the next 20 in the other. etc..
3- If one of the fields that I have is empty, for example let's say the table is called table and the column which is going to be empty is called game. How can I select the empty one:
Here's the table:
Code:
id----  player      ---- game
1 ----  Andrew    ---- 5
2 ----  Someone  ---- 3
3 ----  King          ---- NULL*
4 ----  MOnkey    ---- NULL
I meant by NULL that it's EMPTY, nothing is in it, so if I wanted to arrange all this in Ascending Order by ID and I want it to select the FIRST row where the game field is NULL.
Reply With Quote
  #6  
Old 02-07-2007, 08:31 PM
firstrebel's Avatar
firstrebel firstrebel is offline
 
Join Date: Dec 2005
Location: West London
Posts: 380
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

To limit the return you would use LIMIT is the SELECT statement
Code:
$query= "SELECT coderid,codername,codercountry from coders order by coderid asc LIMIT 20"
This would return the last 20 in an ascending order. To get the last 20 in descending order change asc LIMIT 20 to desc LIMIT 20.

To get a specific range you would use asc LIMIT 1,20 which would give rows 1-20, or asc LIMIT 20,40 would give rows 20-40. Play around until you get what you want by changing asc and desc and the LIMIT parameters.

I don't have a pagination script in working order at the moment, it is something I need to get working on one of my dbs that has around 2200 rows. I have a script but not integrated into the rest of the script.

In the SELECT statement you specify all the fields you want called, whether they have data in them or not. Any that are empty will just have an empty cell in the row.

Try this as your select statement from your example where the table fields are id, player, game:

($query= "select id, player, game from tablename order by id asc");

Change tablename to whatever it is in the db.

Bob
Reply With Quote
  #7  
Old 02-08-2007, 06:19 AM
KingPuyol KingPuyol is offline
 
Join Date: Oct 2006
Posts: 48
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks, I really appreciate it.
Reply With Quote
Reply


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 On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:05 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03951 seconds
  • Memory Usage 2,226KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (4)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete