> I want to display the first 3 words of my record in > the my table and following is the code i'm using. > When i use the SQL Query in mySQL.. it works great.. > but when i try to implement it in my php page.. i'm > not getting anything.. no error, no display, no > result.. just blank.. why? > > <?php > > $db = mysql_connect("localhost","user","pass"); > mysql_select_db("mydb",$db); > > $result = mysql_query("SELECT substring_index(title, ' > ', 3) from news order by ID desc limit 4",$db); > if ($myrow = mysql_fetch_array($result)) { > do { > echo(" $myrow[title] "); > } while ($myrow = mysql_fetch_array($result)); > } > ?>
In case you're wondering, your method is fine. You can continue to use this or what chris suggested. Either way, the problem is that you need an alias for your column that you selected. With the above code, you didn't select a column called "title" so you can't use $row['title']. You created a column called "substring_index(title, ' ', 3)" so you'll have to use $row["substring_index(title, ' ', 3)"]. As you can probably guess, there's an easier way to do it, though... If you use a query such as: SELECT SUBSTRING_INDEX(title, ' ', 3) AS f_title FROM news ORDER BY id LIMIT 4 You can then use $row['f_title'] to print the result. Using "AS" in your query will rename the column to "f_title" or whatever you put there. This is called assigning the column an alias. Of course, though all of this you could of just used $row[0] to get the value. I tend to stay away from using numerical indexes though, as it makes it harder for other people to follow your code. When you see $row['f_title'], it gives you a good idea of what you're displaying. When you see $row[0], you have no idea what it is and you have to go find the query that was run in order to determine what column zero was. Hope that helps. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php