www.webdeveloper.com
Results 1 to 3 of 3

Thread: Php sql error when trying to fetch data!

  1. #1
    Join Date
    Sep 2010
    Posts
    1

    Question Php sql error when trying to fetch data!

    Hello i'm making a website and i'm new to sql so i get an error:
    the website is http://aboutraveling.com and the code is:

    <?

    $story = $_GET['sid'];

    // establish connection to the database containing the quotes.
    $db = mysql_connect("localhost", "*****", "*****") or die ("Unable to connect to database.");
    mysql_select_db("******") or die ("Unable to select database.");
    // select the quotes that have not been displayed (q_mark = 0).


    $author_id = mysql_query("SELECT author_id from travelstories WHERE story_id = $story");
    $ts_username = mysql_query("SELECT username from community_users WHERE user_id = $author_id ");


    while ($row = mysql_fetch_array($ts_username)) {
    echo $row['username'];
    }


    ?>

    the error i get is:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/aboutra/public_html/travelstories_single.php on line 15

    i'm trying for several hours to fix it but i cannot

  2. #2
    Join Date
    Jan 2006
    Location
    MN
    Posts
    440
    Your $author_id is likely null since you don't execute a mysql_query to get $author_id.

    PHP Code:

    $result 
    mysql_query("SELECT author_id from travelstories WHERE story_id = $story");

    while (
    $row mysql_fetch_array($result)) {
    $author_id $row['author_id'];
    }

    $ts_username mysql_query("SELECT username from community_users WHERE user_id = $author_id ");


    while (
    $row mysql_fetch_array($ts_username)) {
    echo 
    $row['username'];


  3. #3
    Join Date
    Apr 2010
    Posts
    213
    Or perhaps the query failed... try this:
    Code:
    $ts_username = mysql_query("SELECT username from community_users WHERE user_id = '$author_id' ");
    (Note the single quotes around $author_id)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles