Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Function help!

  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Function help!

    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

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    change this:
    PHP Code:
    $result mysql_query('SELECT cData FROM content where cid=$edit'); 
    to this:
    PHP Code:
    $result mysql_query("SELECT cData FROM content where cid = '$edit'") or exit(mysql_error()); 

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •