printf and the "%" symbol

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

printf and the "%" symbol

Post by Steveo31 »

I finally got a mySQL query to work, woohoo, but then they had to throw a monkey wrench in there with the printf function. Here's the code:

Code: Select all

<?php

$db = mysql_connect("localhost", "root");

mysql_select_db("user",$db);
$result = mysql_query("SELECT * FROM employees",$db);

for($i=0;$i<3;$i++){
	printf("First Name:  %s<br>\n", mysql_result($result, $i, "first"));
	printf("Last Name:  %s<br>\n", mysql_result($result, $i, "last"));
	printf("Address:  %s<br>\n", mysql_result($result, $i, "address"));
	printf("Position:  %s<br>\n", mysql_result($result, $i, "position"));
	echo "<br>";
}

?>
The for loop was my doing, it actually just had each printf function for the data. My question to you is this... what is the % symbol used for? The tut said something about a variable-somethin in Perl/C. What does the printf function do that echo can't? I see that there are 2 arguements, but what are they?

I looked at php.net....

-S
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

The 'f' in printf means 'formatted'.
http://www.php.net/manual/en/function.sprintf.php gives details of the formatting characters.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

That's gonna take some studying. Thanks for the link though, very handy indeed.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Saw the code and had to comment, soz:

Code: Select all

<?php

// add some error handling to your database functions to save you
// headaches when things don't work as expected, using or die()
// statements is one way to do this.
$db = mysql_connect('localhost', 'root') or die(mysql_error());
mysql_select_db('user', $db) or die(mysql_error());

// separate out your SQL statement from the mysql_query() call
// so that it's easier to debug if things go wrong, also only
// select the fields you need so that you aren't bothering the
// database for information you don't want to use:
$sql = "SELECT first, last, address, position FROM employees";

$result = mysql_query($sql, $db) or die(mysql_error().'<p>'.$sql.'</p>');

// do you only need 3 results, if so add the following to the end
// of your SQL statement - LIMIT 0,3 - if not just change the for
// loop to:
while ($row = mysql_fetch_assoc($result)) {
	// mysql_fetch_assoc() should be used instead of mysql_result()
	// in most instances as it's a faster way of doing things when 
	// you are retrieving a number rows of records containing a 
	// number of fields.

	// if you aren't formatting a result, don't use printf() as
	// echo will do just fine:
	echo 'First Name: '.$row['first'].'<br />';
	echo 'Last Name: '.$row['last'].'<br />';
	echo 'Address: '.$row['address'].'<br />';
	echo 'Position: '.$row['position'].'<br />';
	echo '<br />';
}

?>
Mac
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

That was very helpful. I was wondering about not using the printf function, and was going to ask about how to implement echo instead.

T'anks!!
Post Reply