Jump to content

SELECT tab from information.schema to php


BrainPill

Recommended Posts

 

I would like to fit this specific mysl query into php.


I use prepared statements OOP for most queries if possible.

Is there a way to get this executed:

 

// database connection 


$sql = 'SELECT Table_Name FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = `my_database` AND COLUMN_NAME LIKE `my_column` ';

// do OOP prepared statements to get the columns

I tried the mysql code in phpmyadmin and it worked properly, but in PHP no output.

Please help.

Link to comment
Share on other sites

 

PHP does not give any errors. Or can I see it in phpmyadmin?

 

This is the intended php/mysql code from the function I made.

 

    <?php 

   // make OOP mysqli database connection

    $sql = 'SELECT Table_Name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = `yourdatabase_db` AND COLUMN_NAME LIKE `user_id` ';
 
	$stmt = $conn->prepare($sql) ;
	var_dump($stmt);
	$stmt->execute();
	var_dump($stmt);
	//$stmt->bind_result($tabs);
	while ($stmt->fetch()) {	 
	var_dump($tabs); 		 
	}
   printf("Error: %s.\n\r", mysqli_stmt_error($stmt));
   printf("Error: %d.\n\r", mysqli_stmt_errno($stmt));
   printf("Error: %s.\n\r", mysqli_stmt_sqlstate($stmt));	
   ?>

 

Link to comment
Share on other sites

If you're not seeing error messages with that then there's a problem, because that query has errors.  I would suggest picking either object-oriented or procedural and stick with it, don't mix them.  mysqli_stmt_error expects a statement identifier returned from mysqli_stmt_init, not a mysqli_stmt object.  There is a different way to get error information from a mysqli_stmt object, so start with the documentation and go from there.

Link to comment
Share on other sites

6 hours ago, justsomeguy said:

mysqli_stmt_error expects a statement identifier returned from mysqli_stmt_init, not a mysqli_stmt object.

Apologies for the interjection, this isn't exactly correct, both procedural and OO Style use objects as parameters and returns. (mysqli_stmt_init returns a mysqli_stmt object)

But the advice is still valid, try not to mix them.

Edited by Funce
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...