Jump to content

[SOLVED] Displaying Things


Darkie

Recommended Posts

Yeah.. I'm teaching myself PHP, so I'm a newb right now. o_o I got this code from a website and edited it to my liking, just so I can learn how to do it. But it doesn't work for me, so I must be doing something wrong.

 

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","username","password");

//select which database you want to edit
mysql_select_db("members"); 

//select the table
$result = mysql_query("select * from users");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
  
   $username=$r["username"];
   $idnumber=$r["idnumber"];
   $password=$r["password"];
   $email=$r["email"];
   $ip_address=$r["ip_address"];
   $signup_date=$r["signup_date"];
   $displayname=$r["displayname"];
   $vcode=$r["vcode"];
   $refer=$r["refer"];
   $modded=$r["modded"];
   $acctkind=$r["acctkind"];
   $lli=$r["lli"];
   $money=$r["money"];


   //display the row
   echo "$username <br> $idnumber <br> $modded <br> $signup_date | $password <br>";
}
?>

 

When I use that code, I get this error.

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/darkgeo/public_html/test.php on line 13

 

Help please?

Link to comment
https://forums.phpfreaks.com/topic/47958-solved-displaying-things/
Share on other sites

Try this.

<?php
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","username","password");

//select which database you want to edit
mysql_select_db("members"); 

//select the table
$result = mysql_query("select * from users");
print "<table border=1>\n";

// get field names
print "<tr>\n";
while ($field = mysql_fetch_field($result)){
  print " <th>$field->name<\th>\n";
}// end while
print "</tr>\n\n";

//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
  print "<tr>\n";
  // look at each field
  foreach ($row as $col=>$val){
   print "<td>$val</td>\n";
  }// end foreach
print "</tr>\n\n";
}// end while
print "</table>";
?>

 

You should always error check queries before trying to use the result. The general syntax is....

 

<?php

  
  if ($result = mysql_query($sql)) { // where $sql holds your query.
    if (mysql_num_rows($result)) {
      while($row = mysql_fetch_assoc($result)) {
        // display data.
      }
    } else {
      echo "No records found";
    }
  } else {
    echo Query failed\n$sql\n" . mysql_error();
  }

?>

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.