0

I have reproduced this function:

function getTables()
 {
 global $db;
 $value = array();
 if (!($result = $db->query('SHOW TABLES'))) {
 return false;
 }
 while ($row = $db->fetchrow($result)) {
 if (empty($this->tables) or in_array($row[0], $this->tables)) {
 $value[] = $row[0];
 }
 }
 if (!sizeof($value)) {
 $db->error("No tables found in database");
 return false;
 }
 return $value;
 }

in this manner:

public function getTables() {
 $value = array();
 $tables = array();
 $sql = "SHOW TABLES";
 if($stmt = $this->connect->prepare($sql)) {
 $stmt->execute(); 
 while( $row = $stmt->fetch_row() ) { 
 if(empty($tables) or in_array($row[0], $tables)) {
 $value[0] = $row[0];
 } 
 }
 $stmt->close();
 if(!sizeof($value)) {
 echo 'The database has no tables';
 }
 return $value;
 } else {
 echo 'Couldn\t query the database';
 }
}

But the second method returns me The database has no tables which is not true because I have one table in the db.

What is it wrong with the second method ?

In case you wonder what connect does :

public $connect;
public function __construct() {
 // Define The Database Connection Or Die If Failed Connecting
 $this->connect = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die(DB_CONNECTION_ERROR_MESSAGE);
}

It make a connection with the database. And prepare() it's a mysqli statement. I tried with query() too, same result.

asked Feb 1, 2012 at 8:57
4
  • 2
    Do some debugging. What does your query return? Are there any rows in the result set? Commented Feb 1, 2012 at 8:58
  • What are connect() and prepare() doing? Commented Feb 1, 2012 at 9:00
  • Let me check what the query returns. But the result is just Array. Commented Feb 1, 2012 at 9:01
  • I see in your edit that now connect is included. Try to check for errors. I delete my previuous answer. Commented Feb 1, 2012 at 9:18

2 Answers 2

1

Correct code. Use query instead of prepare:

public function getTables()
{
 $value = array();
 $tables = array();
 $sql = "SHOW TABLES";
 if ($res = $this->connect->query($sql))
 {
 while ($row = $res->fetch_row())
 {
 if (empty($tables) or in_array($row[0], $tables))
 {
 $value[] = $row[0];
 }
 }
 if (!sizeof($value))
 {
 echo 'The database has no tables';
 }
 return $value;
 }
 else
 {
 echo 'Could not query the database';
 }
}

If you still want to use prepare then you will also need $stmt->bind_result and $stmt->fetch() instead of fetch_row.

answered Feb 1, 2012 at 9:10
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but what would be the result I'm binding ?
This is what i'm talking. Use query and don't worry about that. For such queries query is ideal and more convinient.
It works now, I get Array ( [0] => users ). This is what I needed, because now I will iterate through each table and get all the row names and values and output it in a sql file as you would have exported a database table from phpMyAddmin.
0

I think this piece of code is broken $value[] = $row[0]; and probably you should change it to $value[0] = $row[0]; or array_push($value, $row[0])

Nalaka526
11.5k21 gold badges86 silver badges119 bronze badges
answered Feb 1, 2012 at 9:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.