FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need Help integrating php variable into mySQL SELECT query





hobbkins
Alright, I've tried to fix this bug for the last hour, and I'm definately stuck. I'm relatively new to php AND mySQL, so I'm sure this is just a stupid syntax mistake.

So here's my problem:
I call this script from a link on the main articles page.
(ex. url "http://bubinski.frih.net/articles/article.php?id=3")
article.php is a template, and I have this little script that looks through my articles table and pulls up the proper article that matches the id in the url. The entire system works when I manually substitute the $id for a number, like 3, but when I leave it as a varaiable, I get this error:

Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/hobbkins/domains/bubinski.frih.net/public_html/articles/article.php on line 11


Which makes me assume the proper row isn't being returned at all.

and now here's the code:
Note this is just the code in the header section, which retrieves the row, splits the row up into an array of values, and then assigns those values to the proper variables. Later on in my script they are echo'd into the html document.

here's the script in action (unfinished)
http://bubinski.frih.net/articles/

Code:
<?php
$pageType="articles";

require_once($_SERVER['DOCUMENT_ROOT']."/include/config.php");
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());

//problem area, I believe
$result = mysql_query("SELECT * FROM articles WHERE id=$id");
//note, id is a field in the articles table that is unique and incremented by one each time an article is added to my site

while($r = mysql_fetch_array($result)) {
$title = $r['title'];
$date = $r['date'];
$author = $r['author'];
$entry = $r['entry'];
$thisPage = $title;
}
?>


thanks in advance.
hexkid
hobbkins wrote:
[...] I get this error:

Quote:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/hobbkins/domains/bubinski.frih.net/public_html/articles/article.php on line 11


Code:
<?php
// ...
//problem area, I believe
$result = mysql_query("SELECT * FROM articles WHERE id=$id");

### $result is either a resource identifying the record(s)
### returned from the database or false if there was an error
#
### You didn't test if $result was valid ... and

while($r = mysql_fetch_array($result)) {
#
### mysql_fetch_array(false) gives the Warning you mentioned.
#
}
?>


I believe your variable $id in the problem line is not set to the value in the query string. Recheck your assignment. To understand why there are errors in SQL I usually do SQL stuff like this:
Code:
<?php
// $id = (int)$_GET['id'];
// if ($id<=0) exit('Go away!');
$sql = "select * from articles where id=$id";
$res = mysql_query($sql);
if (!$res) {
  // error in query; to keep it simple we'll just print the error
  // to the browser and exit
  echo 'Error in SQL: <b><tt>', mysql_error(), '</tt></b>.<br>';
  echo 'The SQL was <b><tt>', $sql, '</tt></b>';
  exit(0);
}
while ($r = mysql_fetch_array($res)) {
  // do stuff with $r
}
mysql_free_result($res);
?>
Philip
for safety sake, try this one

if (isset($id))
{
//problem area, I believe
$result = mysql_query("SELECT * FROM articles WHERE id='".$id."'");
//note, id is a field in the articles table that is unique and incremented by one each time an article is added to my site

while($r = mysql_fetch_array($result)) {
$title = $r['title'];
$date = $r['date'];
$author = $r['author'];
$entry = $r['entry'];
$thisPage = $title;
}else
{
die ("error message");
}
hobbkins
you're beautiful hexkid. I hadn't learned about assigning url variables to php variables yet using _GET. Thanks for the heads up.

Problem Fixed. Thanks again Frihost community.
Related topics
PHP magic_quotes_gpc question
Inserting data to MYSQL with a PHP script
Need help integrating html code into php
storing current page address as php variable
Need Expert help in php-mysql
Help importing CSV file into MySQL database
Getting PHP variable to work in FCKeditor
Caching Dynamic Images -- no luck
Formatting mySQL date
help getting form info into mysql db
Need help on PHP programming....
mySQL help needed, any other good database thing?
A variable number of inputs for PHP to insert MYSQL
Any one can help 4 PHP object coding self learning site?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.