+ Reply to Thread
Results 1 to 2 of 2

Thread: When using PHP/MySQL

  1. #1
    froger is offline x10Hosting Member froger is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    49

    When using PHP/MySQL

    Lets say we have a test database. In the database we have a table named users. The table has 1 column, name.

    Instead of using something like

    <?php

    $sql = mysql_query("SELECT name FROM users WHERE name='abc'");

    while ($row = mysql_fetch_array($sql))
    {
    echo $row['name'];
    }
    ?>

    would it be faster to just use...

    <?php
    $sql = mysql_fetch_array(mysql_query("SELECT name FROM users WHERE name='abc'"));

    echo $sql['name'];
    ?>

    To fetch a single persons name? I always figured you would use the while statement if you wanted to echo the entire table...

    <?php
    $sql = mysql_query("SELECT name FROM users");

    while ($row = mysql_fetch_array($sql))
    {
    echo $row['name'] . "<br/>";
    }
    ?>

  2. #2
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: When using PHP/MySQL

    If you have one row in the result you definitely don't need the while loop.

+ Reply to Thread

Similar Threads

  1. Replies: 2
    Last Post: 02-01-2010, 05:45 AM
  2. mySQL error - argument is not a valid MySQL result resource
    By Awesomexr in forum Programming Help
    Replies: 3
    Last Post: 12-20-2009, 04:12 PM
  3. Replies: 3
    Last Post: 02-14-2008, 01:38 PM
  4. Replies: 2
    Last Post: 12-17-2007, 03:00 AM
  5. Replies: 2
    Last Post: 11-20-2007, 11:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers