Thursday, July 30, 2009

How do I simply show records in a browser from MySQL using PHP?

I'm a tech, not a programmer! Discovering that I'm a pretty useless programmer actually! However, I'm toying with PHP/MySQL and can't understand why I cant get a simple query to work! I'm trying to display data from a form I made in Joomla using FacileForms.





I put:





%26lt;?php





$username="root";


$password="";


$database="testdb";





$name=$_POST['Value1'];


$value=$_POST['Value2'];





mysql_connect(localhost,$username,$pas...


@mysql_select_db($database) or die( "Unable to select database");





SELECT * FROM `jos_facileforms_subrecords`;








mysql_close();


?%26gt;





I keep getting the following error:





Parse error: syntax error, unexpected '`' in C:\wamp\www\testdb\test1.php on line 13





Can some please spell it out for me! I saved the above script in test1.php and pull up the file in my browser. Just how wrong am I?!





Please enlighten me!





Thanks

How do I simply show records in a browser from MySQL using PHP?
you can not just type








SELECT * FROM `jos_facileforms_subrecords`;





you have to assign that to a variable





ie $sql="SELECT * FROM `jos_facileforms_subrecords`";





$result = mysql_query($sql) or die ("mysql_query ".mysql_error().$sql);





$row =mysql_fetch_array($result);








or if there is more than one row








while($row =mysql_fetch_array($result)) {





...





}
Reply:%26lt;?php





$username="root";


$password="";


$database="testdb";





$name=$_POST['Value1'];


$value=$_POST['Value2'];





$connection = mysql_connect(localhost, $username, $password) or die("Unable to connect");


@mysql_select_db($database, $connection) or die( "Unable to select database");





$query = "SELECT * FROM `jos_facileforms_subrecords`";


$request = mysql_query($query) or die("Invalid query.");


$count = mysql_num_rows($request);





for ($i = 0; $i %26lt; $count; $i++)


{


$result1 = mysql_result($request, $i, "Number");


$result2 = mysql_result($request, $i, "Name");


/* and etc.. I just wrote simple example, you should change "Name", "Number" to you requested names. contact me or add some additional info, what exactly results you are going to take*/


echo $result1; echo "%26lt;/br%26gt;";


echo $result2; echo "%26lt;/br%26gt;";


}





mysql_close($connection);


?%26gt;





Tell me if you were lucky to get it! :) for further questions, contact me :) Good luck!





P.S. this error shows that ` is used incorrectly, just because the query must be in "...".
Reply:error, unexpected in ….. scripts.php on line …





The error may caused by a missing curly bracket in PHP script coding. Beside, it may also caused by error in PHP coding in class definition, as in PHP, a class definition cannot be broke up and distributed into multiple files, or into multiple PHP blocks, unless the break is within a method declaration.





But more commonly, the error is often caused by the use of Short Open tags in PHP,





To use short open tags, it must be enabled in PHP.INI. Search for short_open_tag in PHP.INI, and change the value to On. The line should look line:





short_open_tag = On


No comments:

Post a Comment