# Extracting data from relational databases



## Wozzer (Jul 9, 2012)

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:

```
<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.


----------



## digibucc (Jul 10, 2012)

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.


----------



## Disparia (Jul 11, 2012)

There's a space in that table name, so it needs to be escaped with backticks.


```
$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 (Jul 11, 2012)

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.


----------



## Easy Rhino (Jul 12, 2012)

just FYI, technically evidence db is a schema and not a database.


----------



## Mindweaver (Jul 12, 2012)

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. 

EDIT: Nice catch Easy Rhino!


----------



## Aquinus (Jul 12, 2012)

You need error checking, my friend.

Change this:

```
mysql_connect("localhost","root","");//database connection
mysql_select_db("evidence db");
				
$order = "SELECT * FROM m cases";
				
$result = mysql_query($order);
```

To this:

```
$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.)



Wasley said:


> Error:
> Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\helloworld.php on line 25


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.

```
$order = "SELECT * FROM `m cases`";
```

Lastly, the mysql functions you are using are deprecated. Look into MySQLi or ADODB.


----------



## Wozzer (Jul 14, 2012)

Thanks for the input. It's now working


----------



## Wozzer (Jul 15, 2012)

Has anyone ever had problems with dreamweaver displaying the page layout differently to that shown on a web browser?


----------



## Kreij (Jul 15, 2012)

I haven't but that probably because I never used Dreamweaver. 

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.


----------



## Aquinus (Jul 15, 2012)

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.


----------



## Wozzer (Jul 16, 2012)

Just out of interest - What do you guys use?


----------



## W1zzard (Jul 16, 2012)

Wasley said:


> Just out of interest - What do you guys use?



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


----------



## digibucc (Jul 16, 2012)

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.


----------



## Aquinus (Jul 17, 2012)

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.


----------

