Solved

How do I call a php function twice on the same page?

Posted on 2007-03-17
4
261 Views
Last Modified: 2010-05-18
Hello everyone,

I'm new of course... PHP Functions  

Error when I use the function twice.
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /includes/fb.php on line 12

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /includes/fb.php on line 14

Here is the code.  I'm trying to call the function twice on the same page - info.php

(info.php)
</head>
<body>

<?PHP
include('/includes/fb.php');

$var1 = 2;
add_rating ($var1); // Should display member 2

?>
<br />

<?php
$var2 = 75;
add_rating ($var2); // Should display member 75

?>

</body>
</html>


If I use the function only "once" like this...  it works...
(info.php)
</head>
<body>

<?PHP
include('/includes/fb.php');

$var1 = 2;
add_rating ($var1); // Should display member 2

?>
</body>
</html>



[Here is the function code]


fb.php

function add_rating ($usernum) {

require_once('Connect/xxxx.php'); // T


mysql_select_db($database_xxxx, $xxxx);
$query_username = "SELECT username, joindate FROM Members WHERE usernum = '$usernum'";
$username = mysql_query($query_username, $usell) or die(mysql_error());
$row_username = mysql_fetch_assoc($username);
$totalRows_username = mysql_num_rows($username);


$rating = $row_username['username'];
$rating .= " <b>";
$rating .= $row_username['joindate'];
$rating .= "</b>";

echo $rating;

return $rating;

mysql_free_result($username);  

}
0
Comment
Question by:usiff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 18740335
you use require_once('Connect/xxxx.php');

That is if page Connect/xxxx.php is already included the file is not included again.

You sholud place the mysql_select_db($database_xxxx, $xxxx); inside Connect/xxxx.php
0
 

Author Comment

by:usiff
ID: 18740415
Hello steelseth12,

I'm not 100% on your comment...

I'm using Dreamweaver to connect to the database.... if that helps

Thanks
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 18740447
put the mysql_select_db function in the same file as the mysql_connect function  like

$db = mysql_connect("localhost","username","pass");
mysql_select_db("db_name",$db);
0
 

Author Comment

by:usiff
ID: 18741048
Thanks...  It was right in front of me....  

Take Care

USiFF
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question