...

View Full Version : Function help!



angst
07-07-2005, 05:32 PM
I'm trying to make a simple function to pull data from mysql,
but i'm having trouble passing the variable into the function.

my code :

[php]

function GetContent($edit) {

$SqlHost = "localhost";
$SqlUser = "root";
$SqlPass = "sql";
$SqlDb = "test";

$link = mysql_connect($SqlHost, $SqlUser, $SqlPass) or die('Login details incorrect');
$db_selected = mysql_select_db($SqlDb, $link) or die('Database name incorrect');

$result = mysql_query('SELECT cData FROM content where cid=$edit');
$row = mysql_fetch_array( $result );
echo $row['0'];
mysql_close($link);

}
?>


<? GetContent(1) ?>

I can't seem to get the $edit variable into the select statement, if i make the value static, it works. but dynamicly just gives me an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in temp.php on line 18

any ideas what i'm doing wrong here?

thanks in advance for your time!
-Ken

devinemke
07-07-2005, 06:23 PM
change this:


$result = mysql_query('SELECT cData FROM content where cid=$edit');

to this:


$result = mysql_query("SELECT cData FROM content where cid = '$edit'") or exit(mysql_error());

Fou-Lu
07-07-2005, 06:43 PM
Just to expand on devinemke's posting as well, so you understand what its about.

The problem for this is simple, mysql_query is turning up 0 results, hense the reason why mysql_fetch_array cannot pick it up. The solution is simple, you have single quotations around your query, and php is probably not reading the variable within. Its requesing information based on (string)$edit -> which probably doesn't exist.
"SELECT {SELECT} FROM {TABLE} WHERE {WHERE} = {WHAT}"
That will work, though personally, I would escape it, but thats my preference:

"SELECT `cData` FROM `content` WHERE cid = '" . $edit . "'"
For instance.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum