Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP Databases
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 19th, 2004, 01:43 AM
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to luvskennychesney
Default Can't Connect to Database &Retrieve Name+Address

I have read and read, still don't get how to connect to our school's server. I am suppose to connect to a database called student and get Name, address, city,state,zip from a table called contact and echo it in a mailing lable format. I assume to use break tags as I have before. PLEASE SOMEONE HELP ME!!!
This is what I have came up with. I know something is wrong, but don't know what.
Code:
<?php
$db = mysql_connect('localhost', 'username', 'password');
mysql_select_db('students');
$query = "SELECT * FROM student.contact";
$result = mysql_query($query);
   echo "<br>name";
   echo "<br>address";
   echo "<br>city.state.zip";
mysql_close($db);
?>
 
Old November 19th, 2004, 05:10 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

$result is now an object containing the rows of the query result. you need to go throw them, heres the way I use: If u want to try this ad verbatim, delete ur 3 echo statements, and insert this following text to it lies after the mysql_query and before the mysql_close

while($r = mysql_fetch_array($result)){
echo "<br>Name: " . $r['name'];
echo "<br>Address: " . $r['address'];
echo "<br>City.state.zip: " . $r['city'] . "." . $r['state'] . "." . $r['zip'];
}

Just ask if u got any more questions,

Many shoes,

Jamez/SiliconFuRy
 
Old November 19th, 2004, 05:18 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

I'm actually feeling really generous this morning (and your very lucky) so if u want ur results in a nice table, slip this code in after ur database connection code:

function MakeGenericTable($result){

    $r = mysql_fetch_assoc($result);
    $count = mysql_num_fields($result);
    echo "Number of Rows: " . mysql_num_rows($result) . "<br>\n";
    echo "<table class=results><br>";
    echo "<tr>";

    for ($i=0;$i<$count;$i++) {
         echo "<td class='th'>" . mysql_field_name($result, $i) . "</td>";
    }
    echo "</tr>\n";

    while ($r = mysql_fetch_array($result)) {
         echo "<tr>";
         for ($i=0;$i<$count;$i++) {
             echo "<td>" . $r[$i] . "</td>";
         }
         echo "</tr>\n";

    }

    echo "</table>";
}


Once you've done ur $result=mysql_query($query); just call MakeGenericTable($result);

Many shoes,

Jamez/SiliconFuRy

P.S. just realised, i use this with a style sheet, heres the defs for it too...


table.results {
    position: relative;
    width: 100%;
    table-layout: auto;
    font-family: arial, tahoma, verdana, helvetica, sans-serif;
    font-weight: normal;

    border-collapse: collapse;
    border-spacing: 0;
    border: 1px solid #000;

    font-size: 0.8em;
    background-color: #EEE;
}

td.th {
    font-weight: bold;
    border: 1px solid #000;
    background-color: #BBB;

}
 
Old November 19th, 2004, 12:52 PM
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to luvskennychesney
Default

I used the below code and now i get
 Warning: Access denied for user: 'username@localhost' (Using password: YES) in /home/php_student02/public_html/10a-retrieve.php on line 2

Warning: MySQL Connection Failed: Access denied for user: 'username@localhost' (Using password: YES) in /home/php_student02/public_html/10a-retrieve.php on line 2
Warning: Supplied argument is not a valid MySQL result resource in /home/php_student02/public_html/10a-retrieve.php on line 8

Warning: Supplied argument is not a valid MySQL-Link resource in /home/php_student02/public_html/10a-retrieve.php on line 13
What am I doing wrong now?:(


Code:
<?php
$db = mysql_connect('localhost', 'username', 'password');
mysql_select_db('students');
$query = "SELECT * FROM student.contact";
$result = mysql_query($query);
while($r = mysql_fetch_array($result)){
  echo "<br>Name: " . $r['name'];
  echo "<br>Address: " . $r['address'];
  echo "<br>City.state.zip: " . $r['city'] . "." . $r['state'] . "." . 

$r['zip'];
}
mysql_close($db);
?>
 
Old November 22nd, 2004, 03:11 PM
Registered User
 
Join Date: Sep 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to luvskennychesney
Default

:D Well I finally figured it out. There was a username and password for our school's server it was at the bottom of next week's lesson. You think it would be under this week's huh. Oh well, I thank you for your help, I got some good info from you. For anyone that wants to see the code for it minus the acutally password here it is. This counts the files found and puts in a horizontal line.
Thanks so much.
Code:
<html>
<head>
<title>SQL Query</title>
</head>
<body text="000099">



<?php
$db = mysql_connect('localhost','username','password');
//actually username and password is not shown
mysql_select_db('student');
$query = "SELECT * FROM student.contact";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
// the above code shows how many rows were found
 echo "Number of Rows found are: " . mysql_num_rows($result) . "<br>\n";
//below is just to show you what database was used and what you wanted to query

 echo "Database Selected: <b>$db</b><br> 
          Query: <b>$query</b> 
          <h3>Here are your results!</h3> 
           ";
for ($i=0; $i <$num_results; $i++)
//this means to start at 0 and keep going until no more files found

{
$row = mysql_fetch_array($result);
//remember to use fetch array or fetch rows here

echo "<br><b></b>" . ($row['name']);
*/ I always wanted someone to explain how to echo html with php code first thing is to use double quotes for html and a period to add the php code to it and use () for variables and put what row variable name in [] and remember your ; at the end /*
  echo "<br>" . ($row['address']);
 echo "<br>" . ($row['city']) . "," ." " . ($row['state']) . " " .
 ($row['zipcode']);
//above is how to echo more than one variable in one echo statament
// to echo spaces or commas put them in double quotes. Hope this helps
   echo "";
 

}
mysql_close($db);
?> 

</body>
</html>





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to connect to ms sql 2000 via ip address indraneel ASP.NET 2.0 Professional 1 October 18th, 2006 02:13 AM
Howto retrieve MAC Address of Wireless Connections whlabus Pro Visual Basic 2005 0 June 6th, 2006 11:29 AM
Connect to MSSQL using IP Address echovue PHP How-To 1 April 12th, 2006 03:15 AM
Retreiving IP address & gateway address sjangit VBScript 0 February 3rd, 2004 02:02 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.