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
https://forums.phpfreaks.com/topic/139352-only-returning-one-row/
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!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.