Some help with some php mysql communication.

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
Okay so I'm trying to make a online store using php mysql and flash but I can't seem to get tit to work.



Code:
[B]Warning[/B]:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in [B]/backup/home/mitchell/public_html/products.php[/B] on line [B]12[/B]
This is the code in its entirty

PHP:
<?php

$link = mysql_connect("localhost","*****_*****","*****");
mysql_select_db("mitchell_test");

$query = 'SELECT * FROM products';
$results = mysql_query($query);

echo "<?xml version=\"2.0\"?>\n";
echo "<products>\n";

while($line = mysql_fetch_assoc($results)) {
    echo "<item>" . $line["product"] . "</item>\n";
}

echo "</products>\n";

mysql_close($link);

?>
Asterixs are the username and password for the database.

Basically what the code should do is display the names of all the rows in the table. But All I get is the error.
this is just the begining steps to my project from here the php will create some xml which the flash file will read.
 
Last edited:

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
Now I get the same error except with Mysql_fetch_array() instead of mysql_fetch_assoc()
Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /backup/home/mitchell/public_html/products.php on line 12

I'm really confused.
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
try:

Code:
$results = mysql_query("SELECT * FROM `products`);

echo "<?xml version=\"2.0\"?>\n";
echo "<products>\n";

while($line = mysql_fetch_array($results)) {
    echo "<item>" . $line['product'] . "</item>\n";
}

echo "</products>\n";
 

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
Now it thinks that the "?" is a parse error. It's telling me:

Code:
[B]Parse error[/B]:  syntax error, unexpected '?' in [B]/backup/home/mitchell/public_html/products.php[/B] on line [B]8[/B]
 
Last edited:

clareto

New Member
Messages
250
Reaction score
0
Points
0
dont output the line that declares the document as xml with an "echo". Do it outside the php

Code:
<?php
 
... (code to initialize the db) 
// next line starts the output 
?>
 
<?xml version= ... ?>
 
<?
 
... (php code that makes the xml schema)
 
 
?>
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
yes that works well too. i normally try to minimize echo statements but when its 3am your kinda tired :)

and use <br> instead of \n
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Just for future reference, if your getting an error from a MySQL "result" function, try using "or die(mysql_error())" after your mysql_query(). You might have something wrong in your actual query, which causes nothing to be returned from the MySQL server, which could possibly be the cause of the error from mysql_fetch_array().

PHP:
...
$blah = mysql_query("QUERY HERE") or die(mysql_error());
...

(If you didn't know.)


Also you are getting that error because you forgot the "ending quote" in your MySQL query:

PHP:
// Should Be:
$results = mysql_query("SELECT * FROM `products`");

// Was:
$results = mysql_query("SELECT * FROM `products`);

;)
 

Sheepoholics

New Member
Messages
266
Reaction score
0
Points
0
Alright I got it all figured out. Just some little things needed fixing First off I needed to be selecting from a table that had and INT row. So after I fixed that and used or die(mysql_error()); (thanks for that Bryon) it was much easier to find the problem. And thanks to jake for all of the 3am help. :)
 

flapietoetoe

New Member
Messages
226
Reaction score
0
Points
0
Next time when u have a problem with MySQL u can always run the query in phpmyadmin as a query and see what it gives as output.
It gives a more detailed error most of the time so u can know for sure if there is a problem with ur php syntax or the query
 
Top