Web Hosting Talk







View Full Version : mysql_fetch_array question


Jeanco
12-22-2003, 03:21 AM
Hello, please take a look at the code below and let me know if you see something I'm doing wrong. I'm just learning PHP but judging by whats in the book I can't see anything wrong. The connection to the DB has already been made so tahts not it...


<?php /* Start PRODUCT OF THE MOMENT code */

$result = mysql_query('SELECT * FROM products WHERE ID = 2');

while ($row = mysql_fetch_array($result)) {
echo "<b>{$row['TITLE']}</b> <br>" .
"{$row['SHORT_D']} ...<a href=\"content.php?content={$row['LONG_D']}\">more</a>";
}
?>


I get the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/epasture/public_html/index.php on line 116

Thanks for helping a newbie guys

Steven
12-22-2003, 03:32 AM
try this:




<?php /* Start PRODUCT OF THE MOMENT code */

$result = mysql_query("SELECT title,short_d,long_d FROM products WHERE ID='2'");

while ($row = mysql_fetch_array($result)) {
echo "<b>{$row[0]}</b> <br>" .
"{$row[1]} ...<a href=\"content.php?content={$row[2]}\">more</a>";
}
?>

Jeanco
12-22-2003, 03:45 AM
Nope, still the same error.

Charter
12-22-2003, 03:56 AM
Hi. Try using the following. What's it say?

$result = mysql_query("SELECT * FROM products WHERE ID = 2")
or die("Invalid query: " . mysql_error());

digitok
12-22-2003, 04:04 AM
It's because your query isn't returning anything, make sure the field names are all correct, table names, etc, and try what Charter suggested. Then get back to us.

Jeanco
12-22-2003, 04:06 AM
it says "Invalid query: No Database Selected"

But up at the top of the page I have the following page:

<?php

define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'epasture_ryan');
define('MYSQL_PASS', '***');
define('MYSQL_DB', 'epasture_main');

if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) ) {
die('Failed to connect to host "' . MYSQL_HOST . '". Please contact webmaster at webmaster@epasture.com');
}

else {
mysql_select_db(MYSQL_DB);
?>

Charter
12-22-2003, 04:29 AM
Hi. What's this one say?

mysql_select_db(MYSQL_DB) or die ("Invalid select: " . mysql_error());

Jeanco
12-22-2003, 04:31 AM
Invalid select: Access denied for user: 'epasture_ryan@localhost' to database 'epasture_main'

This is helping :D I'm not sure why I'm not granted access though...

Jeanco
12-22-2003, 04:33 AM
Oh got it, thansk a bunch. I created my usernam and the database, but forgot to add my username to have access to the database *smacks forhead*. Thanks a bunch Charter - really appreciate it.