Jump to content

Return the name of each column in MySQL database


malikah

Recommended Posts

Write the queries in standard mysql statements and I will help you convert to prepared statements.

 

This worked for me procedurally:

$query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS where table_name='table'";
if(mysqli_real_query($dblink, $query))
{
$result = mysqli_store_result($dblink);
while($row = mysqli_fetch_assoc($result))
{
	foreach($row as $_colName => $_colValue)
	{
		echo "{$_colValue}<br />";
	}
}
}

 

Unfortunately, I can't figure out the object-oriented equivalent. Here's my attempt:

 

$query = "SELECT COLUMN_NAME FROM information_schema.COLUMNS where table_name='table'";
if($stmt = $this->conn->prepare($query))
{
$stmt->execute();
$stmt->store_result();

while($row = $stmt->fetch())
{
	foreach($row as $_colName => $_colValue)
	{
		echo "{$_colValue}<br />";
	}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.