Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a personal query window - ideas? 1

Status
Not open for further replies.

JustKIDn

MIS
May 6, 2002
386
US
Hi all,

I have an app that is simalar to an address book, but more.

Sometimes I have a need to use phpMyAdmin to do a select query to find different groups of data.
I thought it would be nice if I could create a little query window to type in my select statements.

What I have so far works great. However, I need it to be flexible, in that it needs to be able to pull data from any of the tables / fields.

i.e.
Code:
SELECT * FROM tbl_address

When I code for this I have no way of knowing what table or fields have been called for.

How can I print the results if the php code has to be generic and doesn't know what table(s) and/or fields are selected?

Any ideas?

Thanks!

____________________________
JustKIDn

____________________________
Families can be together forever...
 
Do you mean like this?
Code:
<?php

require("../database.php");
// contains method getInfo("sql") which returns the record
?>

<html>
<head>
<title>SQL Execute</title>
</head>
<body>
<form action="?" method="post">
<textarea name="query" cols="20" rows="5"></textarea><br>
<input type="submit" value="Submit">
</form>

<?php
$query = $_POST['query'];

if (isset($query))
{
  $query = stripslashes($query);
  if (stristr(" " . $query,'SELECT')) // Working with PHP 4...
  {
    echo "Query: " . $query . "<br>";
    echo "<table border=\"1\" cellpadding=\"3\">";
    echo "<tr>";
    $rs = getInfo($query);
    if (!$rs)
    {
      exit("Error in query: " . mysql_error());
    }
    for ($i=0; $i<mysql_num_fields($rs); $i++)
    {
      echo "<th>" . mysql_field_name($rs,$i) . "</th>";
    }
    echo "</tr>";
    while ($arr = mysql_fetch_array($rs, MYSQL_NUM))
    {
      echo "<tr>";
      for ($j=0; $j<count($arr); $j++)
      {
        echo "<td>" . $arr[$j] . "</td>";
      }
      echo "</tr>";
    }
    echo "</table>";
  }
  else
  {
    echo $query . "<br>";
    $rs = getInfo($query);
    echo "Result: ";
    if ($rs == TRUE)
      echo "Successful";
    else if ($rs == FALSE)
      echo "Unsuccessful: " + mysql_error();
    else
      echo "'" . $rs . "'";
  }
}
?>

</body>
</html>

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Chessbot,

This looks like just what I've been trying to get to.
Judging by it's completeness and how quick you came up with this. You must have had this snippet laying around.

require("../database.php");
// contains method getInfo("sql") which returns the record

Where do I find this getInfo() ?
Is it inside youre database.php file?

It looks to me, this won't work without your getInfo() function.



____________________________
JustKIDn

____________________________
Families can be together forever...
 
My getInfo function looks like this:
Code:
function getInfo($sql)
{
  $dbuser = "user";
  $dbpass = "pass";
  $dbname = "localhost";

  $db = mysql_connect($dbname,$dbuser,$dbpass);
  $dbase = @mysql_select_db("mydb", $db) or die("Unable to select database.");

if (!$dbase)
{
  exit("Unable to connect to database: " . $dbase);
}
$res = mysql_query($sql, $db);
mysql_close($db);
return $res;
}

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
And yes, this was created previously.

Oh, and I've been work wing with Java and javascript too long:
Code:
      echo "Unsuccessful: " + mysql_error();
should be
Code:
      echo "Unsuccessful: " [red].[/red] mysql_error();

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Oh, of course,

That's the stuff I have in my db_link.php file.

Thanks, I'll give it a try and let you know how it turns out.


____________________________
JustKIDn

____________________________
Families can be together forever...
 
Chessbot,

That worked great!

I made some modifications, like changing the name of variables etc. But it does exactly what I wanted it to.

Now I don't have to log into my server and run phpMyAdmin all the time just to run a simple query.

If I could give you more stars I would!

Thanks Chessbot.


____________________________
JustKIDn

____________________________
Families can be together forever...
 
No problem, wrote that for my site a while ago (same reason). You might want to check to make sure that you are on an admin account before loading the page (Joe Shmoe enters "DROP TABLE vital_information" and hits Submit!) but you probably already did that.

--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakespearean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top