Solved

Fetech a value from a array?

Posted on 2004-10-28
201 Views
Last Modified: 2006-11-17
here is a problem i keep running into i do a simple sql query to get soem values from my database..

exp:
$vincheck = "select * from vin where vin = $_POST[vin]";
$check_result = mysql_query($vincheck,$conn) or die(mysql_error());

i then access this information like this
  $temp = mysql_fetch_row($check_result);
  $_SESSION[customer_id] = $temp[5];

This works fine but what i really want, is instead of using $temp[5] i want it to be the vale from the table so it's easier to understand and also not depedant on the table structure.

so i tried replacing $temp[5] with $temp[currentownerID] and i can never get this to work..

currentownerID is the 6th row in the table (rember counting starts at 0)

-Thanks in advance for any help-
0
Question by:mightofnight
    14 Comments
     
    LVL 36

    Expert Comment

    by:Zyloch
    Do this:

    $temp = mysql_fetch_assoc($check_result);
    $_SESSION[customer_id] = $temp["currentowernID"];
    0
     
    LVL 2

    Author Comment

    by:mightofnight
    i had tried that and it doesn't work :( i have done it in both single and double quotes just to make sure.  I even copied and pasted the name of the record from phpmyadmin just to make sure i was spelling it right.
    0
     
    LVL 36

    Expert Comment

    by:Zyloch
    What about:

    $_SESSION["customer_id"] = $temp["currentowernID"];
    0
     
    LVL 2

    Expert Comment

    by:suresh_asp
    Try this

    $temp = mysql_fetch_array($check_result);
    $_SESSION["customer_id"] = $temp["currentownerID"];
    0
     
    LVL 2

    Expert Comment

    by:suresh_asp
    Note: with mysql_fetch_array

    you can access element in array both ways

    $temp[0] or $temp['currentownerID']
    0
     
    LVL 2

    Author Comment

    by:mightofnight
    $detail_report .= "<br>checkvin temp= ".$temp[5]."";
    $detail_report .= "<br>checkvin temp= ".$temp["currentownerID"]."";
    $detail_report .= "<br>checkvin temp= ".$temp[currentownerID]."";
    $detail_report .= "<br>checkvin temp= ".$temp['currentownerID']."";
    echo $detail_report

    gives me this

    checkvin temp= 1
    checkvin temp=
    checkvin temp=
    checkvin temp=

    i couldn't get any of the other stuff to function right
    0
     
    LVL 2

    Author Comment

    by:mightofnight
    suresh_asp - i am retring give me a couple..
    0
     
    LVL 2

    Author Comment

    by:mightofnight
    ok so that worked.. so your saying that only works with the fetch_array and not the fetch row?
    0
     
    LVL 2

    Author Comment

    by:mightofnight
    and then what will happen if there are multiple of rows in the array?
    0
     
    LVL 49

    Expert Comment

    by:Roonaan
    how would there be "multiple rows in the array" ?

    You can build your own array offcourse:
    $s = array();
    $s['a'] = array(1,2,3,4,5);
    $s['b'] = array('a','b','c','d');
    print_r($s);

    But better would be just to read the manual on arrays...

    Regards

    -r-
    0
     

    Expert Comment

    by:mkortleven
    Is your column name in the same case? MySQL table and column names are case-sensitive.
    Try
        print_r($temp);
    Then you can see the column names.

    Marc
    0
     
    LVL 2

    Accepted Solution

    by:
    Try this,

    mysql_fetch_array returns an array of single record/row only

    to get more rows

    $detail_report = "";

    while($temp = mysql_fetch_array($check_result))
    {
    $detail_report .= "<br>checkvin temp= ".$temp["currentownerID"]."";
    }

    echo $detail_report;
    0
     
    LVL 20

    Expert Comment

    by:virmaior
    quite possibly not the source of your problem but these two lines have bad syntax...

    $vincheck = "select * from vin where vin = $_POST[vin]";
    and
    $_SESSION[customer_id] = $temp[5];

    you shouldn't use $_POST[vin]
    it should be $_POST['vin']

    so the lines become:

    $vincheck = 'SELECT * FROM vin WHERE vin = ' . $_POST['vin'];

    and

    $_SESSION['customer_id'] = $temp[5];

    respectively...

    the php website has a long explanation of how you shouldn't assume that [*] will be interpreting * as a string literal

    0
     
    LVL 2

    Author Comment

    by:mightofnight
    Thaks for all the help guys! You really helped clearify things for me!

    -Travis
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now