MySQL SHOW TABLES from DATABASE except one - mysql

So ive got a fully working page to display a dropdown list of all tables avaliable in my database. however i want it to display the list of all tables except one (as one of the tables contains user information which i dont want shown...)
Is there a way to do this?
This is the section of the code ive got so far...
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
if (mysql_select_db($dbname, $conn))
{
?>
<form method="post" action="Browse.php">
<select name="tables">
<?php
while ($row = mysql_fetch_row($result)) {
?>
<?php
echo '<option value="'.$row[0].'">'.$row[0].'</option>';
}
?>
</select>
<input type="submit" value="Show">
</form>
<?php
//mysql_free_result($result);
if (isset($_POST) && isset($_POST['tables']))
{
$tbl=$_POST['tables'];
//echo $_POST['tables']."<br />";
$query="SELECT * from $tbl ORDER BY title ASC";
$res=mysql_query($query);
//echo $query;
if ($res)
{
?>
<table border="1">
<?php
while ( $row = mysql_fetch_array($res))
{
echo "<tr>";
//echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
//echo "<td>".$row[2]."</td>";
//echo "<td>".$row[3]."</td>";
echo "</tr>";
} ?>
</table>
<?php
}
}
}
Its no biggie if there isnt an easy way to do so as i've figured out that by doing the $query="SELECT * from $tbl ORDER BY title ASC"; it wont display the data (as there isnt a title column in the user details table)..but i dont want the table name to be shown in the dropdown box
Just a general query really...
Thanks

You can use a where clause with the SHOW TABLES statement.
The trick is knowing the name of the column that's generated by the SHOW TABLES statement as its name depends on your database name. The name of the column will be "tables_in_{your_dbname}". So if your database name is "blah" the column name will be "tables_in_blah".
So if the table you want to omit from your result set is called "secret_table" you can execute the statement like so:
SHOW TABLES WHERE tables_in_blah <> 'secret_table';
Or if you want to use a wildcard you can do it like so:
SHOW TABLES WHERE tables_in_blah NOT LIKE '%secret_table%';

Try this:
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_data_base_name'
AND table_name NOT LIKE '%USER_TABLE%';
Without like
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'your_data_base_name' AND table_name <> 'USER_TABLE';

Another alternative is to avoid outputting the table in question when you construct the list of options:
<?php
while ($row = mysql_fetch_row($result)) {
if ($row[0] != 'my_private_table_name') {
echo '<option value="'.$row[0].'">'.$row[0].'</option>';
}
}
?>

Related

Modify field from drop down list Mysql

I have this code to have a list of my field in a database.
I have to select one of this element and modify it.
<?php
mysql_connect("*", "*", "*") or die("Connection Failed");
mysql_select_db("Sql860043_2")or die("Connection Failed");
$query = "SELECT * FROM users";
$result = mysql_query($query);
?>
<select name="select1">
<?php while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<option value="Test">
<?php echo $line['firstname'];?>
</option>
<?php } ?>
</select>
This is the result:
http://www.mobilesystemsas.it/ale/test1/test1.php
For example i want to select "Alessandro" and modify it to "Giuseppe".
How i can do it?
Wrap the select tag in a form tag, and set its action attribute to the current php script.
Before performing the query to select all users, check the $_POST superglobal for the name of your select tag. If it is set, perform a MySQL UPDATE statement with that value on the given user.
Here is an example from w3schools:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: You may want to also include a hidden tag within the form to store the User's ID, which will be necessary in your UPDATE statement.

Delete an entire row from a table, using MySQL

I am trying to code for a row to be deleted in MySQL.
I am not sure how to do this so have picked some coding up from another website:
Syntax:
<?php
$personID=$_GET["q"];
if ($personID<="0"){echo( "<center><h3>NO ID ERROR CLICK THIS TO RETRY CLOSE WINDOW</h3></center>");
}
else
{
$con = mysql_connect("mysql.XXX.com","XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$myid=$_COOKIE["user"];
#mysql_select_db("u716720408_admin", $con)or die( "Unable to select database");
$result=mysql_query("DELETE * FROM users WHERE personID=$personID")or die(mysql_error());
$cnt=1;
$row = mysql_fetch_array( $result );
$f1=$row['personID'];
$f2=$row['personFname'];
$f9=$row['llmail'];
$f14=$row['personSname'];
$f19=$row['password'];
?>
Use:
DELETE FROM users WHERE personID=$personID
Make sure you read up about SQL Injection
You don't need the * in the delete query
try
DELETE FROM users WHERE personID=$personID
In your result you dont need to metion * in delete query
$result = mysql_query("DELETE FROM users WHERE personID=$personID") or
die(mysql_error());

Add image column into an existing mysql table?

I want to add a new column called image in an existing mysql table.
The type is BLOB, the attributes are BINARY and NO NULL
What code should I use in PhpMyAdmin?
In PhpMyAdmin you can use the table editor to add a new column from clicking on the structure of a table.
The mysql command instead, would be:
ALTER TABLE table_name ADD image MEDIUMBLOB NOT NULL
MEDIUMBLOB has a maximum size of 16MB, use LONGBLOB (up to 4GB) for anything bigger
If you have problems uploading BLOBs, check the value of max_allowed_packet http://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html
It is not recommended to use BLOBs for saving images.
Save the files somewhere on your webserver for example .../htdocs/images/picture.jpg (assuming you use xampp).
then create a simple Column in you Database for Strings of the image names. To show you why i will use PHP.
in your .../htdocs/index.php
you can then do something like the following:
<?php
//enter you PhpMyAdmin details here.
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT imageName
FROM MyTable
;
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
?>
<img src="images/<?=$row["userid"]?> <!-- this will get repeted for all the rows in you database -->
<?php
}
mysql_free_result($result);
?>
this is where i got the sample code from:
PHP Doc
i just modified it a bit .. but the php documentation is great btw.

Select from table where table = id if session is lang_code

I'm trying to select the english menu title with if session and my current sql table "see code"
if($_SESSION['lang_code'] == 'en') {
echo $courseCat['course_cat_title'];
} else
echo $courseCat['course_cat_title'];
it prints so far good but only the dutch menu title, when I look in my DB I have this table that shows the language country id course_cat_lang_country, now I need to know how I can print the English title when page is www.yourdomain.nl/en/. the full code that I use now is this.
$QcourseCat = mysql_query("SELECT * FROM web_course_cat WHERE course_cat_id = 1");
$courseCat = mysql_fetch_assoc($QcourseCat);
echo "< h4>";
if($_SESSION['lang_code'] == 'en') {
echo $courseCat['course_cat_title'];
} else
echo $courseCat['course_cat_title'];
echo "< h4>";
How about this:
if($_SESSION['lang_code'] == 'en') {
$table='course_cat_eng';
}
else {
$table='course_cat_title';
}
$sql = "SELECT * FROM $table WHERE course_cat_id = 1";
if (! $resource = mysql_query($sql) ){
echo "Error reading from table $table";
}
else {
if (!mysql_num_rows($resource ) ){
echo "No records foin in $table";
}
else {
$courseCat = mysql_fetch_assoc($resource);
echo "<h4>";
echo $courseCat['course_cat_title'];
echo "</h4>";
//...
// other stuff you might want to do
}
}
Please comment if this is not what you are looking for, and I will revise my answer.
You aren't sorting your results to check the language country id. You are getting only the first row. You need to change the query to get the language that you want. Or loop through the results that you get checking which language they are.
In the code that you posted your if statement does the same thing in each branch. And you never use it to select data.
Also as per the comments use MYSQLi or PDO for your querys as MYSQL is deprecated.

use a single return from a sql query

I'm using PHP to make a very specific sql query. For example sake, I have the user's ID number, but I need their name. So I do a sql query from that table with the ID number in order to return the name.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
Now I want to use that. What's the most succinct way to go about making that result into a variable ths I can use?
edit:
I'm hoping that this is not the answer:
$rowCheck = mysql_num_rows($result);
if ($rowCheck > '0') {
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $val){
$username = $val;
}
}
}
I have used something like this to keep it short in the past:
list($name) = mysql_fetch_row(mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db));
echo $name;
In my opinion, the best way to fetch any SQL result is through mysql_fetch_assoc(). To use it, you would do something like this:
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']; // You get an array with each column returned from your query.
}
Still, MySQL extension has been replaced for MySQLi, which is acknowledged to be faster and more practical. It has both OOP and structural bindings, and takes more into account your server settings.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$name = mysql_fetch_row($result)[0];
You should use MySQLi as bellow:
$db = new MySQLi($host,$user,$pass,$db);
$query = $db->query('SELECT name FROM users WHERE userID='.$thisuserid);
$result = $query->fetch_object();
echo $result->name;
If you use SELECT * so you also can access via $result->{field_name}