Jump to content

outputting table names from database


Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello again!

 

I'm here with another PHP/Mysqli related question, I want to output the data i receive from the Query:

SHOW tables; 

Into html code, more specific: links.

This is what i try'd to do:

<div class="container">
  <div class="header" align="center">
   <nav>
		<ul>
			<li><a href="#">Home</a></li>
			<li><a href="#">Databases</a>
				<ul>
<?php
$mysqli = new mysqli('localhost','*****','*****','*****');
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 
$sql = 'show tables from osiris1q_mtg';
$result = $mysqli->query($sql);
if(!$result = $mysqli->query($sql)){
    die('There was an error running the query [' . $mysqli->error . ']');
}
    // output data of each row
    while($row = $result->fetch_assoc()){
    echo "<li><a href='#'></a>"$row"</li>";
}
} else {
    echo "<li><a href='#'></a>0 results</li>";
}				</ul>
			</li>
		</ul>
	</nav>
</div>    <!-- end .header -->
?>

This is the header.php file which is included in the actual index page.

This doesn't return anything not even the html parts of it.

Even all the files i have 'included' aren't showing up anymore.

I hope i explained myself good enough for anyone to understand

 

How Anyone can help me out!

 

Thank you in Advance!

Link to comment
https://forums.phpfreaks.com/topic/292679-outputting-table-names-from-database/
Share on other sites

your code contains a fatal php parse/syntax error, because you are missing a closing ?> tag.

 

you need to have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini so that php will report and display all the errors it detects. you will save a ton of time. for parse errors in your main file, you cannot put these settings into your code, because your code never runs to make use of the settings.

your code contains a fatal php parse/syntax error, because you are missing a closing ?> tag.

 

you need to have php's error_reporting set to E_ALL and display_errors set to ON in your php.ini so that php will report and display all the errors it detects. you will save a ton of time. for parse errors in your main file, you cannot put these settings into your code, because your code never runs to make use of the settings.

 

I dont see where I'm missing the closing tag i'm sorry,

The php.ini file i've uploaded and this is the error:

Parse error: syntax error, unexpected '$row' (T_VARIABLE), expecting ',' or ';' in /home/osiris1q/public_html/includes/header.php on line 20

This is the line but i dont see whats wrong:

echo "<li><a href='#'></a>"$row"</li>";
  • Solution

 

 

This is the line but i dont see whats wrong:

It is not a valid string! You have ended the string, followed that by $row and started a new string. That is not the correct string syntax. The correct way would be to use the concatenation operator ( . a period ) between the string and the variable.

 

If you are to display the table name. Then the while loop needs to be

while($row = $result->fetch_row())
{
    echo "<li><a href='#'></a>".$row[0]. "</li>";
}

Next problem is the closing php tag ?> is in the wrong place. It should be after this else statement

else {
    echo "<li><a href='#'></a>0 results</li>";
}

// closing tag should be after the above lines
?>

You cannot place raw HTML between php tags. 

Edited by Ch0cu3r
  • Like 1

There are several problems with that code.

 

Line 22: There is an else statement that is NOT associated with any if() statement. The two previous if() statements were closed right after they were started. So, that else statement is out of context.

 

Line 24: There is a closing bracket for the else condition, directly followed by raw HTML code.

 

Line 29: There is a closing PHP tag, but the code that it follows is HTML
 

<div class="container">
  <div class="header" align="center">
   <nav>
		<ul>
			<li><a href="#">Home</a></li>
			<li><a href="#">Databases</a>
				<ul>
<?php
$mysqli = new mysqli('localhost','*****','*****','*****');
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
} 
$sql = 'show tables from osiris1q_mtg';
$result = $mysqli->query($sql);
if(!$result = $mysqli->query($sql)){
    die('There was an error running the query [' . $mysqli->error . ']');
}
    // output data of each row
    while($row = $result->fetch_assoc()){
    echo "<li><a href='#'></a>"$row"</li>";
}
} else {
    echo "<li><a href='#'></a>0 results</li>";
}				</ul>
			</li>
		</ul>
	</nav>
</div>    <!-- end .header -->
?>
  • Like 1
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.