6 Replies - 1794 Views - Last Post: 06 February 2011 - 02:58 PM Rate Topic: -----

#1 robahas   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-February 11

Retrieve data from mysql db

Posted 05 February 2011 - 10:50 PM

I've just started learning how to interact with mysql using php. I wrote this little bit of code and I can't get anything out of it. Just a blank screen. I'd appreciate any ideas about what I might be doing wrong, as I've been staring at it for too long with not fresh insights! As you can see I'm accessing a db created by WordPress. Eventually I need to write some code to do that.

<?php

//connect to database
$dbc = mysql_connect('localhost', 'wyman', 'hoopla');
mysql_select_db('wyman');

//define query
$query = "SELECT post_content FROM wp-posts ORDER BY post_date_gmt DESC";
$result = mysql_query($query);

//print data
while ($row = mysql_query($result)) {
echo $row; }

?>



Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve data from mysql db

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: Retrieve data from mysql db

Posted 05 February 2011 - 10:53 PM

Add an or die(mysql_error()) clause after you connect to the database, select the db, and query. If there is an error on the SQL end, it will let you know.

An example:
mysql_query($queryString) or die(mysql_error());


Was This Post Helpful? 0
  • +
  • -

#3 robahas   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-February 11

Re: Retrieve data from mysql db

Posted 05 February 2011 - 11:34 PM

Sweet. Thanks. Updating to this:

<?php

//connect to database
$dbc = mysql_connect('localhost', 'tester', 'hoopla') or die(mysql_error);
mysql_select_db('tester') or die(mysql_error);

//define query
$query = "SELECT post_content FROM wp_posts";
$result = mysql_query($query) or die(mysql_error());

//print data
while ($row = mysql_query($result) or die(mysql_error)) {
echo $row; }

?>



I now get the following error:

Warning: mysql_query() expects parameter 1 to be string, resource given in E:\server\www\learnphp\databases\index.php on line 19

Notice: Use of undefined constant mysql_error - assumed 'mysql_error' in E:\server\www\learnphp\databases\index.php on line 19
mysql_error

I gather from poking around that this just means "the data was not assigned as expected" I also switched to a different db, to get a fresh perspective.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: Retrieve data from mysql db

Posted 05 February 2011 - 11:40 PM

You probably want to use the mysql_fetch_row() function instead of querying in your array. Also, mysql_error() is a function, so it needs the parentheses, which you are missing at line 19. Really, no need for an or die() clause at line 19.
Was This Post Helpful? 1
  • +
  • -

#5 littlebro   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 29-December 09

Re: Retrieve data from mysql db

Posted 06 February 2011 - 12:30 AM

View Postmacosxnerd101, on 05 February 2011 - 11:40 PM, said:

You probably want to use the mysql_fetch_row() function instead of querying in your array. Also, mysql_error() is a function, so it needs the parentheses, which you are missing at line 19. Really, no need for an or die() clause at line 19.


<?php

//connect to database
$dbc = mysql_connect('localhost', 'tester', 'hoopla') or die(mysql_error);
mysql_select_db('tester') or die(mysql_error);

//define query
$query = "SELECT post_content FROM wp_posts";
$result = mysql_query($query) or die(mysql_error());

//print data
//mysql_num_rows : use to count data
//mysql_fetch_array : use to get data row by row
if(mysql_num_rows($result) > 0){
   while ($row = mysql_fetch_array($result)) {
     echo $row['post_content']; 
   }
}
?>


Was This Post Helpful? 0
  • +
  • -

#6 robahas   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-February 11

Re: Retrieve data from mysql db

Posted 06 February 2011 - 12:40 AM

Thanks guys! You got me up and running. Now that I have something that works I can expand on it and experiment.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is online

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,571
  • Joined: 08-June 10

Re: Retrieve data from mysql db

Posted 06 February 2011 - 02:58 PM

View Postmacosxnerd101, on 06 February 2011 - 07:40 AM, said:

Really, no need for an or die() clause at line 19.

or rather a "must not put or die()" at line 19, otherwise your script will die after the last result (where mysql_fetch_assoc() will definitely return FALSE).

View Postrobahas, on 06 February 2011 - 08:40 AM, said:

Now that I have something that works I can expand on it and experiment.

an outdated something though … there are more modern and more comfortable database functions available (e.g. PDO).

This post has been edited by Dormilich: 06 February 2011 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1