FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Get links from a database





alalex
I want to get the links and names of link from a database, and I use the following code:
Code:

<?php
     $connection = mysql_connect('localhost','username','password');
                   mysql_select_db('username_users');
                    $sql = "SELECT (name, link) FROM left_buttons WHERE id = 1";
                   $result = mysql_query($sql);
                  
                       while($row = mysql_fetch_row($result)){
                     echo '<a href="';
                     echo "$row[1]";
                     echo '"';
                     echo '&gt;';
                     echo "$row[0]";
                     echo '</a>';
                     }
                  mysql_close($connection);
   ?>

But it says that the mysql_fetch_row is not a valid mysql result resuorce, I also tried with mysql_fetch_assoc() and it didn't work...
alalex
New code:
Code:

<?php
     $connection = mysql_connect('localhost','username',password');
                   mysql_select_db('alalex_users');
                    $sql = "SELECT * FROM left_buttons WHERE id = 1";
                   $result = mysql_query($sql);
                  
                       while($row = mysql_fetch_row($result)){
                     ?>
                     <a href="
                        <?php echo "$row[link]"; ?>
                     ">
                        <?php echo "$row[name]"; ?>
                     </a>
                     <?php
                     }
                  mysql_close($connection);
   ?>


But still not working!!!!!!!!!!!!! Evil or Very Mad Twisted Evil
Stubru Freak
Try doing

Code:
echo mysql_error();


after your query and see what the error is.
Jamatu
Code:
$connection = mysql_connect('localhost','username',password');

You missed the ' before the password
Code:
$connection = mysql_connect('localhost','username','password');


Anyway try using this, I think it should work

Code:
<?php
$connection = mysql_connect('localhost','username','password');
mysql_select_db('alalex_users');

$sql = "SELECT * FROM left_buttons WHERE id = 1";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
?>
   <a href="<? echo $row[link_url]; ?>"><? echo $row[name]; ?></a>       
<?
}
mysql_close($connection);
?>

Change the name of the 'link' row to 'link_url' or w/e you want as link is a php function
BlackSkad
MySQL crashes when a '_' occures in the name of a field or table. You can solve this by surrounding the name with ``:

Code:
SELECT * FROM `left_buttons` WHERE id = 1


It should work now...

Also, when you have a string whitout variables, surround them with sigle quoted instead of double quotes. The string then is not parsed by the ZEND-engine, which speeds up your script.

Code:
mysql_query('SELECT * FROM `left_buttons` WHERE id = 1')


BlackSkad
alalex
Thanks, I will try it now, and if it works I will tell you! Thanks a lot
alalex
I'm sorry guys, but I tried all your suggestions and it is still not working!!
The database is created it already, the link and the name are stored with id = 1, i did that in phpmyadmin, but it doesn't work:
Code:

$connection = mysql_pconnect('localhost','username','password');
                   mysql_select_db('username_table');
                    $sql = 'SELECT * FROM `left_buttons` WHERE id = 1';
                   $result = mysql_query($sql);
                                    
                       while($row = mysql_fetch_row($result)){
                     ?>
                     <a href="<?php echo "$row[link]"; ?>"><?php echo "$row[name]"; ?></a> 
                     <?php
                     }
                  mysql_close($connection);
   ?>
BlackSkad
You might consider giving us the exact error, that should help us a little more. Change your query statement like this, so it displays mysql-errors:

Code:
$query = mysql_query ($sql) or die (mysql_error);


Oh, and try using mysql_connect instead of mysql_pconnect
alalex
The problem is that there is no error message, instead there is nothing!!
krazycapital
1: You do have data inside the database right?
2: Try this:
Code:
<?php
$connection = mysql_connect('localhost','username','password')or die(mysql_error());
$db = mysql_select_db('username_table',$connection)or die$mysql_error());
$sql = "SELECT * FROM `left_buttons` WHERE `id`='1'";
$result = mysql_query($sql)or die(mysql_error());
while($row = mysql_fetch_row($result)){
echo "<a href=\"".$row[link]."\">".$row[name]."</a>";
}
mysql_close($connection);
?>
alalex
Yeah, I entered the data using phpmyadmin. I will try that, thanks!!
Related topics
WTF! A strange database just appeared lol
Programming links, info, and tutorials
Azureus magnet links faster?
World of Warcraft - itemstats database
database for enhancing the links into the frihost community
targeting <div> tags with links
Download database.
Search more then One Database
Science Universe Podcasts Videos
Free Links Directory - submit your site or favorite site
Ajax and mysql and PHP, pulling info from a database
How to move SQL database to new domain?
Help! Some problems in CPanel, database all of a sudden.
PHP website with SQL Database.
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.