Jump to content
Sign in to follow this  
BrainPill

SELECT tab from information.schema to php

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.

Share this post


Link to post
Share on other sites

You need to check for errors in PHP to see what the issue is.  Maybe your database user doesn't have access to information_schema.

Share this post


Link to post
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));	
   ?>

 

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...