rubyarat2010 Posted January 1, 2014 Share Posted January 1, 2014 So I want to get each table name in a MYSQL database and put it in the variable database. This is what I have so far but I cant seam to get it working, thanks in advance! It just says "Access denied for user ''@'localhost' to database 'mynotesdatabase'" But when I print out the actual rows of the tables it works but not when I want to print out the table names. if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else{ echo "Connected", "<br/>"; } $query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error()); while($row = mysql_fetch_array($query)) { $database = $query; $result = mysqli_query($con,"SELECT * FROM $database"); echo "<b>+" . $database . "</b><br/>"; while($row = mysqli_fetch_array($result)) { $type = ""; if($row['Type'] == 0){ $type = "Link"; } else{ $type="Note"; } echo "  -" . $row['Name'] . " (" . $type . ")"; echo "<br>"; } } mysqli_close($con); Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/ Share on other sites More sharing options...
adam_bray Posted January 1, 2014 Share Posted January 1, 2014 if (mysqli_connect_errno()) $query = mysql_query(...) $result = mysqli_query(...) Are you using mysql or mysqli? The error is probably caused because you've used mysql when you've connected via mysqli. Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463559 Share on other sites More sharing options...
rubyarat2010 Posted January 1, 2014 Author Share Posted January 1, 2014 Hey man thank you so much! I did not see those, so I changed them all to mysql. Now I have this but all it says is "No database selected": $query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error()); while($row = mysql_fetch_array($query)) { $database = $query; $result = mysql_query("SELECT * FROM $database", $con) or die(mysql_error()); echo "<b>+" . $database . "</b><br/>"; while($row = mysql_fetch_array(mysql_query($result, $con))) { $type = ""; if($row['Type'] == 0){ $type = "Link"; } else{ $type="Note"; } echo "  -" . $row['Name'] . " (" . $type . ")"; echo "<br>"; } } mysql_close($con); Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463563 Share on other sites More sharing options...
rubyarat2010 Posted January 1, 2014 Author Share Posted January 1, 2014 Also I realized that the variable database is not database but tables. Just an FYI. Here is the full code if you want to see it, sorry to double post $con=mysql_connect("*", "*", "*", "mynotesdatabase") or die(mysql_error()); $query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error()); while($row = mysql_fetch_array($query)) { $table = $query; $result = mysql_query("SELECT * FROM $table", $con) or die(mysql_error()); echo "<b>+" . $table . "</b><br/>"; while($row = mysql_fetch_array(mysql_query($result, $con))) { $type = ""; if($row['Type'] == 0){ $type = "Link"; } else{ $type="Note"; } echo "  -" . $row['Name'] . " (" . $type . ")"; echo "<br>"; } } mysql_close($con); Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463565 Share on other sites More sharing options...
JIXO Posted January 1, 2014 Share Posted January 1, 2014 Sorry, messed up this one. Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463566 Share on other sites More sharing options...
JIXO Posted January 1, 2014 Share Posted January 1, 2014 <?php $conn = new mysqli('*', '*', '*'); $conn->select_db('mynotesdatabase'); $query = mysqli_query($conn, 'SHOW TABLES') or die(mysql_error()); while($tableShow = mysqli_fetch_row($query)) { $result = mysqli_query($conn, "SELECT * FROM {$tableShow[0]}") or die(mysql_error()); echo "<b>+" . $tableShow[0] . "</b><br/>"; while($fields = mysqli_fetch_assoc($result)) { $type = ""; if($fields['Type'] == 0) { $type = "Link"; } else { $type="Note"; } echo "  -" . $fields['Name'] . " (" . $type . ")"; echo "<br>"; } } mysql_close($conn); Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463567 Share on other sites More sharing options...
rubyarat2010 Posted January 1, 2014 Author Share Posted January 1, 2014 Thank you so much man! Yea I was mxixing em up >.< Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463568 Share on other sites More sharing options...
JIXO Posted January 1, 2014 Share Posted January 1, 2014 Sorry, the above was messed up, try this : <?php $conn = new mysqli('*', '*', '*'); $conn->select_db('mynotesdatabase'); $query = mysqli_query($conn, 'SHOW TABLES') or die(mysql_error()); while($tableShow = mysqli_fetch_row($query)) { $result = mysqli_query($conn, "SELECT * FROM {$tableShow[0]}") or die(mysql_error()); echo "<b>+" . $tableShow[0] . "</b><br/>"; while($fields = mysqli_fetch_assoc($result)) { $type = ""; if($fields['Type'] == 0) { $type = "Link"; } else { $type="Note"; } echo "  -" . $fields['Name'] . " (" . $type . ")"; echo "<br>"; } } mysql_close($conn); If you get any error, paste it here. Link to comment https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/#findComment-1463569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.