PHP and MySQL Help

Discussion in 'PHP' started by midwestbonsai, Dec 2, 2007.

  1. #1
    Howdy Folks,

    I need some major help with MySQL and PHP.

    I have been able to import a file into MySQL using this http://ceo4u.com/data/

    Now I just need to figure out how to call it and sort it using PHP.

    It is one table with 5 fields.
    `magazine`, `date`, `article`, `category`, `entry_id_num`

    I have had little success with MySQL in the past.

    I would sure like to get some help from one of the experts here.
     
    midwestbonsai, Dec 2, 2007 IP
  2. krampus

    krampus Active Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #2
    I'am not sure what do you want to do with that file?

    How do you think.. "figure out how to call it and sort it using PHP"
    there is lot of procedures of how to deal with data from Mysql.. what exactly do you need to do?
     
    krampus, Dec 2, 2007 IP
  3. midwestbonsai

    midwestbonsai Well-Known Member

    Messages:
    402
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Sorry for being vague.
    What I want to do is display on a web page the info in the database.

    First display everything, the have a way to select specifics.

    such as the magazine, or the category, or both, then list them by date.

    Does this help?
     
    midwestbonsai, Dec 2, 2007 IP
  4. krampus

    krampus Active Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    88
    #4
    ok.. so.. you need to find a way to select certain fields from database and display it on the web page..

    if this is correct you will need:

    - write a srcipet that connects PHP to your database
    - once you have connected to database (DB) you will need to write a certain QUERY that describes what you want to pull from DB
    - then you will have those data in one array (variable), and you could put it on the website where you want.

    Now.. you must see some tutorial of HOW TO CONNECT to database. Look at this one:

    http://www.php-mysql-tutorial.com/

    Make sure to look at the chapters:

    - Connect to MySQL Database

    and

    - Get Data From MySQL Database


    Good luck :)
     
    krampus, Dec 2, 2007 IP
  5. midwestbonsai

    midwestbonsai Well-Known Member

    Messages:
    402
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Thats a great tutorial site, thanks!

    I must be doing something wrong, because I get an error while following the instructions in your link.

    <?php
    include 'config.php';
    include 'opendb.php';
    
    $query  = "SELECT * FROM $dbname";
    $result = mysql_query($query);
    
    [COLOR="Red"]while($row = mysql_fetch_row($result))[/COLOR]
    {
        $mag    = $row[0];
        $date = $row[1];
        $title = $row[2];
        $cat = $row[3];
    
    
        echo "Name :$mag <br>" .
             "Subject : $date<br>" .
             "Message : $title <br><br>" .
             "Subject : $cat<br>" ;
    }
    
    include 'closedb.php';
    ?>
    
    Code (markup):
    This gives me the following error.
    Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in . . . line 8
    Line 8 is the red line above.
    Thanks
     
    midwestbonsai, Dec 2, 2007 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    This line suggests you are trying to select fields from a database and not a table:

    $query  = "SELECT * FROM $dbname";
    Code (markup):
    Can you confirm whether the value within the variable $dbname is a table

    Brew
     
    Brewster, Dec 2, 2007 IP
  7. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #7
    Yeah, isn't $dbname for the mysql connection rather than queries?
     
    live-cms_com, Dec 2, 2007 IP
  8. midwestbonsai

    midwestbonsai Well-Known Member

    Messages:
    402
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #8
    I have changed where it said $dbname to the name of the table, but I still get the same error for the same line.
     
    midwestbonsai, Dec 2, 2007 IP
  9. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #9
    That error message means that the SQL query failed.

    Do an
    echo "SELECT * FROM $dbname";
    to see what SQL query is being executed and see if it makes sense.
     
    live-cms_com, Dec 2, 2007 IP
  10. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Change this line:

    $result = mysql_query($query);
    PHP:
    to this:

    if ( !($result = @mysql_query( $query ) ) ) die( mysql_error() ): 
    PHP:
    Let us know the error that gets displayed.

    Brew
     
    Brewster, Dec 3, 2007 IP
  11. midwestbonsai

    midwestbonsai Well-Known Member

    Messages:
    402
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #11
    Well, here is my current code.
    
    <?php
    include 'config.php';
    
    include 'opendb.php';
    
    
    $query  = "SELECT * FROM 'table_name'";
    if ( !($result = @mysql_query( $query ) ) ) die( mysql_error() );
    
    while($row = mysql_fetch_row($result))
    {
        $mag    = $row[0];
        $date = $row[1];
        $title = $row[2];
        $cat = $row[3];
    
    
        echo "Name :$mag <br>" .
             "Subject : $date<br>" .
             "Message : $title <br><br>" .
             "Subject : $cat<br>" ;
    }
    include 'closedb.php';
    ?>
    
    PHP:
    My error is the following:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''table_name'' at line 1
    Am I just screwing this all up?

    
    <?php
    include 'config.php';
    include 'opendb.php';
    
    
    $query  = "SELECT * FROM 'table_name'";
    $result = mysql_query($query);
    
    echo "SELECT * FROM 'table_name'";
    
    include 'closedb.php';
    ?>
    
    PHP:
    Using the code above I get
    SELECT * FROM 'table_name' displayed on the screen, hey at least it wasn't an error.
     
    midwestbonsai, Dec 3, 2007 IP
  12. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #12
    In PHP/MySQL you use `Table_Name`, not 'Table_Name'.

    `table` - for table names or column names.
    'value' - for strings like SELECT * FROM `table` WHERE `column` = 'string'
     
    live-cms_com, Dec 3, 2007 IP
  13. midwestbonsai

    midwestbonsai Well-Known Member

    Messages:
    402
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    105
    #13
    Would you look at that replacing the " ' " with a " ` " worked!
    I don't know where they are on the keyboard, but it still worked!
    Thanks guys!
    I will let you know if I have any more troubles.
     
    midwestbonsai, Dec 3, 2007 IP