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!

Arrays Mysql query.. help! :( 1

Status
Not open for further replies.

unborn

Programmer
Jun 26, 2002
362
US
Ok im trying to get down this whole database thing. Simple bookmark program and im trying to learn to use mysql and i just cant seem to get it. Here are my functions..

Code:
function get_user_urls2($username)
{
  	//extract from the database all the URLs this user has stored
  	if (!($conn = db_connect()))
    	return false;
  	$result = mysql_query( "select * from bookmarks where username = '$username'");
  	if (!$result)
    	return false; 

  	//create an array of the URLs 
  	$url_array = array();
	$link_array = array();
	$name_array = array();
  	for ($count = 1; $row = mysql_fetch_row ($result); ++$count) 
  	{
		$name_array[$count] = mysql_result($result, $count, "name");
    	$link_array[$count] = addslashes($row[1]);
		$url_array[$count] = $name_array[$count]."###".$link_array[$count];
		
  	} 
  	
	return $url_array ;
}

thants to grab all links and their names, now it has been in many diffrent forms as ive tried everyhtign to get it to pull both the name(in col '0') and the url(in col '1'). I really have tried everything i know so i hope someone can help me :( and here is the actuall output

Code:
function display_user_urls2($url_array)
{
  //display the table of URLs

  // set global variable, so we can test later if this is on the page
  global $bm_table;
  $bm_table = true;
?>
  <br />
  <form name=bm_table action="delete_bms.php" method=post>
  <table width=300 cellpadding=2 cellspacing=0>
  <?php
  $color = "#cccccc";
  echo "<tr bgcolor=$color><td><strong>Bookmark</strong></td>";
  echo "<td><strong>Delete?</strong></td></tr>";
  
  if (is_array($url_array) && count($url_array)>0)
  {
    foreach ($url_array as $url)
    {
	
	  if ($color == "#cccccc")
        $color = "#ffffff";
      else
        $color = "#cccccc";
		
	  $url = explode ("###", $url);	
      // remember to call htmlspecialchars() when we are displaying user data
      echo "<tr bgcolor=$color><td><a href='".$url[1]."' target='_new'>".htmlspecialchars($url[0])."</a></td>";
      echo "<td><input type=checkbox name=\"del_me[]\"
             value=\"$url\"></td>";
      echo "</tr>"; 
    }
  }
  else
    echo "<tr><td>No bookmarks on record</td></tr>";
?>
  </table> 
  </form>
<?php
}

like isaid ive tried alot of diffrent stuff so it may be like whooaaa whats he doing.. its jsut i started trying wierd things to see if they worked.. if someone knows what im talkign about maybe a little explanation of how to do it or point to a good tutorial site for this stuff? eventually i would like to make a community site and i need to learn how to pull data out correctly and put in array and sort out and what not. Thanks!

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
There are a few things you can optimize a lot:
Use the PHP function mysql_fetch_assoc() to return the rows as an associative array.
Code:
# Note: make sure $username is properly escaped
$SQL = "SELECT * FROM bookmarks WHERE username='$username'";
# issue query and check for failure
$result = mysql_query($SQL) OR die('Query error: '.mysql_error());
# iterate the result set
while ($row = mysql_fetch_assoc($result)){
   # here you can do wahtever you want
   # i.e. put the row into an array which I would only do if there
   # is a need to reuse the information in more than one place
   # otherwise excute a callback function to output the data
   # example
   display_row($row);
   # $row is an associative array keyed by the column names
   # e.g. $row['url'] would contain the value of the column 'url'
}
# that's it

If you need more read faq434-3850
 
Thanks! I also found out i wasnt calling another function i needed so it actually wasnt working at all for me.. but after reviewing my code i see what i was doing and stripped it down quite a bit and used the assoc thanks!

in the begining man created code.
in the end code will create man.
clones are coming only matter of time.
examples?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top