|
Posted by Michael Austin on 10/21/49 11:40
auron wrote:
> Hi there,
> I have a really stupid and banal problem with showing the results of a
> MySQL query in PHP, preciselly with MySQL count() function that gives
> to a variable in PHP the result.
>
> NOTE:
> The problem here is PHP not MySQL, in MySQL everything works just
> fine.
>
> Here is the query that I wrote for getting the number of how much
> records I have in my litte and tottaly trivial DB:
>
> ------------------------------ CODE ----------------------
> $res = @mysql_query("SELECT COUNT(*) FROM my_data_table");
>
> echo ($res);
> ------------------------------ CODE ----------------------
> PS: my_data_table is a name of table not a function or other MySQL
> related syntax.
>
> The result of this query on the screen is:
> Resource id #4
>
you are echoing the result of the connection id not the result of the query -
this is a poorly written example, but you can use it to see what went wrong (or
you can just copy/paste)
$sql = "select count(*) from my_data_table";
$link=mysql_connect($HOST,$USR,$PWD,$DB);
if( $link ) {
mysql_select_db( $DB, $link );
if( $sql ) {
// make sure there are no trailing semi-colons (ie no sql-injection)
$sql = ereg_replace( ";$", "", $sql );
$sql = ereg_replace( "\\\\", "", $sql );
$result = mysql_query( $sql, $link );
if( $result ) {
echo( "<p>Results: for '$sql;'.</p>\n" );
if( $num = mysql_num_rows( $result ) ) {
echo( "<pre>\n" );
while( $array = mysql_fetch_row( $result ) ) {
while( list($key, $val) = each( $array ) ) {
echo "$val | ";
}
echo "<br />\n";
}
echo( "</pre>" );
} elseif( $af = mysql_affected_rows( $link ) ) {
echo( "<p>$af rows affected.</p>\n" );
} else {
echo( "<p>Command completed.</p>\n" );
} } else {
echo( "<p>Bad query: '$sql;'.</p>" );
echo( mysql_errno() . ": " .
mysql_error() . "<br />" );
}
} else {
echo( "<p>Connected to '$DB'.</p>" );
} # end if
} else {
echo( "<p>Bad connection.</p>" );
echo( mysql_errno() . ": " .
mysql_error() . "<br />" );
}
?>
> I was around the net and I get some info that don't give me help.
> The solution from the net was to insert the value of the variable (in
> this case $res) in a function like mysql_fetch_object or
> mysql_fetch_row etc.
>
> After some hours of trying to do something wtih those functions I
> started to think that getting how much records I have in my DB is
> almost a thing that only persons with a extremlly huge brain can
> concive and only a few superior entities can made.
> :)
>
> Can some body with a huge brain help me?
>
> Thank you in advance.
> Auron
>
> PS: I'm not a PRO I get only started to lear about PHP and MySQL.
> because somebody have told me that "was" easy to make some thing with
> those 2 tool.
> http://eye.cc php newsgroups
--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Navigation:
[Reply to this message]
|