Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    this is an experimental code which is always hanging in the mentioned lines. i would appreciate any comments regardng this issue.

    Code:
    <?
    //make the database connection
    $conn = mysql_connect("localhost", "", "");
    mysql_select_db("UPI", $conn);
    
    
    $sql = "EXPLAIN SELECT * FROM `main` WHERE 1 AND `fam` LIKE \'Malvaceae\'";
    
    
    $result = mysql_query($sql, $conn);
    
    //get field names
    print "<tr>\n";
    while ($field = mysql_fetch_field($result)){     - - - - - - - hang
      print " <th>$field->name</th>\n";
    } // end while
    print "</tr>\n\n";
    
    //get row data as an associative array
    while ($row = mysql_fetch_assoc($result)){       - - - - - - - hang
      print "<tr>\n";
      //look at each field
      foreach ($row as $col=>$val){
        print " <td>$val</td>\n";
      } // end foreach
      print "</tr>\n\n";
    }// end while
    
    print "</table>\n";
    ?>
    thanx,
    Ark!

    also i would like to know whether its true that server side scripts like php files dont need any kind of authorization for access to MySQL databases? or is it only for read access??
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    It works for me!

    Don't know what kind of errors you're recieving (since you apperantly know that mysql_fetch_field() is causing the problem).

    The object returning mysql_fetch_field() has been supported all the way back to PHP 3, so it's hardly an old version of PHP that's causing problems.

    Made some minor alterations to your script (like changing the shorthand tag to <?php, adding my user/passw, a valid DB/table and added an opening <table> tag).

    Your query contains some unneccessary characters:

    "EXPLAIN SELECT * FROM `main` WHERE 1 AND `fam` LIKE \'Malvaceae\'"
    equals
    "EXPLAIN SELECT * FROM `main` WHERE `fam` LIKE 'Malvaceae'"

    You don't need to escape the single quotes inside the double quoted query.
    (Don't know why you're using the EXPLAIN syntax in a script, nor the purpose of the WHERE clause here)

    It's good practise to add an errorchecking line after the query:
    $result=mysql_query($sql) or die(mysql_error());

    That way you will see in clear text what's wrong with your query if that's causing the problems.

    I'm not sure what you want to achieve with your query as it is, but it does analyze the table and return a printable resultset.


    Quote Originally Posted by Arkantos
    also i would like to know whether its true that server side scripts like php files dont need any kind of authorization for access to MySQL databases? or is it only for read access??
    I'm not sure what you mean by this, as your script contains a login towards the DB? (mysql_connect('host', 'user', 'password') ). (you could add the "or die()" syntax here as well, to get a description of a potential connect error, and preventing the script from continuing listing "invalid argument" for all remaining mysql operations.)

    Without valid credentials, the script would be denied access to the DB. You can define different levels of access rights in the 'user' table in the 'mysql' database. Meaning you could write a script that's only allowed READ rights or similar.
    S. Rosland

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    In addition to rosland's recommendations, in such cases where it appears as if the script is in an infinite loop, it is a good idea to put in a `flush();' call right before the closing bracket for the loop. This will force whatever output has been created so far to be sent to the browser, so you can see what's being done as it happens.

  5. #4
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    rosland, what wonder!
    i changed the "explain select" line to what you mentioned, and made the small changes you mentioned, and these errors have gone.

    but there is a new error.
    it says:
    Code:
    parse error, unexpected T_VARIABLE in 1.php on line 11
    i think this is due to the unopened table tag
    i'll see if i can do it, if i get stuck, i know where to find you guys!

    thanx,
    Ark!
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  6. #5
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    okay, the T_VARIABLE error on line 11 has gone but this is the latest error.

    the code redux:
    Code:
    <body>
    <table>
    <?php
    //make the database connection
    $conn = mysql_connect("localhost", "root", "triadpass");
    mysql_select_db("UPI", $conn);
    
    $sql = "EXPLAIN SELECT * FROM `main` WHERE `fam` LIKE 'Malvaceae'";
    
    $result = mysql_query($sql, $conn);
    
    //get field names
    print "<tr>\n";
    while ($field = mysql_fetch_field($result)){
      print " <th>$field->name</th>\n";
    } // end while
    print "</tr>\n\n";
    
    //get row data as an associative array
    while ($row = mysql_fetch_assoc($result)){
      print "<tr>\n";
      //look at each field
      foreach ($row as $col=>$val){
        print " <td>$val</td>\n";
      } // end foreach
      print "</tr>\n\n";
    }// end while
    
    print "\n";
    flush();
    ?>
    </table>
    	</body>
    	</html>
    the error:
    Code:
    table type possible_keys key key_len ref rows Extra 
    main  ALL                                185  Using where
    i added the spaces as this was a table.

    the table i am referring to (MySQL>>UPI>>main) contains these coluumns:
    Code:
    +-------+--------------+------+-----+---------+-------+
    | Field | Type         | Null | Key | Default | Extra |
    +-------+--------------+------+-----+---------+-------+
    | id    | int(11)      |      | PRI | 0       |       |
    | sc_nm | varchar(35)  | YES  |     | NULL    |       |
    | fam   | varchar(30)  | YES  |     | NULL    |       |
    | iden  | varchar(50)  | YES  |     | NULL    |       |
    | aka   | varchar(100) | YES  |     | NULL    |       |
    | sans  | varchar(50)  | YES  |     | NULL    |       |
    | eng   | varchar(75)  | YES  |     | NULL    |       |
    | vern  | varchar(200) | YES  |     | NULL    |       |
    | prts  | varchar(50)  | YES  |     | NULL    |       |
    | dci   | text         | YES  |     | NULL    |       |
    +-------+--------------+------+-----+---------+-------+
    oh please tell where i should beat myself!

    in the ideal world the code would display all the rows where the "fam" coloumn data is equal to "Malvaceae"

    another question, @rosland, what difference is there between "explain select" and "select" and what does the added "explain" do??

    thanx,
    Ark!
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  7. #6
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    That doesn't really look like an error to me. Might be the `explanation' that you no doubt request when using EXPLAIN instead of just plain SELECT. The MySQL manual page on EXPLAIN says "When you precede a SELECT statement with the keyword EXPLAIN, MySQL explains how it would process the SELECT, providing information about how tables are joined and in which order." This would appear to be what you're getting. If you remove the EXPLAIN, you should get the appropriate data returned instead of an explanation as to how the SELECT would be executed.

  8. #7
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I suspected you put the 'EXPLAIN' there by accident.

    Like Shadowfiend says, if you remove 'EXPLAIN' from the query, it will execute normal (and return the data you were looking for).
    S. Rosland

  9. #8
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    you guys are geniouses!!!
    it worked. YAHOOOO!

    i removed the "explain" word and the darn thing returned the query results, thank you, thank you, thank you.

    actually, i am trying to learn html, php and mysql all at once. and this was an example i copied from a book and tried to modify to fit my needs. guess the code given in the book was wrong.

    do you know how to fit the data in a formatted page?
    can i do a query that will output a single row, and then assign the cellular data to different variables like
    $name=<code>coloumn 1
    $address=<code>coloumn 2
    then use these variables in a table?

    thanx for all the help!
    Arkantos.
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  10. #9
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Sure.
    You can embed php code within the HTML, meaning you don't have to seperate the code completely, but can have small snippets of it within the HTML code.

    If you have a more specific question, like "this is my HTML code, and I want to retrieve this data from this table, and print it here in the code", you will be given fairly prompt response here.

    With regard to separating HTML and script code.
    If you have a large site with a similar template on all pages, it would be better to separate code and HTML for maintanance purposes.
    You can achieve this by using templates. Not the DreamWeaver type templates, but PHP templates. You would only need to change the layout one place, and all other pages based on that template would change.
    The templated pages are created real time, not like DreamWeaver which physically rewrites all pages that use a certain template, when you change the template. (It is fairly complex though).
    S. Rosland

  11. #10
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    thank you rosland. i'll do as you say, and try to develop the code. then if it goes wrong, i'll revert back.

    Thanx again,
    Ark!
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:14 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com