Jump to content

only returning one row


Noskiw

Recommended Posts

<?php

$con = mysql_connect("localhost","115886","youtube");
$db = mysql_select_db(115886, $con);

function f(){
$sql = "SELECT username FROM users";
$res = mysql_query($sql) or die(mysql_error());
echo "USERS:<br>";
$row = mysql_fetch_assoc($res);
$sql2 = "SELECT id FROM users";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);

echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td>Username</td><td>ID</td></tr>\n";
echo "<tr><td>".$row['username']."</td><td colspan=\"2\">".$row2['id']."</td></tr>\n";
echo "</table>\n";
}

f();

?>

 

it returns one username and one id from my table.

 

i know it's in a function. but who cares.

 

this is quite annoying. but it was only a test. just to see if functions work. but i really want it to work properly.

Link to comment
Share on other sites

you need to loop through the results....check this exmple from php.net out...

<?php

$conn = mysql_connect("localhost", "mysql_user", "mysql_password");

if (!$conn) {
    echo "Unable to connect to DB: " . mysql_error();
    exit;
}
  
if (!mysql_select_db("mydbname")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}

$sql = "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1";

$result = mysql_query($sql);

if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

mysql_free_result($result);

?>

http://us.php.net/mysql_fetch_assoc

 

other than that i think you will be all gravy....i gotta run otherwise i would try to set it to your specific code.... :-)

 

cheers!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.