Forum Moderators: coopster

Message Too Old, No Replies

Retrieving data from MySQL with PHP

         

Butterfly Wing

11:37 am on Nov 3, 2004 (gmt 0)

10+ Year Member



Folks ,

any one there could provied me a good script ...

i have table made and the data are there inide it..so all i wanted is to retrive that data from mysql and show it on the page dynamically,.. dynamic table.,.

i tried so many search engines , cou7ldnt find one ..

plz help..thank u

coopster

12:33 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You bet, our own PHP Forum Library [webmasterworld.com] has a message that describes step-by-step the Basics of extracting data from MySQL [webmasterworld.com].

Butterfly Wing

2:51 pm on Nov 3, 2004 (gmt 0)

10+ Year Member



Hellow coopster,

Yeah i went to the link that u gave ...its really puzzling for me..

can u make one very simple one... all i need is to extract a ddata from mysql and display it in browser .. dynamically ..

Assum my table looks like this ¦ID¦Image¦Description¦

i wanted that Image and Description to be displayed it on Browser , i dont need any forms .. as am trying to learn just to retrive it.

So do u mind teaching me plaese

will count on u

thanks a lot

jatar_k

6:06 pm on Nov 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at msg 5 in that thread

for step3 that exact query won't work for you but you could use something as simple as

$sql = "select * from ourtablename";

that will grab all of the columns from each row

then for step5 you will obviously need to change the echo line to fit your data

this wont work for you
echo "<p>",$row['id'],": ",$row['manufacturer'];

maybe something like this to start

echo "<p>",$row['column1'];
echo "<br>",$row['column2'];
echo "<br>",$row['column3'];

all the tablenames and column names will have to be changed to the ones you use but everything else is in that tutorial

mincklerstraat

8:25 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I was first learning PHP I found that working with SQL queries required an extra degree of mental abstraction and at that point my poor brain just didn't want to cooperate since it already had so much on its plate.

I admire your go-for-it-ness in wanting to start out using SQL, but if the tutorial link above didn't do it for you (it's a pretty straightforward tutorial, and very well-written), and googling neither, probably it's time to wait a bit with SQL. There's plenty you can learn and accomplish hacking PHP code by adding stuff you need / commenting out stuff without touching the SQL part, and you'll find that once you're more comfortable with PHP in general and things like arrays, the SQL then will come really easy.

dreamcatcher

9:46 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, like mincklerstraat mentioned, maybe you are trying to jump in at the deep end and its not for you. Might be a good idea to pick yourself up some books from Amazons marketplace or Ebay so you can read and digest the information you see. Janet Valade`s PHP & MySQL for Dummies is a really good book if you are starting out.

Another good way of learning is to download a couple of free scripts from Hotscripts.com that use SQL/PHP and see how they are coded. Looking at other peoples code can be a great learning curve. On the other hand, sometimes you can look at things until you are blue in the face and not understand it. Another time it all falls into place. You just have to keep going over it. I think its part of the fun in learning.

Hope you find something that suits you.

:)

Butterfly Wing

10:42 am on Nov 4, 2004 (gmt 0)

10+ Year Member



Hellow Friends ,

This is the code am trying : have a look am getting error, if u all can fixed it up for me :

am getting error on this line : Parse error: parse error in c:\web\test.php on line 12

***************************************************
<html><body>

<?
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "karmas";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname)

$sql = "SELECT ID,logo,Description FROM client";
$result = mysql_query ($sql);



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

$ID = $row[0];
$logo = $row[1];
$Description = $row[2];

$show_stuff .= "<img src=\"$image_path\"><br>$image_description";

// ^^^ this will store all the values of table inside $show_stuff ^^^
}

echo $show_stuff;
?>
</body></html>

************************************************

My Table structure (karmas)

¦ID¦logo¦Description¦

*****************************************************
Parse error: parse error in c:\web\test.php on line 12

($sql = "SELECT ID,logo,Description FROM client"; )

coopster

10:50 am on Nov 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Something in here doesn't look quite right...try this:
mysql_select_db($dbname);
You are missing the terminating semicolon ;)

Code Sentinel

11:24 am on Nov 4, 2004 (gmt 0)

10+ Year Member



yes the evil semi-colon.. my favorite error..

this will get ya many times during your learning of php.. and even when you know it well.. always check the lines above and below the specified error line.

sometimes a bit tricky when large blocks missing an opening or closing curly brace is part of the error.

Butterfly Wing

12:01 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Thanks Folks,

It finally worked , i like it ,...... something not yet arranged ..

cant the data be in dynamic table , so that what ever i retrive it from the datbase it directly show it in table in the browser ..

Let me know more

thnaks

Zipper

12:46 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



I'm not sure what "$show_stuff" does here, so if u can explain it a bit more it'll make things much clear.. anyway, in the simplest form, u can do something like this,


echo "<table>";
echo "<th>ID</th><th>Logo</th><th>Description</th>";

while ($row = mysql_fetch_row($result)){
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>$row[2]</td>";
}

echo "</table>";

mincklerstraat

1:19 pm on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you'll probably also want to replace this line:
$show_stuff .= "<img src=\"$image_path\"><br>$image_description";

with this:
$show_stuff .= "<img src=\"image_path/".$logo."\"><br>ID: ".$ID."<br>Description: ".$Description."<br>"; 

And this will get you further down the road. Shall I tantalize you a little bit?

You can also use this script, but you'll have to modify it a little bit to make it do what you want (yeah, go figure out how! It'll be worth it!):


$host = "localhost";
$user = "root";
$pass = "";
$dbname = "karmas";
$table = "client";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql = "SELECT * FROM ".$table;
$result = mysql_query ($sql);
$resultrows = array();
while($row = mysql_fetch_assoc($result)){
$resultrows[] = $row;
}
echo '<table>'."\n";
foreach($resultrows as $k => $v){
echo "\n".'<!-- this is the beginning of the foreach $resultrows loop, loop number '.$k.'-->';
echo "\n".'<tr><td>'.$k.'</td>';
foreach($v as $k1 => $v1){
echo "\n".'<!-- here we are doing a loop of the foreach $v loop (inside the foreach $resultrows loop -->';
echo "\n".'<td><b>'.$v1.'</b>: '.htmlspecialchars($k1).'</td>';
}
echo '</tr>';
}
echo "\n".'</table>';

If you're not really quick with arrays, it'll be hard to figure out how to make this little table do what you want. But not impossible. Just change the php and what's 'echoed', after
echo '<table>'."\n";
-- don't change the query. 'View source' for a bit more help / clues.

Butterfly Wing

3:21 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Thanks folks ,

let me try with ur codings, tons of thanks for ur effort..

Butterfly Wing

4:00 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Hellow Minc,

Really thnks for help , and i just wonder how do i give my image source , its puzzling for me i dont no how .... can u add me one thing in that code ..

my data structure is like this .. ID¦logo¦Description so all i need to retrive is Logo and Description ,

can u do that for me plz , and also a image src to that logo.

thanks will count to ya all

mincklerstraat

4:06 pm on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in your first script, just set
$image_path
to the directory where your images are (relative to webroot).

like:

$image_path = '/logodirectory'; /* no trailing slash */

If this doesn't work for you, try viewing source and see what you get, modify as necessary.

Butterfly Wing

4:46 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Minc,

If i had just known how to add it , i did it already,

plz dont mind can u add it along the full code that u gave ....am so dull, and new to this stuff

thanks again

Zipper

5:16 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Seems like this has got way too complex.. I got no idea why you have to use two loops.. Not that I have interpreted this entire thing correctly, but from what I understand it's just a simple query & display thing..

To make things much simpler (and clearer) could u answers these qusetions first..

a) How & why have used $image_path, do u know what it does?
b) What goes in the Logo field in the table? Is it the file name of the logo? Could you give an example.
c) where are the actual logos (images} located in your server.

mincklerstraat

5:44 pm on Nov 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oops, in the first post I 'hard coded' image_path, in the second I advised setting the variable ...

OK, here's some code again


<html><body>
<!-- -->
<?
$host = "localhost";
$user = "root";
$pass = "";
$dbname = "karmas";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
// ignore the double slashes (there as spaceholders)
$sql = "SELECT ID,logo,Description FROM client";
$result = mysql_query ($sql);
$image_path = '/imagedirectory';
//
//
//
while ($row = mysql_fetch_row($result))
{
//
$ID = $row[0];
$logo = $row[1];
$Description = $row[2];
//
$show_stuff .= "<img src=\"".$image_path."/".$logo."\"><br>ID: ".$ID."<br>Description: ".$Description."<br>";
//
// ^^^ this will store all the values of table inside $show_stuff ^^^
// ^^^ above comment probably from some earlier code, not for this code ^^^
}
//
echo $show_stuff;
?>
</body></html>

On the line

$image_path = '/imagedirectory'
, replace "
/imagedirectory
' with the images' location (directory they're in).

Zipper: the second snippet is primarily a learning thing - it just shows all the field names and field contents of any table you set as

$table
. One always holds out that little bitty hope that we're also teaching to code, not just writing free scripts ;)
You're right though, the best way for this purpose wouldn't be to do two nested loops.

Butterfly Wing

7:04 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



Folks i think what do u say abt my code i made : its really user friendly check it out , ur code is not bad too just handling tables is bit difficulty i faced it ..chill out i found the answer

******************************************************
<?php

$hostname_conn = "localhost";
$database_conn = "karmas";
$username_conn = "root";
$password_conn = "";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>

<?php
$maxRows_Recordset1 = 6;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
$pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_conn, $conn);
$query_Recordset1 = "SELECT * FROM client";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $conn) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
$totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
$all_Recordset1 = mysql_query($query_Recordset1);
$totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>

<table border="1" cellspacing="0" bordercolor="#000000">
<tr bgcolor="#999999">
<td><strong>pic_url</strong></td>
<td><strong>pic_description</strong></td>
</tr>
<?php do {?>
<tr>
<td><?php echo "<img src=";?>
<?php echo $row_Recordset1['logo'];?><?php echo ">";?></td>
<td><?php echo $row_Recordset1['Description'];?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Zipper

8:04 pm on Nov 4, 2004 (gmt 0)

10+ Year Member



From the little I understood of your problem, what u have done now is as many times harder than the normal approach.. You really should consider revising it unless you're satisfied with the result..

also, you don't have to open/close php tags for each line.. for an example, this one
<?php echo $row_Recordset1['logo'];?><?php echo ">";?>
can be easily coded as,
<?php echo $row_Recordset1['logo'].">";?>

hope it goes well for you.

Butterfly Wing

10:25 am on Nov 5, 2004 (gmt 0)

10+ Year Member



The code i coded automatically i find it userfriendly as i understood clearly and am able to change the dynamic table the way i wanted it...

The code u all gave me was not bad too ... just that i couldnt get what i wanted it . like say in dynamic table .. now i got it..Hurray lol

Thanks to ya all.. I will trouble u all next time if ya all dont mind ..as am trying to learn myself..

Kading Cha = Thanks

mincklerstraat

11:23 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



glad you got it sorted and are figuring stuff out. Yeah, you bet, there's a huge advantage to using code that you understand yourself.