Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another problem :( 2

Status
Not open for further replies.

PhoenixDown

Programmer
Jul 2, 2001
279
CA
OK, I'm using this to get data from rows in my table:

Code:
<?

include 'data/config.php';

mysql_connect(&quot;$server&quot;, &quot;$dbusername&quot;, &quot;$dbpassword&quot;) or die(&quot;Could not connect to database. Please make sure data/config.php exists, and it is configured correctly. &quot;);

mysql_select_db(&quot;$dbase&quot;);

$result = mysql_query(&quot;SELECT displayid, status, email, name, link1name, link1url, link2name, link2url, entriesperpage, ipdisplay FROM guestbook_display&quot;);

?>

Then after I have this:

Code:
<input type=&quot;text&quot; name=&quot;GuestbookEmail&quot; size=&quot;40&quot; value=&quot;<?$row[2]?>&quot; maxlength=&quot;150&quot; />

It's not printing it into the field. Why?
 
The order of operations is

mysql_connect
mysql_select_db
mysql_query

then

mysql_fetch_[array|object|assoc]

I don't see where you are fetching the row from the recordset.
 
Sorry.

I edited it now I'm getting these errors:

Code:
Warning: Supplied argument is not a valid MySQL result resource in /home/puremadnezz/php/calvin/data/templates/admin_display.php on line 10

Warning: Supplied argument is not a valid MySQL result resource in /home/puremadnezz/php/calvin/data/templates/admin_display.php on line 12

I changed it to:

Code:
<?

include 'data/config.php';

mysql_connect(&quot;$server&quot;, &quot;$dbusername&quot;, &quot;$dbpassword&quot;) or die(&quot;Could not connect to database. Please make sure data/config.php exists, and it is configured correctly. &quot;);
mysql_select_db(&quot;$dbase&quot;);

$query = &quot;SELECT displayid, status, email, name, link1name, link1url, link2name, link2url, entriesperpage, ipdisplay FROM guestbook_display&quot;;

mysql_fetch_array($row, MYSQL_NUM);

mysql_free_result($row);

?>

What's wrong?
 
Lines are:

mysql_fetch_array($row, MYSQL_NUM);

mysql_free_result($row);
 
You need to review the input parameters and outputs of PHP's mysql family of functions.

mysql_connect takes connection info as a set of strings and returns a connection handle. mysql_query takes your query as a string and and a connection handle and returns a recordset handle. mysql_fetch_array takes a recordset handle and a return type and returns the data from a single row of your recordset.

Here is an example that runs for me:

given that table foo is:

+------+------+
| id | name |
+------+------+
| 1 | foo |
| 2 | bar |
+------+------+

And that foouser has permission to select from that table, the following script:

<?php

$connection = mysql_connect(&quot;localhost&quot;, &quot;foouser&quot;, &quot;&quot;) or die(&quot;Could not connect&quot;);

mysql_select_db(&quot;test&quot;, $connection) or die (&quot;Could not use database&quot;);

$recordset = mysql_query(&quot;SELECT * from foo&quot;, $connection) or die (&quot;Could not perform query&quot;);

print &quot;<html><body><table>&quot;;

while ($row = mysql_fetch_array ($recordset, MYSQL_NUM))
{
print &quot;<tr><td>&quot;;
print $row[0];
print &quot;</td><td>&quot;;
print $row[1];
print &quot;<\td><\tr>&quot;;
}

print &quot;</table></body></html>&quot;;

?>


Produces a nice little uninteresting table on my browser.


Does this help?
 
Yes, it does. But is there any way to get the row array without the use of the while command?
 
The while is just there to stop getting more rows when there are no more rows to get. mysql_fetch_array starts at the first row of the recordset and returns the next row each time it is called. When called and it has already sent the last row, it returns FALSE. If you know that the recordset only has one row, or if you only interested in the first row, drop the while and just call the function.
 
Try this:
Code:
<?

include 'data/config.php';

$linkid = mysql_connect(&quot;$server&quot;, &quot;$dbusername&quot;, &quot;$dbpassword&quot;) or die (&quot;Could not connect to database. Please make sure data/config.php exists, and it is configured correctly. &quot;);

mysql_select_db(&quot;$dbase&quot;, $linkid);

$query = &quot;SELECT displayid, status, email, name, Link1name, Link1url, Link2name, Link2url, entriesperpage, ipdisplay FROM guestbook_display&quot;;
// Easier to use: &quot;SELECT * FROM guestbook_display&quot;?

$result = mysql_query($query, $linkid);

$rownumber = mysql_num_rows($result);

for ($i = 0; $i < $rownumber; $i++) {
	$row = mysql_fetch_array($result, MYSQL_NUM);
<?
<input type=&quot;text&quot; name=&quot;GuestbookEmail&quot; size=&quot;40&quot; value=&quot;<? echo $row[2]; ?>&quot; maxlength=&quot;150&quot; />
?>
}
?>
 
There are also more members of PHP's mysql family of functions that allow you to traverse a recordset randomly.

You can use mysql_num_rows to determine how many rows are in you query recordset, and mysql_data_seek to move the recordset pointer randomly throught the recordset.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top