Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    accesing a database using php

    i have created an online mysql database but its requires a username, server and password to get on it. For a website i am creating i want to use a php form for connecting to the database. It doesnt seem to be connecting at the moment

    <html>
    <head>

    </head>

    <body>

    <h2>All the properties on sale. For more info on the housesplease go back to the homepage and visit webpages for the areas of cardiff</h2>
    <pre>

    <?php

    $connection = mysql_connect("my server name", "my username", "my password");

    mysql_select_db("my database", $connection) or die("Failed!");

    //Now i will collect all the data held in the database
    //I will specify what table i want to select data from
    //I will be selecting all the properties from the table i select

    $result = mysql_query("SELECT * FROM db1", $connection);


    while($row = mysql_fetch_row($result)) {

    //get the following data

    $Property_ID = $row[1];
    $Property_ID = $row[2];
    $Property_ID = $row[3];
    $Property_ID = $row[4];
    $Property_ID = $row[5];
    $Property_ID = $row[6];
    $Property_ID = $row[7];
    $Property_ID = $row[8];
    $Property_ID = $row[9];
    $Property_ID = $row[10];
    $Property_ID = $row[11];
    $Property_ID = $row[12];
    $Property_ID = $row[13];
    $Property_ID = $row[14];
    $Property_ID = $row[15];
    $Property_ID = $row[16];
    $Property_ID = $row[17];
    $Property_ID = $row[18];
    $Property_ID = $row[19];
    $Property_ID = $row[20];
    $Property_ID = $row[21];

    $Property_Price = $row[1];
    $Property_Price = $row[2];
    $Property_Price = $row[3];
    $Property_Price = $row[4];
    $Property_Price = $row[5];
    $Property_Price = $row[6];
    $Property_Price = $row[7];
    $Property_Price = $row[8];
    $Property_Price = $row[9];
    $Property_Price = $row[10];
    $Property_Price = $row[11];
    $Property_Price = $row[12];
    $Property_Price = $row[13];
    $Property_Price = $row[14];
    $Property_Price = $row[15];
    $Property_Price = $row[16];
    $Property_Price = $row[17];
    $Property_Price = $row[18];
    $Property_Price = $row[19];
    $Property_Price = $row[20];
    $Property_Price = $row[21];

    $Property_Name = $row[1];
    $Property_Name = $row[2];
    $Property_Name = $row[3];
    $Property_Name = $row[4];
    $Property_Name = $row[5];
    $Property_Name = $row[6];
    $Property_Name = $row[7];
    $Property_Name = $row[8];
    $Property_Name = $row[9];
    $Property_Name = $row[10];
    $Property_Name = $row[11];
    $Property_Name = $row[12];
    $Property_Name = $row[13];
    $Property_Name = $row[14];
    $Property_Name = $row[15];
    $Property_Name = $row[16];
    $Property_Name = $row[17];
    $Property_Name = $row[18];
    $Property_Name = $row[19];
    $Property_Name = $row[20];
    $Property_Name = $row[21];

    $House_Number = $row[1];
    $House_Number = $row[2];
    $House_Number = $row[3];
    $House_Number = $row[4];
    $House_Number = $row[5];
    $House_Number = $row[6];
    $House_Number = $row[7];
    $House_Number = $row[8];
    $House_Number = $row[9];
    $House_Number = $row[10];
    $House_Number = $row[11];
    $House_Number = $row[12];
    $House_Number = $row[13];
    $House_Number = $row[14];
    $House_Number = $row[15];
    $House_Number = $row[16];
    $House_Number = $row[17];
    $House_Number = $row[18];
    $House_Number = $row[19];
    $House_Number = $row[20];
    $House_Number = $row[21];

    $Street = $row[1];
    $Street = $row[2];
    $Street = $row[3];
    $Street = $row[4];
    $Street = $row[5];
    $Street = $row[6];
    $Street = $row[7];
    $Street = $row[8];
    $Street = $row[9];
    $Street = $row[10];
    $Street = $row[11];
    $Street = $row[12];
    $Street = $row[13];
    $Street = $row[14];
    $Street = $row[15];
    $Street = $row[16];
    $Street = $row[17];
    $Street = $row[18];
    $Street = $row[19];
    $Street = $row[20];
    $Street = $row[21];

    $Area = $row[1];
    $Area = $row[2];
    $Area = $row[3];
    $Area = $row[4];
    $Area = $row[5];
    $Area = $row[6];
    $Area = $row[7];
    $Area = $row[8];
    $Area = $row[9];
    $Area = $row[10];
    $Area = $row[11];
    $Area = $row[12];
    $Area = $row[13];
    $Area = $row[14];
    $Area = $row[15];
    $Area = $row[16];
    $Area = $row[17];
    $Area = $row[18];
    $Area = $row[19];
    $Area = $row[20];
    $Area = $row[21];

    $Postcode = $row[1];
    $Postcode = $row[2];
    $Postcode = $row[3];
    $Postcode = $row[4];
    $Postcode = $row[5];
    $Postcode = $row[6];
    $Postcode = $row[7];
    $Postcode = $row[8];
    $Postcode = $row[9];
    $Postcode = $row[10];
    $Postcode = $row[11];
    $Postcode = $row[12];
    $Postcode = $row[13];
    $Postcode = $row[14];
    $Postcode = $row[15];
    $Postcode = $row[16];
    $Postcode = $row[17];
    $Postcode = $row[18];
    $Postcode = $row[19];
    $Postcode = $row[20];
    $Postcode = $row[21];

    $Number_of_bedrooms = $row[1];
    $Number_of_bedrooms = $row[2];
    $Number_of_bedrooms = $row[3];
    $Number_of_bedrooms = $row[4];
    $Number_of_bedrooms = $row[5];
    $Number_of_bedrooms = $row[6];
    $Number_of_bedrooms = $row[7];
    $Number_of_bedrooms = $row[8];
    $Number_of_bedrooms = $row[9];
    $Number_of_bedrooms = $row[10];
    $Number_of_bedrooms = $row[11];
    $Number_of_bedrooms = $row[12];
    $Number_of_bedrooms = $row[13];
    $Number_of_bedrooms = $row[14];
    $Number_of_bedrooms = $row[15];
    $Number_of_bedrooms = $row[16];
    $Number_of_bedrooms = $row[17];
    $Number_of_bedrooms = $row[18];
    $Number_of_bedrooms = $row[19];
    $Number_of_bedrooms = $row[20];
    $Number_of_bedrooms = $row[21];

    $Viewing_times_available = $row[1];
    $Viewing_times_available = $row[2];
    $Viewing_times_available = $row[3];
    $Viewing_times_available = $row[4];
    $Viewing_times_available = $row[5];
    $Viewing_times_available = $row[6];
    $Viewing_times_available = $row[7];
    $Viewing_times_available = $row[8];
    $Viewing_times_available = $row[9];
    $Viewing_times_available = $row[10];
    $Viewing_times_available = $row[11];
    $Viewing_times_available = $row[12];
    $Viewing_times_available = $row[13];
    $Viewing_times_available = $row[14];
    $Viewing_times_available = $row[15];
    $Viewing_times_available = $row[16];
    $Viewing_times_available = $row[17];
    $Viewing_times_available = $row[18];
    $Viewing_times_available = $row[19];
    $Viewing_times_available = $row[20];
    $Viewing_times_available = $row[21];


    <br>

    for($i=0; $i<mysql_num_fields($result); $i++)
    print $row[$i]." ";
    echo "\n";

    mysql_close($connection);


    ?>
    </pre>
    </body>

    </html>

    can anyone spot what i am doing wrong with this. i have checked all my variables i.e Property_Id and they all seem to be same as database.
    Last edited by bluebirdjc; 03-27-2007 at 05:47 PM.

  • #2
    New Coder
    Join Date
    Mar 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    anyone

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    gee, thats one long page of code you've got there... is db1 your table name?

    your connection looks fine, but I'll give oyu an example of how to use the data maybe a bit more effectively

    lets say the table in my database is table, and I have 2 fields, filed1 and field2 (let's go easy).

    PHP Code:
    $res_query mysql_query("SELECT * FROM table");
    while(
    $row mysql_fetch_array($res_query))
    {
      echo 
    "field1: " $row[field1] + ", field2: " $row[field2] + "<br />";

    thats a simple way of just showing all the rows, if instead you want to store it all in pseudo-arrays, you'd go like this:

    PHP Code:
    $count 0;
    $res_query mysql_query("SELECT * FROM table");
    while(
    $row mysql_fetch_array($res_query))
    {
      
    $field1[$count] = $row[field1]; 
      
    $field2[$count] = $row[field2];
      
    $count ++;

    now on another note: print is a function that returns something, echo just sends the string to the client, so if you want a bit of a faster results with lots of data, echo is the way to go (you don't even need to use print).

    Anyways, I hope that helped

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The difference in speed between echo and print will never be noticable by a human.

    http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

    Anyways, use mysql_fetch_array() like Krokador showed you.

  • #5
    New Coder
    Join Date
    Mar 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks all for the help but my lecturer wants me to do it in the way ive been shown i think i mayjust have some problems with defining my feilds now

    this is my code now

    <html>
    <head>

    </head>

    <body>

    <h2>Database of all properties </h2>
    <pre>

    <?php

    $connection = mysql-connect("myservername","myusername", "mypassword");

    mysql_select_db("dcm6jkc", $connection) or die("failed!");

    $result = mysql_query("SELECT * FROM db1", $connection);


    while($row = mysql_fetchrow($result)) {

    //Getting data from feilds

    $Property_ID = $row[1];
    $Property_Price = $row[2];
    $Property_Name = $row[3];
    $House_Number = $row[4];
    $Street = $row[5];
    $Area = $row[6];
    $Postcode = $row[7];
    $Number_of_bedrooms = $row[8];
    $Viewing_times_available = $row[9];

    for($i=0; $i<mysql_num_fields($result); $i++)
    print $row[$i]." ";
    echo "\n";

    }

    mysql_close($connection);
    ?>

    i actually have 21 properties so 21 rows in database overall but i didnt think you would be able to represent all rows for each field in the way i have been shown so i have defined each one for a different row of the database as it stands.

    can anyone help me so i can display the database please

  • #6
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,046
    Thanks
    19
    Thanked 42 Times in 42 Posts
    i dont no if its just a typo (assuming it is from the first post)
    PHP Code:
    // $connection = mysql-connect("myservername","myusername", "mypassword");
    //change the - to _
    $connection mysql_connect("myservername","myusername""mypassword"); 
    and always wrap PHP code within the php tags
    makes it so much easier to read

  • #7
    New Coder
    Join Date
    Mar 2007
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yea just changed that it aint solved much though thanks anyway

  • #8
    New Coder
    Join Date
    Mar 2007
    Posts
    76
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when you access rows from a mysql_fetch_array with the number, you're getting the fields alright, but for one thing, they start at 0, not 1.

    secondly, whats the point of storing the results if you're showing them on a line afterwards? I'm not too sure I understand what your code is meant to do.

    thridly, you have 1 variable for an unset number of row, so when your while will be over, the only accessible data you'll have will be the last row you fetched. Is that what you want?

    fourth, mysql_num_rows returns the number of rows in your result set. I doubt that for loop will help you in displaying the data from the row as you're iterating through the row fields and not the total rows of the result set: that's what the mysql_fetch_array while loop is for.

    Try maybe describing exactly what you have to do, might be able to help you more. And I'm sorry if the post sounds a bit harsh, I'm just trying to help you understand how it works
    Last edited by Krokador; 03-28-2007 at 07:04 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •