Connecting Tech Pros Worldwide Help | Site Map

How come I can't do this query with php script?

21novembre@gmail.com
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi all,

I'm working on my first php+mysql program.
I have a mysqld running and there's a DB named "example" with a table
"tbl". Here it is:
--------------
mysql> use example;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tbl;
+------+----------+----------+----------------+
| idx | UserName | LastName | FreeText |
+------+----------+----------+----------------+
| 2 | Rafi | Ton | Just a test |
| 1 | li | ken | Just two tests |
| 3 | ghjgj | jhn | Jus tests |
+------+----------+----------+----------------+
3 rows in set (0.00 sec)
---------------

When I tried to apply the following sample program, strange result came
to me. Let me show you the script.
---------------
<html>
<head><title>Web Database Sample Index</title>
</head>
<body bgcolor=#ffffff>
<h2>Data from tbl</h2>

<?
mysql_connect() or die ("Problem connecting to DataBase");
$query = "select * from tbl";
$result = mysql_db_query("example", $query);

if ($result) {
echo "Found these entries in the database:<br><p></p>";
echo "<table width=90% align=center border=1><tr>
<td align=center bgcolor=#00FFFF>User Name</td>
<td align=center bgcolor=#00FFFF>Last Name</td>
<td align=center bgcolor=#00FFFF>Domain Name</td>
<td align=center bgcolor=#00FFFF>Request Date</td>
</tr>";

while ($r = mysql_fetch_array($result)) {

$idx = $r["idx"];
$user = $r["UserName"];
$last = $r["LastName"];
$text = $r["FreeText"];

echo "<tr>
<td>$idx</td>
<td>$user</td>
<td>$last</td>
<td>$text</td>
</tr>";
}
echo "</table>";

} else {
echo "No data.";
}

</body>
</html>
---------------
I ran this php page in IE and only to get "No data." as the result,
while I was expecting a list of my table contents. I cannot find the
error I made, actually I almost copied this script from some book.
Please help me to check it.
Any suggestion would be appreciated.

Best, zhy

Ivan Omelchenko 608308824
Guest
 
Posts: n/a
#2: Jul 17 '05

re: How come I can't do this query with php script?


21novembre@gmail.com пишет:[color=blue]
> Hi all,
>
> I'm working on my first php+mysql program.
> I have a mysqld running and there's a DB named "example" with a table
> "tbl". Here it is:
> --------------
> mysql> use example;
> Reading table information for completion of table and column names
> You can turn off this feature to get a quicker startup with -A
>
> Database changed
> mysql> select * from tbl;
> +------+----------+----------+----------------+
> | idx | UserName | LastName | FreeText |
> +------+----------+----------+----------------+
> | 2 | Rafi | Ton | Just a test |
> | 1 | li | ken | Just two tests |
> | 3 | ghjgj | jhn | Jus tests |
> +------+----------+----------+----------------+
> 3 rows in set (0.00 sec)
> ---------------
>
> When I tried to apply the following sample program, strange result came
> to me. Let me show you the script.
> ---------------
> <html>
> <head><title>Web Database Sample Index</title>
> </head>
> <body bgcolor=#ffffff>
> <h2>Data from tbl</h2>
>
> <?
> mysql_connect() or die ("Problem connecting to DataBase");
> $query = "select * from tbl";
> $result = mysql_db_query("example", $query);[/color]
[color=blue]
> I ran this php page in IE and only to get "No data." as the result,
> while I was expecting a list of my table contents. I cannot find the
> error I made, actually I almost copied this script from some book.
> Please help me to check it.
> Any suggestion would be appreciated.
>[/color]

mysql_db_query
Send a MySQL query (PHP 3, PHP 4 , PHP 5)
resource mysql_db_query ( string database, string query [, resource
link_identifier ] )

Note:
This function has been deprecated since PHP 4.0.6. Do not use this
function. Use mysql_select_db() and mysql_query() instead.
JDS
Guest
 
Posts: n/a
#3: Jul 17 '05

re: How come I can't do this query with php script?


On Wed, 22 Jun 2005 05:29:09 -0700, 21novembre wrote:
[color=blue]
> mysql_connect() or die ("Problem connecting to DataBase");
> $query = "select * from tbl";
> $result = mysql_db_query("example", $query);[/color]

Go to the mysql_* section on http://www.php.net

COPY AND PASTE THE CODE EXAMPLES EXACTLY. Change the names.

Problem solved.

No point in reinventing the wheel. Plus the php.net site's examples
include some error checking and whatnot that your script desperately needs.

later...

--
JDS | jeffrey@go.away.com
| http://www.newtnotes.com
DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

Philip Olson
Guest
 
Posts: n/a
#4: Jul 17 '05

re: How come I can't do this query with php script?


Change:
echo "No data.";
To:
echo "No data. And here is why: ";
echo mysql_error();

Notice how $result is evaluating to false, this is because the query
failed. According to the PHP manual mysql_query() (or mysql_db_query())
returns false on failure. Anyway, use mysql_select_db() and
mysql_query() instead.

21novembre@gmail.com
Guest
 
Posts: n/a
#5: Jul 17 '05

re: How come I can't do this query with php script?


Thank you, everybody. It works!!

Closed Thread