Jump to content

Create array from mysql results


bravo14

Recommended Posts

Hi Guys

 

I am trying to create an array of results based on a mysql query using the code below:

 

 <?phpif(isset($_POST['submit'])){//set variables from form$home=$_POST['home-team'];$away=$_POST['away-team'];$user_id="2";$meeting_date="2013-05-13";//$meeting_date=date("Y-m-d",strtotim($_POST['meeting-date']));//create score card$new_card_sql=("INSERT INTO `tbl_card` (`user_id`,`meeting_date`,`home`,`away`) VALUES ('$user_id','$meeting_date','$home','$away')");//$new_card=mysql_query($new_card_sql);print $new_card_sql;//find rider information$home_riders_query=("SELECT * FROM `tbl_riders` where `club_id` = '$home'");$home_riders_sql=mysql_query("SELECT * FROM `tbl_riders` where `club_id` = '$home'") or die(mysql_error());if(mysql_num_rows($home_riders_sql)>0){while ($row = mysql_fetch_array($home_riders_sql)){    $data[] = $row;} foreach ($data as $row){echo $row;   } } //redirect to score-card.php//header("location:score-card.php");}?>
 

 

However I only get the following result

 

INSERT INTO `tbl_card` (`user_id`,`meeting_date`,`home`,`away`) VALUES ('2','2013-05-13','1','4')ArrayArrayArrayArrayArrayArrayArray

 

I am wanting to see an array with the following fields in from the table tbl_riders --> rider_name rider_number

 

What am I doing wrong and how would I go about getting my desired result?

 

 

Link to comment
Share on other sites

Well, $row is an array of a single record from the result set. So you can't echo it. You will need to echo the fields that you want displayed. If you only need rider_name and rider_number, then only include those in your query. Also, only query the fields you need, i.e. don't use "SELECT *" unless you really need all fields.

 

Try this

if(isset($_POST['submit']))
{
    //set variables from form
    $home = $_POST['home-team'];
    $away = $_POST['away-team'];
    $user_id = "2";
    $meeting_date = "2013-05-13";
    //$meeting_date=date("Y-m-d",strtotim($_POST['meeting-date']));
    //create score card
    $new_card_sql = "INSERT INTO `tbl_card` (`user_id`,`meeting_date`,`home`,`away`) VALUES ('$user_id','$meeting_date','$home','$away')";
    //$new_card=mysql_query($new_card_sql);
    print $new_card_sql;
    //find rider information
    $home_riders_query = "SELECT rider_name, rider_number FROM `tbl_riders` where `club_id` = '$home'";
    $home_riders_sql = mysql_query("SELECT * FROM `tbl_riders` where `club_id` = '$home'") or die(mysql_error());
    if(mysql_num_rows($home_riders_sql))
    {
        while ($row = mysql_fetch_array($home_riders_sql))
        {
            $data[] = $row;
        }
 
        foreach ($data as $row)
        {
            echo "{$row['rider_name']}: {$row['rider_number']}<br>\n";
        }
    }

    //redirect to score-card.php
    //header("location:score-card.php");
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.