Some php help

jake3340

Supreme Member
Joined
Nov 20, 2008
Messages
1,446
Reaction score
442
Okay im a noob when it comes to php but i know a bit to get by. Okay so here is what I am trying to do. I created an sql database and inserted 10 links into it, I want to create a loop and echo the first link, when the loop, loops again it echos the second link after it reaches the end instead of starting over again it stops. How do i go around doing this ?
 
Not sure if I understand your question correctly but you want to create a website that takes a list of 10 links from a sql database and echo's the first one.... then you want it to echo the second link and so on until it reaches the end.
What I can't wrap my head around is what you mean by the "loop" do you want it to echo all the links on 1 page with one page refresh or do you want it to refresh the page and spit out the next one... Basically what "loops" in order to get the next link?
 
I am not much into php but i think this could be done simply by putting links into array

PHP:
[PHP]$arraylinks = array("http://site1.com", "http://site2.com", "http://site3.com", "http://site4.com");
then afterwards

PHP:
foreach ($arraylinks as &$value) {
    echo $value; }

So, this will echo every link in $arraylinks array, once

I didnt have much experience about putting sql database content into array but i figure its not hard to google
 
Last edited:
If your problem not solved yet , I can help you.

let me know in gtalk or gmail : asifbrown

Thanks,
Asif
Sr. Software Engineer
 
that's really easy:

PHP:
$db = new MySQLi($host, $user, $pass, $db_name);
$result = $db->query("select $column_name from $your_table_name");
while ($row = $result->fetch_assoc()){
        print $row[$column_name];
}
//gg
 
PHP:
// get links from database
$getLinks = mysql_query("SELECT `link` FROM `links_table`");
// create an array at the first row, echo that row, then continue to next row
while ($linksArr = mysql_fetch_array($getLinks)) echo $linksArr['link'] . "<br>";
 
Last edited:
This should do the trick

PHP:
<?php
    if ($con = mysql_connect('host', 'user', 'password')) {
        if (mysql_select_db) {
            $result = mysql_query('SELECT * FROM `table`');
            
            if (mysql_num_rows($result) > 0) {
                while($row = mysql_fetch_assoc($result)) {
                    echo $row['field name'];
                }
            } else {
                die('No records returned.');
            }
        } else {
            die('Unable to select MySql Database.');
        }
    } else {
        die('Unable to connect to MySQL Server.');
    }
?>
 
Back
Top