0

Hi All,

I have a number of different tables storing various bits on information. Ib wanting to create a control panel for it, which will initially give a record count for each table.

How can i run queries from multiple tables in one connection?

Ive tried connecting then running a query, display the results, free results then close, but it only works once, all others give an error

6
Contributors
15
Replies
18
Views
7 Years
Discussion Span
Last Post by adamramadhan
0

Ive tried connecting then running a query, display the results, free results then close, but it only works once, all others give an error

If you close the connection, nothing else will work..
Use something like require_once() to insure only one instance.

0

hi,

Here is the code itself.

It returns the first queries results but the second provides an error on the second query.

<?php

$link = mysql_connect("localhost", "username", "password") or die('error 1<hr>'.mysql_error());
mysql_select_db("database", $link) or die('error 2<hr>'.mysql_error());

$result = mysql_query("SELECT ID FROM TABLE1", $link) or die('Query Error<hr>'.mysql_error());
$num_rows = mysql_num_rows($result);

echo "$num_rows Private Seller records\n";
echo "<br>\n";
mysql_free_result($result);

$result = mysql_query("SELECT ID FROM TABLE2", $link) or die('Query Error<hr>'.mysql_error());
$num_rows = mysql_num_rows($result);

echo "$num_rows records\n";
echo "<br>\n";
mysql_free_result($result);

mysql_close($link);

?>
0

try nixing all those those die statements. just leave the one at the beginning if the connection fails, but not the ones with the queries. If there is an error with those queries the code will exit.

0

Can you explain the benefit of that?

I already did.
Once you have connected to the database, you are connected until you close it. You don't re-connect for every table query. Do the initial database object , then just do your table queries after that.

0

Im not sure if i have made myself clear.

I want to query the rows in two different tables and output. The error codes surely dont matter, as with or without there are no results for anything beyond the 1st query, but they exist in the table.

The code shows i have connected to the database, and have 2 different queries, the first one works, but the second doesnt.

I have removed the dies as you suggest, with no avail. With them, i get the error for query 2, without them i get zero return.

0
<?php

$link = mysql_connect("localhost", "username", "password") or die('error 1<hr>'.mysql_error()); // connect here
mysql_select_db("database"); //select the database in opened account

$result = mysql_query("SELECT ID FROM TABLE1"); //database and link assumed (see manual).
$num_rows = mysql_num_rows($result);

echo "$num_rows Private Seller records\n";
echo "<br>\n";
mysql_free_result($result);

$result = mysql_query("SELECT ID FROM TABLE2"); //also assumed as above
$num_rows = mysql_num_rows($result);

echo "$num_rows records\n";
echo "<br>\n";
mysql_free_result($result);

mysql_close($link);

?>

I guess selecting ID will give you all the ID that exist? Sort of like * but for just one field? Mostly I see * used if you want to dump the entire table contents.
You can put other error trapping in there , but die is a bit extreme unless the initial connect fails, then there is no point of continuing...

0

I want to make a login system in php,where 1 user will be able to log in only one time.pls give me the code of php connecting database

0

I want to make a login system in php,where 1 user will be able to log in only one time.pls give me the code of php connecting database

The following code will connect to the database of your choice (you must enter the database username, password and database name).

<?PHP
	$dbhost = 'localhost';
	$dbuser = 'database_user';
	$dbpass = 'database_password';
	$dbname = 'database_name';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to MySQL');
	mysql_select_db($dbname);	
?>
0

I have copied your code and ran it on one of our servers and it runs perfectly fine. If we add one record to TABLE1 then it says:

1 Private Seller records
0 records

and if we add two records to TABLE2 then it says:

1 Private Seller records
2 records

This leads me to believe that either your connection is not working or you have a spelling mistake with something related to the database/tables.

0

I want to make a login system for my website by php,where 1 user will be able to log in only one time.please help me.
I know how to connect with database through php.

0

Use PDO.

<?php

$pdo = new PDO( /*.. connection parameters ..*/ );

$result[] = (int) $pdo->query('select count(*) from table1')->fetchColumn();

// repeat above code for selected tables and then loop through $result

// if you want to get counts for all tables then use 'SHOW TABLES' in MySQL (PDO query) or pg_tables table in PGSQL

Edited by nileshgr: n/a

0

This thread got hijacked by joyes. The login question should have been started as a new thread!
If the OP is still reading this, please indicate if this thread is solved or not.
Thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.