Hey
Im getting there with PHP but one thing that does my head in is arrays.
Im trying to get all the content from the db and echo it..

For exmaple, in the db it reads under the 'names' column...
matt
harry
dave
katie
trisha
paul
tracy
brian
martin
sam

I want to be able to echo this on a page, each on a new line.

But, if a new name is added to the database, i want it to automatically display the new one etc

Probably really easy for you lot ;) and maybe ive over complicated the explanation.

Hope you guys can do it.
Also, there is £5 in it via paypal for whoever comes up with a working script.

Recommended Answers

All 4 Replies

<?php
mysql_connect("locahost","username","password") or die(mysql_error());

//change "dbname" to the name of your actual database
mysql_select_db("dbname") or die(mysql_error());

//change "TABLENAME" to the name of the actual table you have
$result=mysql_query("SELECT `names` FROM TABLENAME`") or die( mysql_error() );
while( $row=mysql_fetch_assoc($result) )
{
  echo '<div>' . $row['names'] . '</div>';
}
mysql_close();
?>

Here is how it's done, first we perform all necessary database connections, the we query the database, after obtaining the result-set we transform it to an array The and lastly we print it's content using a foreach loop structure.

<?php
    var $link;    //SQl Link variable.
    var $query;   //SQL query string.
    var $result;  //Where the result set is going to be stored.
    var $array;   //The result-set as an array
    var $table;   //Table name

    $link=mysql_connect($host,$username,$passsword) or die("SQL Server error");
    mysql_select_db($database,$link) or die("Database Error");
    
    $query="SELECT names FROM $table "; //Build query string.
    $result=mysql_query($query,$link);  //Get the result-set.
        
    /* Transform  the result-set to an array so we can loop through it's data  */
    $array=mysql_fetch_array($result);  
    foreach($array as $value)
        echo $value . "<br />";
?>

Regards, Triztian

OkThanks it works!
1 more thing to get this to my needs.
How can I make it into a variable so i can use it on another page.

I tried this..

while( $row_hardware=mysql_fetch_assoc($pull_floats_hardware) )
			{
			  $row_hardware2 = '' . $row_hardware['name'] . '<br/>';
			}

Then

echo $row_hardware2;

on another page but it only shows 1 row

Here is how it's done... /* Transform  the result-set to an array so we can loop through it's data  */

You are simply printing all the columns of the first record. The goal if to print all the records from a column. Refer to my post.

How can I make it into a variable so i can use it on another page. on another page but it only shows 1 row

save your db result as an array:

To clarify, on what you have: $row_hardware2 = '' . $row_hardware['name'] . '<br/>'; $row_harware2 is NOT an array. But if you did: $row_hardware2[] = '' . $row_hardware['name'] . '<br/>'; then execute:

foreach($row_hardware2 as $index=>$v){
 echo $i .': ' . $v;
}

If you really need it on a completely different page, then use a $_SESSION variable instead. Just make sure that at the beginning of your file you have: <?php session_start(); then the body of your while loop should be $_SESSION['row_hardware2'][] = '' . $row_hardware['name'] . '<br/>'; and your foreach:

foreach($_SESSION['row_hardware2'] as $index=>$v){
 echo $i .': ' . $v;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.