<?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; }
?>
Retrieve data from mysql db
Page 1 of 16 Replies - 579 Views - Last Post: 06 February 2011 - 02:58 PM
#1
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.
Replies To: Retrieve data from mysql db
#2
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:
An example:
mysql_query($queryString) or die(mysql_error());
#3
Re: Retrieve data from mysql db
Posted 05 February 2011 - 11:34 PM
Sweet. Thanks. Updating to this:
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.
<?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.
#4
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.
#5
Re: Retrieve data from mysql db
Posted 06 February 2011 - 12:30 AM
macosxnerd101, 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'];
}
}
?>
#6
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.
#7
Re: Retrieve data from mysql db
Posted 06 February 2011 - 02:58 PM
macosxnerd101, 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).
robahas, 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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|