p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: PHP and MySQL .... need help


Message #1 by "Brady Love" <opettaja@p...> on Sun, 22 Sep 2002 22:17:04
Having some troubles... it will only echo teh link and not the "- 
$description" anyone know whats wrong?
---------------------------------------------------------------------------
function sponsors_p(){
	global $dbhost, $db, $dbpass, $dbuser, $tablename;
	
	$link_id = db_connect();
	if(!$link_id) die ("unable to connect to db");
	mysql_select_db($db);
	$query = "SELECT siteName, url, type, description FROM $tablename 
WHERE type = 'sponsor'";
	if(!$query) die ("unable to get data");
	$result = mysql_query($query);
	if(!$result) die ("unable to do query");
	while($query_data = mysql_fetch_array($result)){
		$siteName = $query_data['siteName'];
		$url = $query_data['url'];
		$type = $query_data['type'];
		$description = $query_data['description'];
		
		echo "<a href=\"$url\">$siteName</a> - $description <br>";
	}
}
---------------------------------------------------------------------------
Thanks
Message #2 by "Dan Ostrowski" <dan@t...> on Sun, 22 Sep 2002 22:50:22
First, i would work on losing those globals, either shoving them into a 
global array, or just passing a database abstraction object to the 
function.

Second, i don't really understand why you set a string variable and then 
test to see if it's set. That doesn't make a lot of sense.

>	$query = "SELECT siteName, url, type, description FROM $tablename 
>WHERE type = 'sponsor'";
>	if(!$query) die ("unable to get data");

As far as the rest goes, you are syntactically correct. I am not sure why 
it's not working, but I would bet it's either a misnaming, misspelling, or 
database problem. Since you are accessing the end variables the same way, 
you probably just have a small spelling error somewhere like in the mysql 
table or somesuch.

>	$result = mysql_query($query);
	if(!$result) die ("unable to do query");
	while($query_data = mysql_fetch_array($result)){
		$siteName = $query_data['siteName'];
		$url = $query_data['url'];
		$type = $query_data['type'];
		$description = $query_data['description'];
		
		echo "<a href=\"$url\">$siteName</a> - $description <br>";
	}
}
---------------------------------------------------------------------------


regards,
dan
Message #3 by "Nikolai Devereaux" <yomama@u...> on Mon, 23 Sep 2002 12:16:49 -0700
Perhaps there is no description set in the database table!  Are you sure you're
inserting descriptions properly?

nik


  Return to Index