SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sql - what is wrong with this code?

    Code PHP:
    <?php
    //connect to database
    include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    //retrieve last 3 articles from article table
    //sort them with DESC
    //limit a number of characters in text
    $sql = '
    SELECT title, LEFT(text, 250) FROM article ORDER BY date DESC LIMIT 0, 3; 
    ';
    $result = mysqli_query($link, $sql);
    if (!$result)
    {
    	$error = 'Could not return title and text requested: '. mysqli_error($link);
    	include $_SERVER['DOCUMENT_ROOT'] . '/includes/error.html.php';
    	exit();
    }
    while ($row = mysqli_fetch_array($result))
    {
    	$articles[] = array('title' => $row['title'], 'text' => $row['text']);
    }
    include 'articles.html.php';
    ?>
    What can be wrong with the sql I sent? Something must be wrong with the way how I use LEFT() because without it all works fine.
    Thanks,
    Alex.

  2. #2
    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)
    Code:
    SELECT title, LEFT(text, 250) AS text FROM article ORDER BY date DESC LIMIT 0, 3;
    @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.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FYI, SQL itself is not wrong but the array index 'text' returned by mysql_fetch_array() $row['text'] could not be found. That might be causing the error. And the above fix by Anthony will remove your error.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Code:
    SELECT title, LEFT(text, 250) AS text FROM article ORDER BY date DESC LIMIT 0, 3;
    ... crap. Thanks a lot for help!
    Alex.

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2009
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    FYI, SQL itself is not wrong but the array index 'text' returned by mysql_fetch_array() $row['text'] could not be found. That might be causing the error. And the above fix by Anthony will remove your error.
    Thank you!


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
  •