Hello. im trying to use this code for a categories table for my website. i am getting this error Warning: mysql_db_query(): supplied argument is not a valid MySQL-Link resource in /home/motorbik/public_html/cat page.php on line 48 this is the code <?php $categ = "Gear Reviews"; // setup SQL statement $SQL = "SELECT * FROM Gear Reviews WHERE categ = '$categ'"; // execute SQL statement $retid = mysql_db_query($db, $SQL, $cid); // check for errors if (!$retid) { echo( mysql_error()); } else { // display results echo ("<p><dt><b>$categ</b><br>\n"); while ($row = mysql_fetch_array($retid)) { $url = $row["url"]; $name = $row["name"]; $description = $row["description"]; echo ("<p><a href='$url'>$name</a></p>\n"); } echo ("</dt></p>"); } ?> PHP:
What's $cid ? Also, in the $SQL, if the table name is Gear Reviews, try enclosing it within quotes. Like $SQL = "SELECT * FROM 'Gear Reviews' WHERE categ = '$categ'"; PHP:
okay well thanks for that, i have no errors now. but i do have another problem the page says No database selected <?php $categ = "Gear Reviews"; // setup SQL statement $SQL = "SELECT * FROM 'Gear Reviews' WHERE categ = '$categ'"; // execute SQL statement $retid = mysql_db_query($db, $SQL); // check for errors if (!$retid) { echo( mysql_error()); } else { // display results echo ("<p><dt><b>$categ</b><br>\n"); while ($row = mysql_fetch_array($retid)) { $url = $row["url"]; $name = $row["name"]; $description = $row["description"]; echo ("<p><a href='$url'>$name</a></p>\n"); } echo ("</dt></p>"); } ?> PHP:
@Kurt Wittingham, firstly ensure that you are connecting to your database before trying to query it. You can do this with the two functions mysql_connect() and mysql_select_db(). Also when querying you database, all that needs to be done is use the mysql_query() function, with one parameter, the query itself. Another thing is the use grave accents or back ticks to surround your table name should be used, not single quotes. However that should be your last resort. Choosing a better table name, or using an underscore between Gear Reviews would be more suitable.
would you be able to code what you mean in the second part of what you said please. im really new to php and dont quite understand
So if you have any spaces or mysql command words in your table/column names, then you should use grave accents around the, like so: $SQL = "SELECT * FROM `Gear Reviews` WHERE categ = '$categ'"; PHP: However as i said above, that should be a last resort. If you need to use them around your table/column names, then you're not picking them wisely enough...
More explicitly, accent grave and commas are used for encapsulating asyntactic strings so MySQL doesn't try to interpret them. For example, Imagine if you decided to decided to call your table DROP DATABASES (don't do this): SELECT * FROM DROP DATABASES WHERE TRUNCATE TABLE = 1; PHP: Big problems.