Forum Moderators: coopster

Message Too Old, No Replies

Problem with PHP querying a db

Is this correct syntax? cause I'm positive it is

         

mikejson

2:03 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



$result = mysql_query("SELECT * FROM faqs")
or die("Invalid query: " . mysql_error());

The table name is faqs...in that exact case(to rule out case-sensitive mistake).

It is giving me this as the error

Invalid query: No Database Selected

uhm... FROM faqs... isn't that selecting the db?

I have already connected using mysql_connect();

The login as localhost and everything is working correctly from what I can tell. Is this possibly the problem though? I use the connect with blank args, and this is connecting to a db on the local host.

Any thoughts?

willybfriendly

2:05 pm on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



faqs is the table, which is different than the DB.

Put a line like this in:

$db = @mysql_select_db("yourDataBaseName")

WBF

mikejson

3:37 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



Hmm... I can't seem to get the database name though.... I'm not the administrator, and the damn guy is on holidays right now. I'm sure I have to use SHOW DATABASE some how, but I'm kinda new to PHP as well.

Anyone?

Paul in South Africa

3:57 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



The following code should give you a listing of all the database names available from the current connection.

$connect = mysql_connect("localhost");
if (!$connect) die ("Could not connect to MySQL");
$dbases = mysql_list_dbs($connect);
$num = mysql_num_rows($dbases);
for ($x = 0; $x < $num; $x++)
print mysql_tablename($dbases, $x)."<br>";

mikejson

4:12 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



This is my entire script could anyone tell me what the hell I"m doing wrong here....(still the same error, no db selected). The database is called adm, I checked by printing them all out and made sure the spelling was correct.

<?php

$connect = mysql_connect();
or die ("Could not connect to MySQL");

$db = mysql_select_db("adm", $connect);

$result = mysql_query("SELECT * FROM faqs")
or die("Invalid query: " . mysql_error());

mysql_close($connect);

?>

mikejson

5:24 pm on Aug 27, 2003 (gmt 0)

10+ Year Member



I just found out(from the admin that is on vacation), that I have 2 passwords to log in, 1 for sql, and 1 for other.... I wish I knew this earlier

[edited by: eelixduppy at 9:51 pm (utc) on Feb. 18, 2009]

timster

5:31 pm on Aug 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a pickle. "Show databases" would be the command. Have you logged into the mysql server before? What platform are you using?

If you can log into mysql from a terminal..

1. Type "show databases;" to see the database names.
2. Make a note of the database(s) you think might be yours.
3. "Use database [databasename];"
4. "show tables"

You might consider using a database client utility that lets you see the structure of your mysql stuff. (e.g., YourSQL, which is free for MacOS X.)