Can I test php script (connecting to MySQL) from browser?

Jimbo

Member
Licensed User
Longtime User
I am new to PHP so apologies if this seems like a daft question. Here goes . . .

I am following Erel's MySQL tutorial with my own DB hosted by 1&1. Using the script in the tutorial, is it possible to pass an SQL query via a browser?

For example, with Erel's MS SQL tutorial, you can call the .aspx script page followed by '?query=SELECT * FROM table' from a browser; and the query then gets passed to the DB and the result subsequently shown.

I am fully aware of security issues - but at this moment in time I am just trying to get the basics to work.

Note that if I 'hard-code' the SQL query within the php script and simply call the php page from my browser then I get the expected result (hence this isn't a connectivity issue but more like a syntax issue)

Hoping someone can help me! . . . .
 

Jimbo

Member
Licensed User
Longtime User
Ok, thanks for the VERY quick reply.

Assuming it is possible to write a php script to send any query (and ideally any type of SQL command such as insert, drop, etc) can you please, please point me to tutorial/ example. I have looked for the last few evenings but without joy!!

Whilst you're online, may I say a massive Thank-You for an excellent product. I have spent over 25 years programming different languages on various hardware platforms and I can say that this is the best development tool I have ever used.
 
Upvote 0

Jimbo

Member
Licensed User
Longtime User
Thank you :)

You can just replace:
$query = file_get_contents("php://input");
With:
$query = $_GET["query"]

:BangHead:
When I do the above I get the following error message:
Fatal error: Function name must be a string in . . . (path on server)

Please can you confirm the exact syntax of the URL.
If you need to see the message then my live/test URL is:

www.pbsrsolutions.com/B4A06.php . . . . .

Thanks once again for your kind help!
 
Upvote 0

Jimbo

Member
Licensed User
Longtime User
Can you post the script?


As requested: (I have removed the DB connection details for obvious reasons!)

<?

$databasehost = "xxxx";
$databasename = "xxxx";
$databaseusername ="xxxx";
$databasepassword = "xxxx";

$con = mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
mysql_select_db($databasename) or die(mysql_error());
$query = $_GET("query");
$sth = mysql_query($query);

if (mysql_errno()) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysql_error();
}
else
{
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
}
?>
 
Upvote 0

Jimbo

Member
Licensed User
Longtime User
Thanks Erel - the square brackets corrected everything and allowed me to pass a query within the URL such as:

Basic4android (Basic for Android) - Rapid Application Development. . . . .com/B4A08.php?query=select * from People


However, if I add a 'WHERE' condition statement to a string field it gives an error (The website cannot display the page). :sign0085: Is this to be expected?? I am using the correct SQL syntax (also works fine in your MS SQL tutorial) so is this a PHP syntax issue? Or is it just not possible to achieve?

Just to make you aware, I noticed that an int field works fine; i.e. WHERE RecordID=x displays the correct result.

Thanks for anyone tha can shed any light on where I am going wrong.
 
Upvote 0
Top