trying to dump out table info

trying to dump out table info

am 13.07.2009 22:53:16 von Govinda

Hi all

I am not sure why this is failing/giving error. I am quite new at PHP/
Mysql, and apologize for what will undoubtedly end up being a trivial
error on my part.
Here is my code:

$db_billing=mysql_connect(localhost,metheuser,mypass,billing );
if (!$db_billing) { die('Could not connect: ' . mysql_error()); }

$sql = "SHOW TABLES";

$result = mysql_query($sql);

foreach(mysql_fetch_assoc($result) as $k => $v) { //line 62
$ssql = "DESCRIBE ".mysql_real_escape_string($v);
$rresult = mysql_query($ssql);
echo "".$k.":
\n";
echo "

\n";
print_r(mysql_fetch_assoc($rresult));
echo "
\n";
echo "
\n";
}

which is giving this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in /home/meee/public_html/somedir/test.php on line 62

I read about:
-mysql_fetch_assoc
-mysql_query
-SHOW TABLES

but do not see why this should be failing. Why isn't $result a '
valid MySQL result resource'?

-G

------------
Govinda
govinda.webdnatalk@gmail.com


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 13.07.2009 22:58:28 von Daniel Brown

On Mon, Jul 13, 2009 at 16:53, Govinda wrote:
> Hi all
>
> I am not sure why this is failing/giving error. =A0I am quite new at
> PHP/Mysql, and apologize for what will undoubtedly end up being a trivial
> error on my part.

Hey, I recognize that code.... ;-P

> Here is my code:
>
> $db_billing=3Dmysql_connect(localhost,metheuser,mypass,billi ng);
> if (!$db_billing) { die('Could not connect: ' . mysql_error()); }
>
> =A0 =A0 =A0 =A0$sql =3D "SHOW TABLES";
>
> =A0 =A0 =A0 =A0$result =3D mysql_query($sql) or die(mysql_error());

Note the addition to the $result definition. This will cause the
script to exit with the MySQL error message (if any) if the issue is
in the query.

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 13.07.2009 23:24:46 von Govinda

On Jul 13, 2009, at 2:58 PM, Daniel Brown wrote:

> On Mon, Jul 13, 2009 at 16:53, Govinda
> wrote:
>> Hi all
>>
>> I am not sure why this is failing/giving error. I am quite new at
>> PHP/Mysql, and apologize for what will undoubtedly end up being a
>> trivial
>> error on my part.
>
> Hey, I recognize that code.... ;-P

:-)

>> Here is the gifted code:
>>
>> $db_billing=mysql_connect(localhost,metheuser,mypass,billing );
>> if (!$db_billing) { die('Could not connect: ' . mysql_error()); }
>>
>> $sql = "SHOW TABLES";
>>
>> $result = mysql_query($sql) or die(mysql_error());
>
> Note the addition to the $result definition. This will cause the
> script to exit with the MySQL error message (if any) if the issue is
> in the query.

when I make that change, it returns this:

"No database selected"

so I try to modify like this:

$db_billing=mysql_connect(localhost,metheuser,mypass,billing );
if (!$db_billing) { die('Could not connect: ' . mysql_error()); }

$sql = "SHOW TABLES FROM billing LIKE 'myTable'";

$result = mysql_query($sql) or die(mysql_error());

foreach(mysql_fetch_assoc($result) as $k => $v) { //line 64
//{DESCRIBE | DESC} tbl_name [col_name | wild]
//$ssql = "DESCRIBE ".mysql_real_escape_string($v);
$ssql = "DESCRIBE myTable ".mysql_real_escape_string($v);
$rresult = mysql_query($ssql);
echo "".$k.":
\n";
echo "

\n";
print_r(mysql_fetch_assoc($rresult)); //line 71
echo "
\n";
echo "
\n";
}

and now the script returns this:

Tables_in_billing (myTable):

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL
result resource in /home/meee/public_html/somedir/test.php on line 72


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 00:40:36 von Daniel Brown

On Mon, Jul 13, 2009 at 17:24, Govinda wrote:
>
> "No database selected"

You're getting warmer....

How would you select a MySQL database in PHP? (Hint: there's a
built-in function.)

--

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 01:28:08 von Govinda

On Jul 13, 2009, at 4:40 PM, Daniel Brown wrote:

> On Mon, Jul 13, 2009 at 17:24, Govinda
> wrote:
>>
>> "No database selected"
>
> You're getting warmer....
>
> How would you select a MySQL database in PHP? (Hint: there's a
> built-in function.)

To try and follow your lead and focus just on what you are saying, to
be rid of that first error that said "No database selected",
I changed the code back to this:

$db_billing=mysql_connect(localhost,metheuser,mypass,billing );
if (!$db_billing) { die('Could not connect: ' . mysql_error()); }

//$sql = "SHOW TABLES FROM billing LIKE 'mytable'";
$sql = "SHOW TABLES"; //line 237

//$result = mysql_query($sql);
$result = mysql_query($sql,$db_billing) or die(mysql_error());
//$result = $db_billing->query("$sql") or die(mysql_error()); // line
241

foreach(mysql_fetch_assoc($result) as $k => $v) { //line 243
//{DESCRIBE | DESC} tbl_name [col_name | wild]
$ssql = "DESCRIBE ".mysql_real_escape_string($v);
//$ssql = "DESCRIBE mytable ".mysql_real_escape_string($v); //line 246
$rresult = mysql_query($ssql);
echo "".$k.":
\n";
echo "

\n";
print_r(mysql_fetch_assoc($rresult)); //line 250
echo "
\n";
echo "
\n";
}

The question is why does line 240 think that no db is selected?!
I read the docs for mysql_query and it says:
resource mysql_query ( string $query [, resource $link_identifier ] )
so that 2nd param is optional; "If the link identifier is not
specified, the last link opened by mysql_connect() is assumed".
But anyway I stuck it in there too, to test, and I get the same
result, i.e. "No database selected".
I tried line 241 too. No luck.
So I am really stumped (lame as that feels/sounds). Line 237 is just
a string assignment. Line 240 *IS* being handed the $link to use,
which AFAIK means a reference to the database as well as the table, so
why the error?


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 01:42:36 von Daniel Brown

On Mon, Jul 13, 2009 at 19:28, Govinda wrote:
>
> =A0 =A0 =A0 =A0$db_billing=3Dmysql_connect(localhost,metheuser,mypass,bi l=
ling);
> if (!$db_billing) { die('Could not connect: ' . mysql_error()); }

if(!mysql_select_db('dbname',$db_billing)) die(mysql_error());

> =A0 =A0 =A0 =A0//$sql =3D "SHOW TABLES FROM billing LIKE 'mytable'";
> =A0 =A0 =A0 =A0$sql =3D "SHOW TABLES"; //line 237

The fourth parameter you have in mysql_connect() only evaluates
within the engine as a boolean True statement. RTFM to see why. ;-P

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 02:17:10 von Govinda

On Jul 13, 2009, at 5:42 PM, Daniel Brown wrote:

> On Mon, Jul 13, 2009 at 19:28, Govinda
> wrote:
>>
>> $db_billing=mysql_connect(localhost,metheuser,mypass,billing );
>> if (!$db_billing) { die('Could not connect: ' .
>> mysql_error()); }
>
> if(!mysql_select_db('dbname',$db_billing))
> die(mysql_error());
>
>> //$sql = "SHOW TABLES FROM billing LIKE 'mytable'";
>> $sql = "SHOW TABLES"; //line 237
>
> The fourth parameter you have in mysql_connect() only evaluates
> within the engine as a boolean True statement. RTFM to see why. ;-P

Dan, I am really making the effort.. despite how it may look.

I read about mysql_connect again and see that 4th param is optional.
seemingly intended for when we want to force a new link and not just
use one that was already opened. Well I am just at this point trying
to use a first link, so I would assume I need to say what db I am
trying to talk to. Too bad the docs do not give a single example
where the 4th param is being used. Anyway I see in the function def.
that that 4th is a boolean. That does not make sense to me since I
think it should be a string (the name of the database). Anyway it was
working with PEAR DB with this:
$db_billing=DB::connect('mysql://metheuser:mypass@localhost/ billing');
and still does seem to be connecting ok with this now (without pear db)-
$db_billing=mysql_connect(localhost,metheuser,mypass,billing );
....no error arises from the next line-
if (!$db_billing) { die('Could not connect: ' . mysql_error()); }
....so we must be talking to the db, right?
And to my real question, how can I get mysql_query what it needs in
this line:
$result = mysql_query($sql) or die(mysql_error());

I learn the best by *seeing* example code. I wish I could see a
working example.

-G

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 02:29:42 von Daniel Brown

On Mon, Jul 13, 2009 at 20:17, Govinda wrote:
>
> Dan, I am really making the effort.. =A0despite how it may look.

I believe you. I sent you the function and code that should work
for you, but it may have been lost in the chopped replies. So it'll
be something like this:

$host =3D 'localhost';
$user =3D 'user_name';
$pass =3D '123';
$db_name =3D 'my_database';

$link =3D mysql_connect($host,$user,$pass);
$conn =3D mysql_select_db($db_name,$link);

$sql =3D "SHOW TABLES";
$result =3D mysql_query($sql);
// ....
?>

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 14:16:16 von host

Govinda wrote:
>
>
> I learn the best by *seeing* example code. I wish I could see a
> working example.
>
> -G
>
Govinda,

Dan has shown you the path. Good luck.

The problem I have always had with the PHP Manual (online) has been the
lack of complete examples. Yes, there are examples of how to use each
function. But functions such as the MySQL related ones require multiple
steps to get to a point where anything works. There are at least two
steps to get any database function to work 1)connect to database
2)select connected database. Until these two things are correct, none
of the other functions will work because you do not have a database to
work on.

Great list

Dewey Williams



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: trying to dump out table info

am 14.07.2009 15:15:44 von Daniel Brown

On Tue, Jul 14, 2009 at 08:16,
host@deweywilliams.com wrote:
>
> The problem I have always had with the PHP Manual (online) has been the l=
ack
> of complete examples. =A0Yes, there are examples of how to use each funct=
ion.
> But functions such as the MySQL related ones require multiple steps to ge=
t
> to a point where anything works. =A0There are =A0at least two steps to ge=
t any
> database function to work 1)connect to database 2)select connected databa=
se.
> =A0Until these two things are correct, none of the other functions will w=
ork
> because you do not have a database to work on.

This is a continuous process, and one of which we're definitely
aware. For now, you can get more information and examples from the
user notes at the bottom of each page, but we are working on official
examples as well. For some things, however, full examples will not be
provided, as it would be out of the scope of the individual function's
manual entry. In such events, the official documentation is a good
reference, but not a substitute for learning on your own and from
others.

That said, we are *always* looking for people who would be serious
about providing long-term assistance to the PHP Project, including the
official documentation. If you're interested in learning how to do
so, get in touch with me off-list at danbrown@php.net and I'll be
happy to point you in the right direction.

--=20

daniel.brown@parasane.net || danbrown@php.net
http://www.parasane.net/ || http://www.pilotpig.net/
Check out our great hosting and dedicated server deals at
http://twitter.com/pilotpig

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php