Jump to content

Create array from mysql results


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

    //set variables from form
    $home = $_POST['home-team'];
    $away = $_POST['away-team'];
    $user_id = "2";
    $meeting_date = "2013-05-13";
    //create score card
    $new_card_sql = "INSERT INTO `tbl_card` (`user_id`,`meeting_date`,`home`,`away`) VALUES ('$user_id','$meeting_date','$home','$away')";
    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());
        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
Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

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

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.