Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 6th, 2004, 05:29 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default accessing information from a db

Hi,

I'm new w/ php/mysql... I trying to display information from a db.... Here is the code in question.. Below that is how I created the db, it went through ok, then the error I get when I try it out... Any help would be great.

<html>
<body>
<?
    $db = mysql_connect("localhost", "mysql");
    mysql_select_db("mydb",$db);
    $result = mysql_query("SELECT * FROM employees",$db);

    printf("First Name: %s<br>\n", mysql_result($result,0,"first"));
    printf("Last Name: %s<br>\n", mysql_result($result,0,"last"));
    printf("Address: %s<br>\n", mysql_result($result,0,"address"));
    printf("Position: %s<br>\n", mysql_result($result,0,"position"));
?>
</body>
</html>


CREATE TABLE employees ( id tinyint(4) DEFAULT '0' NOT NULL AUTO_INCREMENT, first varchar(20), last varchar(20), address varchar(255), position varchar(50), PRIMARY KEY (id), UNIQUE id (id));INSERT INTO employees VALUES (1,'Bob','Smith','128 Here St, Cityname','Marketing Manager');
INSERT INTO employees VALUES (2,'John','Roberts','45 There St , Townville','Telephonist');
INSERT INTO employees VALUES (3,'Brad','Johnson','1/34 Nowhere Blvd, Snowston','Doorman');

mysql -u mysql mydb < mydb.dump

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/php/db_test.php on line 25
First Name:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/php/db_test.php on line 29
Last Name:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/php/db_test.php on line 33
Address:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/apache/htdocs/php/db_test.php on line 37
Position:

 
Old March 12th, 2004, 04:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

for starters, you should be checking whether the previous mysql functions are successful. Use mysql_error() to determine this.

For example:

$db = mysql_connect("localhost", "mysql");
if ($db == FALSE)
{
   die("Couldn't connect to MySQL. MySQL said: " . mysql_error());
}

$link = mysql_select_db("mydb",$db);
if ($link == FALSE)
{
   die("Couldn't select database. MySQL said: " . mysql_error());
}

$result = mysql_query("SELECT * FROM employees",$db);
if ($result == FALSE)
{
   die("Query failed. MySQL said: " . mysql_error());
}

?>


Also, you should use mysql_fetch_array() instead of several calls to mysql_result() if you're going to be displaying more than one column's data from each row.

    $row = mysql_fetch_array($result);

    echo "First Name: {$row['first']}<br>\n";
    echo "Last Name: {$row['last']}<br>\n";
    echo "Address: {$row['address']}<br>\n";
    echo "Position: {$row['position']}<br>\n";


It's difficult to give you more insight because the line numbers in your error messages do not correspond to the code you posted.

I strongly suggest reading through the MySQL section of the PHP manual:
  http://www.php.net/mysql

Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing sms information abhi2086 C# 3 September 3rd, 2008 09:25 AM
Oage accessing information not under control Ron Howerton Ajax 3 March 28th, 2008 01:14 PM
How to write files information to mysql db using p method PHP Databases 4 February 14th, 2006 09:18 PM
Accessing MySql db from a Servlet(Urgent) balarkavelidi Apache Tomcat 1 January 15th, 2006 10:03 AM
Error 7399 when accessing remote db ?! VerdaFolio Access 1 June 2nd, 2004 10:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.