Jump to content

retrieving table names from database


WesleyA

Recommended Posts

I made a set up for showing tables with a php script.

 

Its based on a script from w3schools.

     // make connection with localhost      $conn = new mysqli($servername, $username, $password, $dbname);     // Check connection     if ($conn->connect_error) {         die("Connection failed: " . $conn->connect_error);                               }       // script calling to connect with dbase      function write_content () {		      // reading the tables from the dbase       $sql = "SHOW TABLES"; //  show tables       $result = $conn->query($sql);       while ($row = $result->fetch_assoc()) {       echo current($row) . '<br/>';       }		$conn->close();	return;       }		echo "<br><br><br>";	echo "The following tables are found in the database  " ;	echo write_content ();

The problem is that conn is a not defined variable.though it worked in a condition with if.

Then a second error message is given sounding:

Fatal error: Call to a member function query() on a non-object in .....

 

these errors concern this line:

     $result = $conn->query($sql);
Link to comment
Share on other sites

When writing scripts I do not only face the problem of how a condition works but that is the lesser problem because you can find tons of scripts and examples online.

 

The real issue is WHEN. WHEN do I have to use a condition like isset/empty.

 

Which type of variables are generally demanding a condition.

 

I know programmers distinguish primitive and complex variables. But there might be more categories of variables.

 

Anyway when creating a script the issue is more to know how to discover what has to happen.

 

Some variables are just shown by the browser you can say:

     $var = 'hello world ';     echo $var;

But when the variable $var becomes variable or retrieves data from a database or array then it gets more complex.

 

Is it so that these variable variables require a condition? And what is the exact name of this type of variables?

 

And what is the reason that PHP (or a programming language in general) has determined that conditions should be set in these matters?

Link to comment
Share on other sites

 

The real issue is WHEN. WHEN do I have to use a condition like isset/empty.

 

When a $_POST or $_GET are not created yet! OR when a coding requires this value before it can proceed, then you would either prevent this coding running, or give a default value to work with until such a time when a value is retrieved from $_POST or $_GET where it would use use that value instead.

Link to comment
Share on other sites

But when the variable $var becomes variable or retrieves data from a database or array then it gets more complex.Is it so that these variable variables require a condition? And what is the exact name of this type of variables?

Variables that are things like a connection to a database or an open file are generally just called resources in PHP. If you try to print one, for example, it will print something like "Resource 1". Other than that, PHP has plenty of classes and objects. A class is like a blueprint and an object is one specific instance of the class (like a specific house built from a blueprint).

Is it so that these variable variables require a condition?

Variables don't require anything, it's up to you as the programmer to know when you need to use the various control structures to make your program do what you intend.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...