0

I wrote this simple script based on my database table (db_member) with an auto incrementing row I'd and some other columns: 1: 'privilege' column, privilege values can be one of 'A', 'B' or 'C'.

2: 'username' column, for respective usernames

Objective: Output the 'username' on a row where the 'privilege' value is 'A'. There's only one such row in the database.

Environment: Windows 7 Ultimate. XAMPP 1.8.2. The XAMPP installation has Apache 2.4.10, MySQL 5.4.39, PHP 5.4.31. Dreamweaver CS6 and Mozilla Firefox,

Observation: The script runs in the browser. However, the exact space for the script turns up empty even in firefox source code. Neither a result nor an error.

Been hammering my head against this for two weeks now. And yes.... I have searched the web to no avail.

Script:

  <?php

  error_reporting(1);

  ?>
  <!doctype html>
  <html>
  <head>
  <meta charset="utf-8">
  <title>My site</title>
  </head>

  <body>
  <div id="container">
   <header>
   </header>
   <section>
  <h1>Testing, testing, 1, 2, 3.</h1>
   <?php

  $host = 'localhost';
  $user = 'dbuser';
  $password = 'userpass';
  $database_name = 'dbname';


  //Establish a connection with MySQL
  $db = mysql_connect($host, $user, $password) or
   die('<b>Sorry!<br>Unable to connect to the database .<br/>Please try later.</b>');

  //Ensure the correct database is accessed
  mysql_select_db($database_name, $db) or die(mysql_error($db));


   $query = 'SELECT * FROM db_members WHERE privilege = "A"';
  $result = mysql_query($query, $db) or die('Can\'t connect to database');

  mysql_fetch_array($result);
  if(mysql_num_rows($result) > 0)
  {
  echo $row['username'];
  }
  else{
  echo '<b>There\'s no result.</b>';
  }

 ?>
 </section>
</div><!--End of container-->
</body>
</html>
3
  • do you get any error Commented May 2, 2015 at 8:37
  • 1
    mysql_fetch_array($result); code must be $row = mysql_fetch_array($result); Commented May 2, 2015 at 8:38
  • Don't use select * . It's useless for us, and slow for you. Commented May 2, 2015 at 9:13

2 Answers 2

2

You have not collected mysql_fetch_array($result); result in $row

$row= mysql_fetch_array($result);
Sign up to request clarification or add additional context in comments.

2 Comments

This would normally sit inside a while loop (as per the 2.5 billion examples on the internet that the op failed to find)
it depends upon requirement of user,if he want to fetch only one row then while loop not required
0

You have 2 issues with your script

  1. You need to remove extra single quote after opening php tag at the first line

    <?php'
    
  2. Before using $row, you need to assign it a value:

    $row = mysql_fetch_array($result);
    if(mysql_num_rows($result) > 0)
    {
      echo $row['username'];
    } else {
      echo '<b>There\'s no result.</b>';
    }
    

    Note: The code above shall only load one row from database, if you want to load multiple rows, then you need to use WHILE loop

    while ($row = mysql_fetch_array($result))
    {
      echo $row['username'];
    } else {
      echo '<b>There\'s no result.</b>';
    }
    

1 Comment

Thanks. I removed the apostrophe behind PHP opening tag and edited the script as advised. Thanks all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.