php functions help
I am trying to use a php function to get the data out of a mysql database.
The code in the function:
function get_content() {
$contentxy = mysql_query("SELECT * FROM $content_table WHERE url='$uri'");
while($row21 = mysql_fetch_array($contentxy))
{
$ctitle = $row21[ 'title'];
$author = $row21[ 'author'];
$makedate = $row21[ 'creation_date'];
$category = $row21[ 'category'];
$display = $row21[ 'content'];
$views = $row21[ 'pageviews'];
$cuid = $row21[ 'id'];
$rank = $row21[ 'pagerank'];
}
}
And the function called
get_content();
//here we use the function results to print content to the page
echo 'home > ' . $category . ' > ' . $ctitle;
echo '<h1>' . $ctitle . '</h1>';
echo '<h3>' . $author . '</h3>';
echo '<p>' . $display . '</p>';
echo '<br />Last edited on: ' . $makedate . '<br />';
When I use this code I get a php error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /websites_local/www/postit/test-4/functions.php on line 39
line 39 is:
while($row21 = mysql_fetch_array($contentxy))
Any help will be appriciated
greg posted this at 17:24 — 12th December 2008.
He has: 1,581 posts
Joined: Nov 2005
You have to send variable data into the funtion when you call it, otherwise it wont know what $content_table or $uri is.
Then before you call your function, assign data to those vars
Also, variables inside a function like that, wont carry their values outside the function
For example,
$ctitle = $row21[ 'title'];
, outside the function$ctitle
will not have the value it was assigned with inside the function. If you want it to have you need to assign that variable as a global by doing thisglobal $ctitle;
you can add more vars you want to be global by separating them witha comma and space
global $ctitle, $author, $makedate;
etc$contentxy
doesn't need to be global, as it only uses the data inside the function.So your code with the vars set to global, and sending var data to the form would look like this
<?php
function get_content($content_table, $uri) {
//set any vars you want to keep their data outside this function
global $ctitle, $author, $makedate, $category, $display, $views, $cuid, $rank;
$contentxy = mysql_query("SELECT * FROM $content_table
WHERE url='$uri'");
while($row21 = mysql_fetch_array($contentxy))
{
$ctitle = $row21['title'];
$author = $row21['author'];
$makedate = $row21['creation_date'];
$category = $row21['category'];
$display = $row21['content'];
$views = $row21['pageviews'];
$cuid = $row21['id'];
$rank = $row21['pagerank'];
}//end while
}//end function
//these following are the vars used in the query
//now with the data you want the query to use
$content_table = "my_table_name";
$uri = "my_url";
//send these vars into the function
get_content($content_table, $uri);
//here we use the function results to print content to the page
echo 'home > ' . $category . ' > ' . $ctitle;
echo '<h1>' . $ctitle . '</h1>';
echo '<h3>' . $author . '</h3>';
echo '<p>' . $display . '</p>';
echo '<br />Last edited on: ' . $makedate . '<br />';
?>
Unless all your tables have exactly the same column names, this funtion will fail then too, as it will need to have:
author, creation_date, category, content, pageviews
As those are what data field names you ask for the function to return from mysql table. If they are not there, you wont get the data you expect
Another thing is you get the results via a
while
. Usually you use a while when you are getting more than one row, whereas in your case you are only echoing variables and not arrays, you can onyl have one row's data.So you can just assign the results to an array if you wish
EG
<?php
$contentxy = mysql_query("SELECT * FROM $content_table
WHERE url='$uri'");
$row21 = mysql_fetch_assoc($contentxy);
$ctitle = $row21['title'];
$author = $row21['author'];
$makedate = $row21['creation_date'];
$category = $row21['category'];
$display = $row21['content'];
$views = $row21['pageviews'];
$cuid = $row21['id'];
$rank = $row21['pagerank'];
?>
Much required reading for you...
http://uk.php.net/global
cmoyer posted this at 17:38 — 12th December 2008.
He has: 131 posts
Joined: Jun 2008
Thanks Greg, that fixed my problem.
All I had to do was add the global values and it worked like a charm.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.