Jump to content

Recommended Posts

I have been looking over and over and trying over and over to get this to work.

 

Im seriously new at PHP,

 

I want to echo results from SQL query...

 

<?    
    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
    
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
        
    $result = mysql_query("SELECT * FROM userRegistration");
    while ($row = mysql_fetch_array($result)){
        echo $row;
    }
    
    mysql_close($con);
?>

 

this returns:

 

ArrayArrayArray

Link to comment
https://forums.phpfreaks.com/topic/168259-solved-want-to-display-array-of-results/
Share on other sites

Hi

 

You can use print_r, possibly combined.

 

Personally I would use foreach:-

 

<?    
    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
    
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
        
    $result = mysql_query("SELECT * FROM userRegistration");
    while ($row = mysql_fetch_array($result)){
        foreach($row as $oneRow) echo $oneRow;
    }
    
    mysql_close($con);
?>

 

All the best

 

Keith

Thankyou very much..

 

*grins*.

 

However now it prints each record twice.

 

So my output is:

 

value1value1value2value2value3value3

 

instead of

 

value1value2value3:

 

<?    
    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
    
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
       
    $result = mysql_query("SELECT * FROM userRegistration");
    while ($row = mysql_fetch_array($result)){
        foreach($row as $oneRow) echo $oneRow . "<br />";
    }
   
    mysql_close($con);
?>

Hi

 

Can't see why that should be the case.

 

Try this, just to check the keys and see if the issue is the data:-

 

<?    
    $con = mysql_connect($server, $username, $password);
    mysql_select_db('distal', $con);
    
    if (!$con)
    {
      die('Could not connect: ' . mysql_error());
    }
       
    $result = mysql_query("SELECT * FROM userRegistration");
    while ($row = mysql_fetch_array($result)){
        foreach($row as $key => $oneRow) echo "$key $oneRow <br />";
    }
   
    mysql_close($con);
?>

 

All the best

 

Keith

So the first without your latest addition was:

 

firstName2

firstName2

lastName2

lastName2

userName2

userName2

password2

password2

emailAddress2

emailAddress2

dateOfBirth2

dateOfBirth2

Male

Male

remindPassword2

remindPassword2

6

6

firstName

firstName

lastName

lastName

userName

userName

password

password

emailAddress

emailAddress

dateOfBirth

dateOfBirth

Male

Male

remindPassword

remindPassword

5

5

 

now with your latest addition:

 

0 firstName2

firstName firstName2

1 lastName2

lastName lastName2

2 userName2

userName userName2

3 password2

password password2

4 emailAddress2

emailAddress emailAddress2

5 dateOfBirth2

dateOfBirth dateOfBirth2

6 Male

sex Male

7 remindPassword2

reminder remindPassword2

8 6

id 6

0 firstName

firstName firstName

1 lastName

lastName lastName

2 userName

userName userName

3 password

password password

4 emailAddress

emailAddress emailAddress

5 dateOfBirth

dateOfBirth dateOfBirth

6 Male

sex Male

7 remindPassword

reminder remindPassword

8 5

id 5

Hi

 

Doh, know what the problem is. Mysql_fetch_array brings back an array of all the elements, but by default both the numeric and associative array.

 

Try this to bring back just the associative array (or use MYSQL_NUM to just bring back the numeric array).

 

<?   

    $con = mysql_connect($server, $username, $password);

    mysql_select_db('distal', $con);

   

    if (!$con)

    {

      die('Could not connect: ' . mysql_error());

    }

     

    $result = mysql_query("SELECT * FROM userRegistration");

    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){

        foreach($row as $key => $oneRow) echo "$key $oneRow <br />";

    }

 

    mysql_close($con);

?>

 

All the best

 

Keith

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.