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); Quote Link to comment 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. Quote Link to comment 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); Quote Link to comment Share on other sites More sharing options...
rubyarat2010 Posted January 1, 2014 Author Share Posted January 1, 2014 (edited) 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); Edited January 1, 2014 by rubyarat2010 Quote Link to comment Share on other sites More sharing options...
JIXO Posted January 1, 2014 Share Posted January 1, 2014 (edited) Sorry, messed up this one. Edited January 1, 2014 by JIXO Quote Link to comment Share on other sites More sharing options...
Solution JIXO Posted January 1, 2014 Solution 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); Quote Link to comment Share on other sites More sharing options...
rubyarat2010 Posted January 1, 2014 Author Share Posted January 1, 2014 (edited) Thank you so much man! Yea I was mxixing em up >.< Edited January 1, 2014 by rubyarat2010 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.