Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWBIE: Connecting 1

Status
Not open for further replies.

TheConeHead

Programmer
Aug 14, 2002
2,106
US
Can anyone provide me with a sample connection string and query? I am working within PHP and am familiar with SQL (not My SQL)

Thanks!

[conehead]
 
Code:
<?php
	# sample connection query
	$link = mysql_connect('ServerNameOfMySql', 'MyLoginnameForMySql', 'MyPasswdForMySql') ;
	if ($link && mysql_db_select_db(&quot;samp_db&quot;)
	{
		$qry = 'SELECT * FROM MyTbl' ;
		$result = mysql_query($qry) 
			or die(&quot;Sorry Query failed&quot;);
		while ($row = mysql_fetch_row($result))
		{
			for ($i = 0; $i < mysql_num_fields($result); $i++)
			{
				if ($i > 0)
					print(&quot;\t&quot;) ;
				print($row[$i]) ;
			}
			print(&quot;\n&quot;) ;
		}
	}
	mysql_free_result($result) ;
?>



[ponder]
----------------
ur feedback is a very welcome desire
 
excellent thanks.... When I use sql, I can grab specific field values like: rs(&quot;fieldValue&quot;).... how would I do so throughout a php page?

You can do more than simply loop through it right?

[conehead]
 
chnage qry to
Code:
$qry = 'SELECT Fld1, Fld2, Fld3 FROM MyTbl' ;
Besides, mysql_fetch_row, another option mysql_fetch_array can be used. Herw u have to substitiute fld name in place of no subscripts i.e. for
Code:
$qry = 'SELECT FirstName, Lastname FROM Names' ;
$result = mysql_query($result)
		or die()  ;
while ($row = mysql_fetch_array($result)) 
{
	printf(&quot;%s %s\n&quot;, $row[0], $row[1]) ;
	printf(&quot;%s %s\n&quot;, $row['FirstName'], 
$row['Lastname']) ;
}
Thing 2 note is that subscript names have identical spelling including case as in SELECT option i.e name in Lastname is all lower case



[ponder]
----------------
ur feedback is a very welcome desire
 
excellent... that gives me plenty to look into... thanks

[conehead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top