Click to See Complete Forum and Search --> : db function
cwilkey
02-14-2007, 12:28 PM
Greetings,
I've created a funciton to perform an sql query. I'd like to be able to return the mysql_num_rows value. Could someone please advise? Here is my code:
function query($statement) {
$sql = "$statement";
$call = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
mysql_num_rows($call);
return $call;
}
Here is how I'm calling it:
<?php $getit = query("select * from table);
while ($results = mysql_fetch_assoc($getit)) {
echo mysql_num_rows($getit);
?>
NightShift58
02-14-2007, 01:49 PM
function query($statement) {
$sql = "$statement";
$call = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
mysql_num_rows($call);
return $call;
}You don't need mysql_num_rows() in there, it's just taking up space and not doing anything for you.
<?php $getit = query("select * from table);
while ($results = mysql_fetch_assoc($getit)) {
echo mysql_num_rows($getit);
?>I'm not sure you would want to echo the number of result rows in a loop.
Here's your function with some preventive maintenance:<?php
function query($pSQL="") {
$call = false;
IF ($pSQL <> "" AND (function_exists("mysql_ping") ? @mysql_ping() : true)) {
$call = @mysql_query($pSQL) or die("SQL Error: $pSQL<br>" . mysql_error());
}
return $call;
}
?>
cwilkey
02-14-2007, 02:03 PM
So how can I get the returned number of rows?
NightShift58
02-14-2007, 02:19 PM
<font color="#000000"><?php
$result = query("SELECT * FROM test_table";
echo mysql_num_rows($result);
function query($pSQL="") {
$call = false;
IF ($pSQL <> "" AND (function_exists("mysql_ping") ? @mysql_ping() : true)) {
$call = @mysql_query($pSQL) or die("SQL Error: $pSQL<br>" . mysql_error());
}
return $call;
}
?>
cwilkey
02-14-2007, 03:04 PM
It doesn't seem to work:
<?php
$contact = query("select order from contact"); while ($results = mysql_fetch_assoc($contact)) {
$counter = 1;
while ($counter <= mysql_num_rows($results)){
echo "<option value=" . $counter . ">" . $counter . "</option>";
$counter ++;
}
}
?>
cwilkey
02-14-2007, 03:11 PM
Nevermind, I figured it out....