Hello i want a basic search which counts how many records there are ths code works in mysql but when i use it in php its jus prints this code and dont display result

// Standard SQL
$query="SELECT COUNT(*) FROM dates WHERE search='yes'";
echo $query;

Recommended Answers

All 10 Replies

use:

$sql = "SELECT COUNT(*) FROM `dates` WHERE `search` = 'yes'";
$query = mysql_query($sql);
$res = mysql_fetch_row($query);
$total = $res[0];
echo $total;

it says Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource

you have to connect to the database first. i figured you did that but i see you have no idea.

use this:

$host = ''; // mysql host (usually 'localhost')
$user = ''; // mysql username
$pass = ''; //mysql password
$db   = ''; //database name
$con = mysql_connect($host,$user,$pass) or die('Error: ' . mysql_error());
mysql_select_db($db) or die('Error selecting database');

then put the code from the first post here

hello no ive got all the connections its because ive got two sql querys running how can i have two querys running thanks

make sure the variables that contain the query are named different so you don't overwrite anything. i would have to see the code to see exactly what the problem is.

<?



$sql ="SELECT COUNT(*) FROM dates WHERE day='y'"; 
$query = mysql_query($sql);
$res = mysql_fetch_row($query);
$total = $res[0];
echo $total;


$query = "SELECT Code FROM Records WHERE name= '".$_SESSION['name']."'";

// Connect to DB server
$connection = mysql_connect("", "", "");
if (!$connection)
	die("Cannot connect to DB");

// Select database
mysql_select_db("", $connection)
	or die("Cannot find DB");

$result = mysql_query($query, $connection);

// Loop through data and display
while($row = mysql_fetch_array($result))
{
$code = $row['Code'];

} 
// Close connection ! (please !)
mysql_close($connection);

?>

you need to have the database connection before any queries take place.

connections have been made but just taken them out for posting on fourm

you only need to connect once.

k ill have ago it should work thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.