Resource id #2 - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Resource id #2

__GizMo____GizMo__ Posts: 10Member
When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
Resource id #2

I've got this php script:
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">


Title here!


<?php
mysql_connect("localhost","root","");
mysql_select_db("php");
$query = "select * from users";
$result = mysql_query($query);
print("$result");

?>



Comments

  • jcarlssonjcarlsson Posts: 21Member
    : When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
    : Resource id #2
    :
    : I've got this php script:
    : <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    :
    :
    : Title here!
    :
    :
    : <?php
    : mysql_connect("localhost","root","");
    : mysql_select_db("php");
    : $query = "select * from users";
    : $result = mysql_query($query);
    : print("$result");
    :
    : ?>
    :
    :


    have you looked at any page for how you should write the code? I don't know PHP but I think it says on some pages how it should be, if you have taken the code from a page then complain to the one that wrote the page and ask him to try the script :-)
    [red]--[/red]
    [green]jcarlsson[/green] [blue]<[/blue][purple][email protected][/purple][blue]>[/blue]
    [red]http://www.jcarlsson.tk[/red]

  • turboturbo Posts: 2Member
    : When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
    : Resource id #2
    :
    : I've got this php script:
    : <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    :
    :
    : Title here!
    :
    :
    : <?php
    : mysql_connect("localhost","root","");
    : mysql_select_db("php");
    : $query = "select * from users";
    : $result = mysql_query($query);
    : print("$result");
    :
    : ?>
    :
    :
    :
    :
    You have to throw the result into an array using mysql_fetch_array and then loop through each element in the array. So your program should be something ilke this then:

    $result = mysql_query("SELECT * FROM users");
    while ($r = mysql_fetch_array($result)) {
    $row1 = $r["row_1_name"];
    $row2 = $r["row_2_name"];
    etc...
    }

    Hope that helps
  • michaelmwilsonmichaelmwilson Posts: 3Member
    : When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
    : Resource id #2
    :
    : I've got this php script:
    : <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    :
    :
    : Title here!
    :
    :
    : <?php
    : mysql_connect("localhost","root","");
    : mysql_select_db("php");
    : $query = "select * from users";
    : $result = mysql_query($query);
    : print("$result");
    :
    : ?>
    :
    :
    :
    :

    if you want to just see what comesout in the result, then do

    mysql_connect("localhost","root","");
    mysql_select_db("php");
    $query = "select * from users";
    $result = mysql_query($query);

    $array=mysql_fetch_assoc($result);
    print_r($array);

    This will give you an output like

    Array( [key] => "VALUE",
    [key1] => "VALUE1",
    etc...)

  • CptnChainsawCptnChainsaw Posts: 1Member
    : : When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
    : : Resource id #2
    : :
    : : I've got this php script:
    : : <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    : :
    : :
    : : Title here!
    : :
    : :
    : : <?php
    : : mysql_connect("localhost","root","");
    : : mysql_select_db("php");
    : : $query = "select * from users";
    : : $result = mysql_query($query);
    : : print("$result");
    : :
    : : ?>
    : :
    : :
    : :
    : :
    :
    : if you want to just see what comesout in the result, then do
    :
    : mysql_connect("localhost","root","");
    : mysql_select_db("php");
    : $query = "select * from users";
    : $result = mysql_query($query);
    :
    : $array=mysql_fetch_assoc($result);
    : print_r($array);
    :
    : This will give you an output like
    :
    : Array( [key] => "VALUE",
    : [key1] => "VALUE1",
    : etc...)
    :
    :
    I've tried the above and it works fine. The thing is, I want to extract the actual value in the array so that I can insert it in to a lookup table, how would I do that?

    At the moment the above will print out: Array ( [ID] => 3 )
    I just want the value 3 so that it can be inserted to the lookup table straight away.

    What does Resource id #2 mean? I got also, which is what led me here ! :)

    Any ideas?

    Cheers,

    CptnChainsaw :)
  • nukedwebnukedweb Posts: 2Member
    : : : When i try to view the data from a mysql database using php, then the only thing that is in my browser is:
    : : : Resource id #2
    : : :
    : : : I've got this php script:
    : : : <!doctype html public "-//W3C//DTD HTML 4.0 //EN">
    : : :
    : : :
    : : : Title here!
    : : :
    : : :
    : : : <?php
    : : : mysql_connect("localhost","root","");
    : : : mysql_select_db("php");
    : : : $query = "select * from users";
    : : : $result = mysql_query($query);
    : : : print("$result");
    : : :
    : : : ?>
    : : :
    : : :
    : : :
    : : :
    : :
    : : if you want to just see what comesout in the result, then do
    : :
    : : mysql_connect("localhost","root","");
    : : mysql_select_db("php");
    : : $query = "select * from users";
    : : $result = mysql_query($query);
    : :
    : : $array=mysql_fetch_assoc($result);
    : : print_r($array);
    : :
    : : This will give you an output like
    : :
    : : Array( [key] => "VALUE",
    : : [key1] => "VALUE1",
    : : etc...)
    : :
    : :
    : I've tried the above and it works fine. The thing is, I want to extract the actual value in the array so that I can insert it in to a lookup table, how would I do that?
    :
    : At the moment the above will print out: Array ( [ID] => 3 )
    : I just want the value 3 so that it can be inserted to the lookup table straight away.
    :
    : What does Resource id #2 mean? I got also, which is what led me here ! :)
    :
    : Any ideas?
    :
    : Cheers,
    :
    : CptnChainsaw :)
    :

    Resource IDs are "file pointers" basically. The data from a MySQL lookup is stored in memory, and Resource ID #2 tells PHP where to find it. This's also the way it works when opening/saving text files from PHP. =)

    If your SELECT statement pulls more than one row out of MySQL, and lets say it has 3 tables, you could use a code similar to this:

    $sql = "";
    $result = mysql_query($sql) or die("Failed: $sql"); //Stops the script if the query failed
    $numrows = mysql_num_rows($result); //The number of rows the query returned

    for($x=0;$x<$numrows;$x++){
    $result_row = mysql_fetch_row($result); //grabs the first (or next) array of results
    $field1 = $result_row[0];
    $field2 = $result_row[1];
    $field3 = $result_row[2];

    #Process the variables here
    }

Sign In or Register to comment.