SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question inserting variable in mysql using php

    hey folks, i m trying to insert and echo back data aganist that query i made this code
    Code PHP:
    <?php
    $cue = $_POST['fname'];
     
    $result = ("SELECT * FROM info WHERE names = '$cue'")
    [B]
    while ($row = mysql_fetch_array($result))[/B]
    {
    echo $row["name"];
    echo "<br/>";
    }
    ?>
    the bold line gives a error. i think i m doing wrong quotes on $result query too. wht to do.......
    All those who wander aren't lost.

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You assigned $result a string, you didn't actually run any query against the database

    You need to call mysql_query()

    PHP Code:
    $result mysql_query("SELECT * FROM info WHERE names = '" mysql_real_escape_string($cue) . "'"); 

  3. #3
    SitePoint Wizard nofel's Avatar
    Join Date
    Aug 2007
    Location
    Earth
    Posts
    1,766
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    <?php
    $result = mysql_query("SELECT * FROM info WHERE names = '" . mysql_real_escape_string($cue) . "'");
    while ($row=mysql_fetch_array("$result")
    {
    echo $row["name"];
    echo "<br/>";
    }
    ?>
    now i m getting error on line 15 which is bold. am i doing it wrong or wht
    All those who wander aren't lost.

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You have more opening parentheses ( than closing ) on the previous line.

    Code:
    while ($row = mysql_fetch_array($result))
          ^                        ^       ^^
                                            | 
                                            missing
    And don't add quotes around $result, it's not a string, it's a pointer to a resource in memory holding the buffered result set of the query

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    1. Missing a closing parenthesis on line 2 at the end
    2. $result should not have quotes around it.
    3. SQL statement needs to be delimited by a semicolon
    4. [don't use double quotes unless you need to embed variables in a string]

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    3. SQL statement needs to be delimited by a semicolon
    Not in PHP with mysql_query

    Quote Originally Posted by http://us.php.net/function.mysql-query

    resource mysql_query ( string $query [, resource $link_identifier ] )

    Parameters

    query

    A SQL query

    The query string should not end with a semicolon.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $rResult 
    mysql_query(
        
    sprintf(
            
    "SELECT name FROM info WHERE names = '%s'",
            
    mysql_real_escape_string($_POST['fname'])
        )
    );
    printf(
        
    '<h4>%s matching record(s) found.</h4>',
        
    mysql_num_rows($rResult)
    );
    while(
    $aRecord mysql_fetch_assoc($rResult))
    {
        
    printf(
            
    '%s<br />',
            
    $aRecord['name']
        );
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Dan Grossman
    SQL statement needs to be delimited by a semicolon
    News to me. Then again I always use prepared statements which negate a delimiter.


Bookmarks

Posting Permissions

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