Read from a MYSQL database using PHP

From The IT Community
Jump to: navigation, search




This little page will take the name (unique key) of a player as the URL parameter and read every available information from the MYSQL database.

I use this for an online game where I want to get the coordinates of one player.


First here is the script, you can copy it, modify the MySQL variables and save it as a .php file (like read.php):

<?php

$mysqlhost="localhost"; // MySQL-Host

$mysqluser="mysqluser"; // MySQL-User

$mysqlpwd="topsecret"; // Password

$mysqldb="mysqldatabase"; // Name of Database

 

$name=$_GET['name'];

$db = @new mysqli($mysqlhost,$mysqluser,$mysqlpwd,$mysqldb);

	

$query = "select * from players where name='" . $name . "'";
$result = $db->query( $query );

     //loop to show each records
     while( $row = $result->fetch_assoc() ){
             //extract row
             //this will make $row['name'] to
             //just $name only
            extract($row);
             
            //creating new table row per record
                echo "{$x} ";
                echo "{$y} ";
		 echo "{$name} ";
	 }
$db->close();	
?>

To read the information of a name from the database call the file like this: http://yoururl.com/read.php?name=player1

It will also output the coordinates and the name, all separated by a blank.



Was this article helpful? Then please donate to keep The IT Community alive...

If you found this article helpful please share it, comment and help others by writing your own article.






Translate this page:




Articles found in the same category:
(max. 20 shown)