1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Extracting data from relational databases

Discussion in 'Programming & Webmastering' started by Wozzer, Jul 9, 2012.

  1. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.47/day)
    Thanks Received:
    68
    Location:
    England
    Hi,

    New to phpmyadmin, PHP, MySQL, etc - so please excuse me if the answer to the question is simple.

    I've built a database using XAMPP called "evidence db". Within that database, I have a table called "m cases". This table has 11 columns.

    I currently have the following code:
    Code:
    <html>
    <head>
    <title>Search data</title>
    </head>
    <body>
    <table>
      <tr>
        <td align="center">EMPLOYEES DATA</td>
      </tr>
      <tr>
        <td>
          <table border="1">
          <tr>
            <td>Case ID</td>
          </tr>
    <?
    
    mysql_connect("localhost","root","");//database connection
    mysql_select_db("evidence db");
    				
    $order = "SELECT * FROM m cases";
    				
    $result = mysql_query($order);	
    
    while($data = mysql_fetch_row($result)){
    $field1= $row["Case ID"];
      echo("$field1");
    }
    ?>
        </table>
      </td>
    </tr>
    </table>
    </body>
    </html>
    Error:
    Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\helloworld.php on line 25

    Thanks in advanced.

    PS - If anyone can recommend PHP/Phpmyadmin tutorials - i'd be interested in reading them.
     
  2. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,965 (2.39/day)
    Thanks Received:
    1,514
    i recommend checking out http://www.phpfreaks.com/, not to advert other forums but they are dedicated to php coding and will likely get you solved much faster than here, unless one of the few gurus happen to post.

    also, all you ever need other than a forum is the manual pages
    http://php.net/manual/en/index.php

    more specifically, the man page for mysql_fetch_row(), here:
    http://php.net/manual/en/function.mysql-fetch-row.php

    as it says, it is recommend with current php versions to use a different mysql extension entirely, with good reason. you may want to consider that. good luck.
     
    Wozzer says thanks.
  3. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,492 (1.28/day)
    Thanks Received:
    658
    Location:
    Geneva, FL, USA
    There's a space in that table name, so it needs to be escaped with backticks.

    Code:
    $order = "SELECT * FROM `m cases`";
    In the future, don't put spaces in your table or column names. Use an underscore (m_cases) or go camel case (mCases).

    Also, if you're just starting to learn, I'd suggest going with their PDO interface instead of the database specific functions.

    http://www.php.net/pdo

    Been meaning to put together a small intro to PDO for the forum, with the recent rise in PHP/database questions. Maybe I'll get around to doing it now that I said I would... ;)
     
    Wozzer and digibucc say thanks.
  4. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.47/day)
    Thanks Received:
    68
    Location:
    England
    Did put the backticks and have also updated the DB name to have no spaces - still no luck. Will have a better look at it tomorrow. Thanks for the input.
     
  5. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,589 (4.53/day)
    Thanks Received:
    3,421
    just FYI, technically evidence db is a schema and not a database.
     
    Mindweaver and digibucc say thanks.
  6. Mindweaver

    Mindweaver Moderato®™ Staff Member

    Joined:
    Apr 16, 2009
    Messages:
    5,453 (2.58/day)
    Thanks Received:
    3,053
    Location:
    Statesville, NC
    You don't have a password setup for "root"? I would set a password or actually go one step further and change the "root" user id to something else completely, but that's just me. :toast:

    EDIT: Nice catch Easy Rhino! :toast:
     
    Crunching for Team TPU
  7. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    7,061 (6.42/day)
    Thanks Received:
    2,603
    Location:
    Concord, NH
    You need error checking, my friend.

    Change this:
    PHP:
    mysql_connect("localhost","root","");//database connection
    mysql_select_db("evidence db");
                    
    $order "SELECT * FROM m cases";
                    
    $result mysql_query($order);
    To this:
    PHP:
    $conn mysql_connect("localhost","root","");//database connection
    if(!$conn) {
        throw new 
    Exception('Unable to connect');
    }

    $dbname 'evidence db';
    if(!
    mysql_select_db($dbname)) {
        throw new 
    Exception('Unable to use db: {$dbname}');
    }
                    
    $order "SELECT * FROM `m cases`";
                    
    if(!
    $result mysql_query($order$conn)) {
        throw new 
    Exception("Query failed.\n".mysql_error($conn));
    }
    Since your table also has a space in it, you need to denote the table name using back ticks. It may allow double quotes for table names as well, but I don't recall (I use PostgreSQL too often.)

    Your query is throwing an error (mysql_query(), must be returning a non-object and the only non-object it returns is false on an error.) Try out the back-ticks, that probably will fix it.
    PHP:
    $order "SELECT * FROM `m cases`"
    Lastly, the mysql functions you are using are deprecated. Look into MySQLi or ADODB.
     
    Last edited: Jul 12, 2012
  8. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.47/day)
    Thanks Received:
    68
    Location:
    England
    Thanks for the input. It's now working

    :toast:
     
  9. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.47/day)
    Thanks Received:
    68
    Location:
    England
    Has anyone ever had problems with dreamweaver displaying the page layout differently to that shown on a web browser?
     
  10. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.76/day)
    Thanks Received:
    5,620
    Location:
    Cheeseland (Wisconsin, USA)
    I haven't but that probably because I never used Dreamweaver. :D

    Okay, I'm not trolling. I've noticed that in several IDE environments that what the IDE shows is not always the same as when it runs in a browser or as an application. For instance, working with WPF in VS2010, what is shown in the IDE is not always the same as when you run the app outside of the IDE.
    Minor emulation differences I assume.

    Best to check everything in its final resting place (like in a browser) than count on the display in the tools to be accurate.
     
  11. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    7,061 (6.42/day)
    Thanks Received:
    2,603
    Location:
    Concord, NH
    Dreamweaver has its own HTML render. You will find that with certain setting, they will look a little different between different browsers, be it Firefox, Chrome, IE, etc. When testing, you really should check all the different browsers that you're planning to support. That's the only way to make sure it works on all of them the way you have your HTML and CSS configured because two browsers might handle padding, margins, and borders differently depending on the settings.

    My rule of thumb is: Don't use Dreamweaver. :)
     
  12. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,133 (0.47/day)
    Thanks Received:
    68
    Location:
    England
    Just out of interest - What do you guys use?
     
  13. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,138 (3.87/day)
    Thanks Received:
    12,099
    i use dreamweaver for the rough layouting. any text editor (ultraedit in my case) + firefox firebug for the details

    if you are asking what database: tpu uses mysql everywhere, memcached for caching
     
  14. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,965 (2.39/day)
    Thanks Received:
    1,514
    i use jetbrains phpstorm. if i were to use a wysiwyg it would be dreamweaver, but i don't do much real layout work, and stick to coding.
     
  15. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    7,061 (6.42/day)
    Thanks Received:
    2,603
    Location:
    Concord, NH
    I use vim to edit PHP, my database of choice is PostgreSQL. Even when I do layout, I usually write the CSS by hand and I use various browsers to test it. I prefer Chrome, but not everyone uses Chrome, so I keep Firefox and IE handy as the most commonly used.
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page